Avatar of lep1
lep1
 asked on

Button added to UserControl Form not visible

Depending on where I add an 'Apply Changes" button on a UserControl form, it is sometimes not visible.  (BTW: the button forces control to an addhandler to serialize data and control switch setting).  It seems that if the button is added to the lower left or lower right corner of the form, it is visible, but if added to the top, the UserControl seems to mangle the button(?)

Also, there is some interesting behavior if placement of the UserControl is above the added button, and this behavior acts like a button click.  Thus, there seems to be volatility if a button added to a UserControl Form is anywhere near the UserControl.

Below is the code I am using:

 
        Dim myform As New Form
        myuc.autosize = True 'Note myuc is the actual usercontrol, instantiated as an object from usercontrol resources
        myform.AutoSize = True
        myform.Dock = DockStyle.Fill
        myform.Controls.Add(myuc)
        myform.Show()
        myform.Width = 0
        myform.Height = 0
        myform.Width = myuc.width
        myform.Height = myuc.height + 50
        myform.Left = 0
        myform.Top = 0
        Dim btn As New Button
        btn.Text = "Apply"
        btn.Top = myform.Bottom - 50
        btn.Left = myform.Right - 60
        btn.BringToFront()
        myform.Controls.Add(btn)

Open in new window

.NET ProgrammingMicrosoft DevelopmentVisual Basic.NET

Avatar of undefined
Last Comment
lep1

8/22/2022 - Mon
AndyAinscow

You don't specify a width and height for the button - try setting those values and then it should appear.  (If width and / or height are zero what will you see ?)
Ioannis Paraskevopoulos

I would also advise to check the Anchor property of the button. If it is set to bottom and right, and you put it on the top left corner and resize your user control, you may have the button outside the visible bounds.

Giannis
lep1

ASKER
Thanks for the responses, however, the above recommendations didn't work.  It appears that when the "Apply Changes" button is placed between the user control and the top of the form, the usercontrol tends to act like a button click since the form disappears.  However, if the button is placed at the bottom right or bottom left of the form using the original code (provided above), everything works normally.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Nasir Razzaq

You do not set the top and left properties of myuc control so it is added as top left location and probably hides the button behind itself or shows it on top.
ASKER CERTIFIED SOLUTION
lep1

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lep1

ASKER
None of the experts solutions worked, however, my solution solved the issue