anchor property for button not working

T

TS

When I set the anchor property in code, the button doesn't move:
this.cmdStartTutorial.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);


When I change the anchor property from top & left defaults at design time I
get one of these errors depending on which sides I set:
"object type cannot be converted to target type"
"cannot widen from target type to primitive type"

What is the matter with this?

Also, I eventually want to put this positioning on a base class that the
form will inherit from. Will there be any reason for this not working if it
works on a regular form?

thank you!
 
T

TS

Oh yeah, I'm trying to make this button always positioned at the top right
of the form. Like I said, the button will be created dynamically on the base
class, if this has any bearing on the outcome (the base class is just a
class, not a form):
Button cmdLanguage = new Button();

cmdLanguage.Size = new Size(80,24);

cmdLanguage.Text = "Language";

cmdLanguage.Font = new Font(cmdLanguage.Font,cmdLanguage.Font.Style);

THIS DOESN"T WORK SO I SET THE LEFT PROPERTY BELOW TO SIMULATE IT-------
//cmdLanguage.Anchor = AnchorStyles.Right;

cmdLanguage.Left = this.Width - cmdLanguage.Width - 8; //I had to add 8
more, I guess for the form's border??

// Add a delegate to handle the Click event.

cmdLanguage.Click += new System.EventHandler(this.cmdLanguage_Click);

this.Controls.Add(cmdLanguage);
 
T

TS

Oh yeah, I'm trying to make this button always positioned at the top right
of the form. Like I said, the button will be created dynamically on the base
class, if this has any bearing on the outcome (the base class is just a
class, not a form):
Button cmdLanguage = new Button();

cmdLanguage.Size = new Size(80,24);

cmdLanguage.Text = "Language";

cmdLanguage.Font = new Font(cmdLanguage.Font,cmdLanguage.Font.Style);

THIS DOESN"T WORK SO I SET THE LEFT PROPERTY BELOW TO SIMULATE IT-------
//cmdLanguage.Anchor = AnchorStyles.Right;

cmdLanguage.Left = this.Width - cmdLanguage.Width - 8; //I had to add 8
more, I guess for the form's border??

// Add a delegate to handle the Click event.

cmdLanguage.Click += new System.EventHandler(this.cmdLanguage_Click);

this.Controls.Add(cmdLanguage);
 
T

TS

Oh yeah, I'm trying to make this button always positioned at the top right
of the form. Like I said, the button will be created dynamically on the base
class, if this has any bearing on the outcome (the base class is just a
class, not a form):
Button cmdLanguage = new Button();

cmdLanguage.Size = new Size(80,24);

cmdLanguage.Text = "Language";

cmdLanguage.Font = new Font(cmdLanguage.Font,cmdLanguage.Font.Style);

THIS DOESN"T WORK SO I SET THE LEFT PROPERTY BELOW TO SIMULATE IT-------
//cmdLanguage.Anchor = AnchorStyles.Right;

cmdLanguage.Left = this.Width - cmdLanguage.Width - 8; //I had to add 8
more, I guess for the form's border??

// Add a delegate to handle the Click event.

cmdLanguage.Click += new System.EventHandler(this.cmdLanguage_Click);

this.Controls.Add(cmdLanguage);
 
T

TS

Oh yeah, I'm trying to make this button always positioned at the top right
of the form. Like I said, the button will be created dynamically on the base
class, if this has any bearing on the outcome (the base class is just a
class, not a form):
Button cmdLanguage = new Button();

cmdLanguage.Size = new Size(80,24);

cmdLanguage.Text = "Language";

cmdLanguage.Font = new Font(cmdLanguage.Font,cmdLanguage.Font.Style);

THIS DOESN"T WORK SO I SET THE LEFT PROPERTY BELOW TO SIMULATE IT-------
//cmdLanguage.Anchor = AnchorStyles.Right;

cmdLanguage.Left = this.Width - cmdLanguage.Width - 8; //I had to add 8
more, I guess for the form's border??

// Add a delegate to handle the Click event.

cmdLanguage.Click += new System.EventHandler(this.cmdLanguage_Click);

this.Controls.Add(cmdLanguage);
 
R

Rhett Gong [MSFT]

Hi TS,
Glad to see you have found a workaround for your problem.

Since you said your base class was just a class (not a form class) and you
used "this.Controls.Add" in your code, I feel a bit unsure with your base
class. What class your base class is inherited from? Is it a user control
or just a simple calss?
Though we have some bugs with the archor, it is hard for me to say why your
class won't work while it works fine on a regular form. If you would like
to get more details on this problem, I could assist you to go further .


Thanks,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
T

TS

the base class inherits from the UIProcess block's WindowsFormView class,
which inherits from windows.forms.form, so my base class is a form class.

This is my first windows form project. I was under the impression that
setting the anchor property would make the control stick to the sides I
designate, and there wouldn't be any space between the edge of the form and
the control. Is this correct, or is the anchor used to keep a control in the
same spot relative to the sides i select, which would permit space between
the edge of the form (I figured this behavior was by default, so the anchor
would do something different)

If I have this base class and I want a button to be put in the top right
corner up against the border of the form on every inherited form, how do I
do it if not by controls.add? If I just put this control at a spot on the
base form's design page, the control doesn't show up at run time on the
inherited forms.

thanks
 
T

TS

Also, the method I was using to position the element:
cmdLanguage.Left = this.Width - cmdLanguage.Width - 8;

Does not work well. The location of this button is not always on the edge in
every inherited form. It is usually off by 3 or 4 pixels either part of it
goes off the edge of the form, or there is a gap between the button and the
edge of the form. Why does this occur, and i'm still looking for the better
solution, hoping the anchor property is what I need.
 
R

Rhett Gong [MSFT]

If I have this base class and I want a button to be put in the top right
corner up against the border of the form on every inherited form, how do I
do it if not by controls.add?
Since your base class inherits from form, we can treat it as a form class
in some way. Then to add a control inside this class, we use Controls.Add.
If I just put this control at a spot on the
base form's design page, the control doesn't show up at run time on the
inherited forms.
From your description, it seems that the language button is created
dynamically and not a member of your base class, so it depends on your code
of base class whether to show at run time on the inherited form or not.
Could you tell where you added the following code snippet in the base form
class?
//----------------------------------------------------
Button cmdLanguage = new Button();
.........
this.Controls.Add(cmdLanguage);
//-----------------------------------------------------
Also, the method I was using to position the element:
cmdLanguage.Left = this.Width - cmdLanguage.Width - 8;
Does not work well. The location of this button is not always on the edge in
every inherited form.
With this line, you just sets the distance between the left edge of your
button and the left edge of its container's client area. To set the
position of the button in the form, please use the Location property. If
the base form is set with a fixed size, this should be easy, otherwise, you
need to handle the SizeChanged event and re-calculate the position by
yourself.
Or, you could use the anchor property to keep an anchored distance to the
edges of the Form. As I said before, this is an amazing feature of forms,
but it has some bugs to make its behavior unstable.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
T

TS

If I just put this control at a spot on the
From your description, it seems that the language button is created
dynamically and not a member of your base class, so it depends on your code
of base class whether to show at run time on the inherited form or not.
Could you tell where you added the following code snippet in the base form
class?
//----------------------------------------------------
Button cmdLanguage = new Button();
........
this.Controls.Add(cmdLanguage);
//-----------------------------------------------------
This is in the initialize event. Like I said, I tried to just put the
control on the page at design time, then I would set its location at run
time, but doing this made the button not appear, so thats why i created it
dynamically.

Rhett Gong said:
If I have this base class and I want a button to be put in the top right
corner up against the border of the form on every inherited form, how do I
do it if not by controls.add?
Since your base class inherits from form, we can treat it as a form class
in some way. Then to add a control inside this class, we use Controls.Add.
If I just put this control at a spot on the
base form's design page, the control doesn't show up at run time on the
inherited forms.
From your description, it seems that the language button is created
dynamically and not a member of your base class, so it depends on your code
of base class whether to show at run time on the inherited form or not.
Could you tell where you added the following code snippet in the base form
class?
//----------------------------------------------------
Button cmdLanguage = new Button();
........
this.Controls.Add(cmdLanguage);
//-----------------------------------------------------
Also, the method I was using to position the element:
cmdLanguage.Left = this.Width - cmdLanguage.Width - 8;
Does not work well. The location of this button is not always on the edge in
every inherited form.
With this line, you just sets the distance between the left edge of your
button and the left edge of its container's client area. To set the
position of the button in the form, please use the Location property. If
the base form is set with a fixed size, this should be easy, otherwise, you
need to handle the SizeChanged event and re-calculate the position by
yourself.
Or, you could use the anchor property to keep an anchored distance to the
edges of the Form. As I said before, this is an amazing feature of forms,
but it has some bugs to make its behavior unstable.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

Hi,
Now, as you said, I create a form (call it form1) and I add a button
dynamically in the form. (see below)
//-------------------------code begin-----------------------------
//----as you said in initialize, i add the button in form1's constructor
public Form1()
{
InitializeComponent();
System.Windows.Forms.Button cmdLanguage = new Button();
cmdLanguage.Location = new Point(100,0);
cmdLanguage.Size = new Size(80,24);
cmdLanguage.Text = "Language";
cmdLanguage.Font = new Font(cmdLanguage.Font,cmdLanguage.Font.Style);
cmdLanguage.Anchor = AnchorStyles.Left;
cmdLanguage.Click += new System.EventHandler(this.cmdLanguage_Click);
this.Controls.Add(cmdLanguage);
}
//-------------------------end----------------------------------------
Then I create another form (call it form2) which is inherited from form1
(public class Form2 : ResizeTest.Form1). Since all buttons are from form1,
I feel confused how you can move the button in the design of form2. If you
set the postition of the button no matter it is in design time or runtime,
it will keep a fixed relative position as it is in form1.
From my test the anchor property works fine in most situations, though it
fails sometimes due to known bugs.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

I am glad to know you have it working in your side. :)


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
L

Lee Gillie

One of my co-workers was having this exact problem.
What we found is that what you did AFTER setting the
anchors caused the error. For her, she would click on
some other property to get out of the anchor selector,
and it would immediately EDIT that field. This is
when she saw the errors you cite. When I clicked away,
the next property value was made current, but it was
not actually editing it. A subtle difference.

The work around we found was for her to always hit the
ENTER key to get out of the anchor selector at design
time.

I suspect some kind of mouse or desktop option she has
set. It is interesting others see this same problem.

Does this observation register with others???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top