943,884 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1263
  • C# RSS
Sep 16th, 2009
0

location and size questions

Expand Post »
Hi all!

Well, again I find myself stuck in a silly situation. I've found some more settings that seem to baffle me.

First off all, why can't I set the Right property of a control?

Example:
C# Syntax (Toggle Plain Text)
  1. PictureBox ExamplePictureBox = new PictureBox();
  2. ExamplePictureBox.Right = 50;

This returns that "Property or indexer 'System.Windows.Forms.Control.Right' cannot be assigned to -- it is read only (CS0200)"
So... why can I perfectly assign a Left property but not a Right? Same goes for Bottom.

Secondary question, is there a way to allign/dock/anchor something in the way that I want some button to stick to the upper-right corner no matter what (maybe insert some margin of 10 pixels, but I can probably figure that out)? I have tried to Anchor it using something like (AnchorStyle.Top | AnchorStyle.Right) (not sure of the term AnchorStyle, but something along those lines) but that didn't work.

Then a Location question. The intellisense tells me I can either get or set stuff to it, though every time I try to use something like this.Location.X = 250; It tells me it isn't a variable and won't build... The same goes for DesktopLocation and several others I have found and tried but can't remember at the moment. Which property should I set to manually assign a location? (The point of this is that I would like to have a status-form on the right side of my main form, maybe that clarifies some stuff)


Thanks for the help and time!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008
Sep 16th, 2009
0

Re: location and size questions

I don't know either why that is.
You should ask the implementors of the TextBox class.
What I do know is that you cannot set the Height of a TextBox with a MultiLine property set to false. (the default) In this case the height depends on the height of the Font used.
I also know a Rectangle is a Point and a Size (among others).
This seems to be the only option for a TextBox.
So if you want to change the Location of a TextBox, change it by assigning a new Point to it. TextBox.Location.X = 50; will not work either.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Sep 16th, 2009
0

Re: location and size questions

Hi all!

Okay, then the new Point is clear enough to me, define a new point, assign it and use the variable Point to assign a new location.

The controls in question are PictureBox and Form by the way, TextBox is more or less known to me since they don't have a whole lot of options.

Well, I guess the question is still sort of open, that's a first from my experience If anybody else has some ideas please post them, and I will see what MSDN has to offer! (And if I happen to find a solution before it is posted here, I will of course let you guys know!)

Thanks for the help so far.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008
Sep 16th, 2009
1

Re: location and size questions

ok lets jump off this dock.

the X and Y properties of a location type cannot be set directly. so no you can't set them that way.

the "anchor" property should do what you want, but only if the position of your controls is set prior to the setting of the anchor property. ( in code execution order)
that looks something like this.
[icode=csharp]
Control.Anchor = AnchorStyles.Top | AnchorStyles.Right
[/icode]

the control.Right property is "get" only. that means it tells you the distance in pixels from the right edge of the control to the right edge of the parent control, you can't set it because its actually just a calculation, it doesn't point toward an actual variable. it looks something like this...
[Code = csharp]
Public Int string Right()
{
get
{
return (this.Location.X + this.Width) - this.Parent.Width;
}
}
[/code]

So setting it will fail.


you can also manually stick a control to the top right corner using the forms on resize event, but the anchor property does just fine.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Sep 16th, 2009
0

Re: location and size questions

Hi all!

Thanks you very much for the information!! I will try and work with the Anchor and AnchorStyle's and see if I can fit in a margin (I'm not using any VisualStyles so I don't have a border and placing something right on the edge looks horrible. any tips or hints on that would also be appreciated)

And I'll see if I find it usefull enough to write a simple function to place something on the right side of something else.

Thank you for all your help, this clarifies it all, at least enough for me to move on.

Will leave this thread open for 12-24 hours for any more responses. then I will marked it solved.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008
Sep 16th, 2009
0

Re: location and size questions

Glad i could help, and as for the padding, just place the control the distance from the anchored sides that you want it to stay, and then the anchor property handles it for you , it will stay the same distance from the anchored edges at all times.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Sep 17th, 2009
0

Re: location and size questions

Hi all!

Thank you very much for all the help, this has gotten me all the help I needed.

For reference to others:

The Location property needs a variable like this
C# Syntax (Toggle Plain Text)
  1. Point ExamplePoint = new Point(200, 200);
  2. this.Location = ExamplePoint;

And the Right property is read-only and just used for reference. (Which makes me wonder why people don't just use Width.)


And last but not least, the DesktopLocation can also be used on your MainForm but has to be AFTER your form has been created, NOT before. I prefer to load things like that in the this.Load event, but I guess there are other and maybe better ways.

Last 12 hours go into effect now.

Muchos gracias everyone! Once again you've provided all the info I need.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Total of 3 Newb Questions on This Code...
Next Thread in C# Forum Timeline: drag and drop , copy and paste images





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC