C#控件添加到面板中环

编程入门 行业动态 更新时间:2024-10-23 04:44:37
本文介绍了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#控件添加到面板中环

本文发布于:2023-11-06 13:40:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   中环   面板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!