Dynamic Button Controls (Anchor) Property Problem

G

Guest

I have a simple form with nothing on it, except one button named btnTesting.
I'm only using this button for testing purposes. I have a module that scans a
database and displays a button for each Customer who has an order. The module
that generates the buttons dynamically works great. Now, when I started this
project I had placed several other buttons on the form called btnExit and
btnOpen. But, for some reason, when my module that created the buttons for
each Customer ran, it somehow wiped out ALL other buttons on the form.

So, to solve this problem, I figured why not just create these two buttons
dynamically as well and that would solve the problem. So, "after" my other
buttons for the Customers were created, I created my two final buttons using
the following code:

Dim btn1 As New Button
btn1t.Location = New System.Drawing.Point(888, 603)
btn1.Margin = New System.Windows.Forms.Padding(2)
btn1.Name = "btnExit"
btn1.Size = New System.Drawing.Size(117, 66)
btn1.TabIndex = 1
btn1.Text = "Exit"
Controls.Add(btn1)
AddHandler btn1.Click, AddressOf btnExit_Click

I basically copied this code from my Designer.vb file so that I knew it
would be correct.

Bingo, my two buttons appear and they work properly too. So, when I tested
the form I realized I did not set the Anchor properties because when I
resized the form, the buttons did not stay anchored properly. So, I set the
anchor properties on my existing button and then went into the Designer.vb
file to see how the Anchor property should be coded. Once I got this
information I added the following line of code to my code above. This was to
set the anchor property for my btnExit button to the Right and Bottom areas.
I also set the anchor property for my btnOpen button as well.

btn1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or
System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

Then, when I ran the program again, bingo, the problem came back. To test a
little more, I took the Anchor Property setting away from one of my Dynamic
buttons (btnOpen) and when I ran the program again, the btnOpen button
appeared, but not the btnExit button, which had the Ancho Property set.

So, I have concluded that the Anchor Property must have some conflicts when
other buttons are being created dynamically. Because this did not just happen
with my code here, it also happened when I didn't create the buttons
dynamically.

Now, here's the interesting thing. If I try to create all of my dynamic
buttons as a part of the Form_Load, Form_Activate or Form_Enter events all
the buttons with no Anchor Property set appear, but the btnExit button with
the Anchor property set does not appear. Now, if I run the code that
dynamically creates the button from a button on the form, instead of the
Form_Load event, it all works correctly, even the buttons with the Anchor
Property set. But, if I switch it back to the Form_Load event, it doesn't
work.

So, in summary, if I try to create my buttons during the Form_Load or
Form_Activate events, all buttons with the Anchor Property not set will
appear, while those with the Anchor property set will not appar. Yet, If I
load the form with no buttons created dynamically and then I run the same
procedure to create the buttons, they all show up correctly, including the
btnExit button that has the Anchor Property set.

All I can figure is there must be some conflict with the form being created
and the Anchor Property being set. I figure, in reality, it is being created,
but I just can't see it. Does this make any sense? If so, how do I get around
this problem?

Why is this happening and if theres are reason why, what is the solution to
get around it? Basically, I need to be able to dynamically create my Customer
buttons and I need to have two buttons on the form "anchored" to the lower
right portion of the form. I don't care if these two buttons are created by
code or via the form. Can anyone help me out with this?

Thanks,
 
L

Linda Liu[MSFT]

Hi,

I performed a test based on your description but didn't reproduce the
problem on my side.

In fact, I don't think there's a conflict with the form being created and
the Anchor property of the child controls.

The following is the code of my test.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim btnexit As New Button
btnexit.Location = New Point(Me.Width - 40 - btnexit.Width,
Me.Height - btnexit.Height - 70)
btnexit.Text = "Exit"
btnexit.Anchor = CType(AnchorStyles.Right Or AnchorStyles.Bottom,
AnchorStyles)
AddHandler btnexit.Click, AddressOf btnexit_Click
Me.Controls.Add(btnexit)

Dim btnopen As New Button
btnopen.Location = New Point(btnexit.Left - 10 - btnopen.Width,
Me.Height - btnopen.Height - 70)
btnopen.Text = "Open"
btnopen.Anchor = CType(AnchorStyles.Right Or AnchorStyles.Bottom,
AnchorStyles)
AddHandler btnopen.Click, AddressOf btnopen_click
Me.Controls.Add(btnopen)

End Sub

Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
MsgBox("Exit button is clicked")
End Sub
Private Sub btnopen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
MsgBox("Open button is clicked")
End Sub

End Class

Please try it on your machine to see if the problem still exits and let me
know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu[MSFT]

Hi,

I am reviewing this post and would like to know the status of this issue.

If the problem is still not solved, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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