Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Add Controls To Panel In a Loop

I wish to add a button for every line in a file to a panel. My code so far is:

StreamReader menu = new StreamReader("menu.prefs");
int repetition = 0;

while(!menu.EndOfStream)
{
    Button dynamicbutton = new Button();
    dynamicbutton.Click += new System.EventHandler(menuItem_Click);
    dynamicbutton.Text = menu.ReadLine();
    dynamicbutton.Visible = true;
    dynamicbutton.Location = new Point(4+repetition*307, 4);
    dynamicbutton.Height = 44;
    dynamicbutton.Width = 203;
    dynamicbutton.BackColor = Color.FromArgb(40,40,40);
    dynamicbutton.ForeColor = Color.White;
    dynamicbutton.Font = new Font("Lucida Console", 16);
    dynamicbutton.Show();
    menuPanel.Controls.Add(dynamicbutton);
    repetition++;
    MessageBox.Show(dynamicbutton.Location.ToString());
}
menu.Close();

The problem is that only the first control gets created.

like image 268
YoshieMaster Avatar asked Jun 20 '11 04:06

YoshieMaster


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

The code looks fine but there could be a following situations.

1.You might have only one entry in the file, so you are experiencing only One Button added to the panel.

2.Your panel width is smaller than the sum of all the dynamic buttons width.

I suspect no 2 is the main reason that is causing problem.

So, I recommend that you use FlowLayoutPanel. To add a dynamic content as it automatically layout all the child controls.

like image 51
crypted Avatar answered Oct 18 '22 04:10

crypted


Each time it is generating the same name for dynamic controls. That's the reason why it is showing only the last one. It simply overwrites the previous control each time.

like image 36
AspMyLife Avatar answered Oct 18 '22 03:10

AspMyLife


int x = 4;
int y = 4;
foreach(PhysicianData pd in listPhysicians)
{
   x = 4;
   y = panPhysicians.Controls.Count * 30;
   RadioButton rb = new RadioButton();
   rb.CheckedChanged += new System.EventHandler(rbPhysician_CheckedChanged);
   rb.Text = pd.name;
   rb.Visible = true;
   rb.Location = new Point(x, y);
   rb.Height = 40;
   rb.Width = 200;
   rb.BackColor = SystemColors.Control;
   rb.ForeColor = Color.Black;
   rb.Font = new Font("Microsoft Sans Serif", 10);
   rb.Show();
   rb.Name = "rb" + panPhysicians.Controls.Count;
   panPhysicians.Controls.Add(rb);
}
like image 39
Daniel Howard Avatar answered Oct 18 '22 02:10

Daniel Howard