本文介绍了C#控件添加到面板中环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个文件中添加一个按钮,每行的面板。 到目前为止我的代码是:
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.
推荐答案中的代码看起来不错,但有可能是以下几种情况。
The code looks fine but there could be a following situations.
1.You可能只有一个入口在该文件中,因此,您所遇到只有一个按钮添加到面板。
1.You might have only one entry in the file, so you are experiencing only One Button added to the panel.
2.您的面板宽度比所有的动态按钮的总和较小的宽度
我怀疑2号是造成问题的主要原因。
I suspect no 2 is the main reason that is causing problem.
所以,我建议您使用 FlowLayoutPanel的。要添加一个动态的内容,因为它会自动布局所有子控件。
So, I recommend that you use FlowLayoutPanel. To add a dynamic content as it automatically layout all the child controls.
更多推荐
C#控件添加到面板中环
发布评论