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:

PictureBox ExamplePictureBox = new PictureBox();
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!

Recommended Answers

All 6 Replies

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.

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 :P 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.

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. Control.Anchor = AnchorStyles.Top | AnchorStyles.Right 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...

Public Int string Right()
{ 
get
{
     return (this.Location.X + this.Width) - this.Parent.Width;
}
}

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.

commented: Good explanation! +13

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.

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.

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

Point ExamplePoint = new Point(200, 200);
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.