Click here to Skip to main content
15,891,703 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a form that display a button create dynamically in it, in the run time the button doesn't appear normally, it doesn't show the text in it and it's back color

What I have tried:

my code:
C#
Button b = new Button();
                   
                    b.Size = new Size(77, 30);
                    b.BackColor =  Color.Red;
                    b.ForeColor = Color.White;


                   
                    b.Location = new Point(1470, 140 );
                  
                    b.Name = "button1";
                    b.Text = "Save";
                    
                    b.Click += new EventHandler(button_Click);
                    
                    Controls.Add(b);

and for event handler 
             protected void button_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
           //-------- do something here ////////
         
        }
Posted
Updated 18-Nov-18 1:22am
v4
Comments
OriginalGriff 18-Nov-18 1:46am    
Show us the code fragment that you used to create the button - we can't do anything without that!
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 18-Nov-18 3:34am    
b.Location = new Point(1470, 140 );
Are you sure that location is within the limits of your form?
RickZeeland 18-Nov-18 4:54am    
Nothing wrong with your code, I tried it and it works, maybe you should retry with a new Winforms project. Also make sure you don't have any buttons on the Form with the same name ...

When I try your code, I get a red button with the white text "Save" well over to the right of my form.

This is exactly what I would expect.
I would suggest that you start by using the debugger to ensure that the code is executed properly, and if the button is still not visible or correct, change the location to 0,0 to ensure it is within the form and probably not covered by some other control. (If necessary, move controls in the designer to ensure the top left corner of you form is empty first.)

But apart from a large X location value, the code appears to work fine here.
 
Share this answer
 
Hi,

It seems that the location is not fitting on the screen or is hidden by another control right on that location, or the project did not build clean (without error). I would follow the below steps to solve this problem:

1- Put a breakpoint and trace the code behind the form step-by-step and make sure it runs without any error or exception
2- Change the location to 0,0 in order to make sure that the display has enough place to present the button then re-arrange the location based on your own wish
3- Make sure the control is not seating under another control, there are several ways to check this, one is to remove all other controls, another is to use the BringToFront method.

Cheers,
Aydin
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900