好的,我不知道如何做到这一点,因为我正在尝试自学C#并同时创建一个工作程序。
我有一个IP地址列表:
List<IPAddress> addresses
它可以包含1到1500个IP地址。我可以使用此调试代码访问此列表中的所有项目,以验证列表是否设置正确。
// Loop through the array and send the contents of the array to debug window.
for (int counter = 0; counter < addresses.Count; counter++)
{
System.Diagnostics.Debug.WriteLine(addresses[counter]);
}
我的Windows窗体中有一个基本的FlowPanelLayout容器,可以向上和向下滚动:
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.AutoScroll = true;
this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 67);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(1172, 597);
this.flowLayoutPanel1.TabIndex = 1;
我想在该列表中创建一个新的FlowLayoutPanel foreach IPAddress。
从Research看起来我可以使用列表作为Dictionary文件为每个FlowPanel创建一个新的变量名称,但我不确定如何。
我做了一个测试FlowPanel并提出了下面的布局,我希望每个动态创建的FlowPanel都可以滚动,如果需要从左到右,有一个标签(IP地址)和6个按钮,每个位置都有它们上面有相同的文字:Router,Switch,Steelhead,InPath,Server和NCAP。
我还希望以后能够为每个正在创建的按钮执行On Click和On Load Events。在创建它们时我是否必须立即执行此操作,或者以后我是否可以单独执行此操作?
//
// flowLayoutPanel2
//
this.flowLayoutPanel2.Controls.Add(this.label2);
this.flowLayoutPanel2.Controls.Add(this.button1);
this.flowLayoutPanel2.Controls.Add(this.button2);
this.flowLayoutPanel2.Controls.Add(this.button3);
this.flowLayoutPanel2.Controls.Add(this.button4);
this.flowLayoutPanel2.Controls.Add(this.button5);
this.flowLayoutPanel2.Controls.Add(this.button6);
this.flowLayoutPanel2.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;
this.flowLayoutPanel2.Location = new System.Drawing.Point(3, 3);
this.flowLayoutPanel2.Name = "flowLayoutPanel2";
this.flowLayoutPanel2.Size = new System.Drawing.Size(1071, 57);
this.flowLayoutPanel2.TabIndex = 0;
//
// label2
//
this.label2.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(3, 18);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(129, 20);
this.label2.TabIndex = 0;
this.label2.Text = "199.169.250.126";
//
// button1
//
this.button1.Location = new System.Drawing.Point(138, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(150, 50);
this.button1.TabIndex = 1;
this.button1.Text = "Router";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(294, 3);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(150, 50);
this.button2.TabIndex = 2;
this.button2.Text = "Switch";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(450, 3);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(150, 50);
this.button3.TabIndex = 3;
this.button3.Text = "Steelhead";
this.button3.UseVisualStyleBackColor = true;
//
// button4
//
this.button4.Location = new System.Drawing.Point(606, 3);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(150, 50);
this.button4.TabIndex = 4;
this.button4.Text = "InPath";
this.button4.UseVisualStyleBackColor = true;
//
// button5
//
this.button5.Location = new System.Drawing.Point(762, 3);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(150, 50);
this.button5.TabIndex = 5;
this.button5.Text = "Server";
this.button5.UseVisualStyleBackColor = true;
//
// button6
//
this.button6.Location = new System.Drawing.Point(918, 3);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(150, 50);
this.button6.TabIndex = 6;
this.button6.Text = "NCAP";
this.button6.UseVisualStyleBackColor = true;
答案 0 :(得分:0)
我会使用以FlowLayoutPanel
开头的构造,并插入一系列TableLayoutPanel
控件。每个TableLayoutPanel
包含一行,包含7列,一行用于标签和六个按钮:
namespace IPAddressDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<IPAddress> ipAddresses = new List<IPAddress>();
ipAddresses.Add(new IPAddress(new Byte[] { 127, 0, 0, 1 }));
ipAddresses.Add(new IPAddress(new Byte[] { 198, 0, 0, 1 }));
ipAddresses.Add(new IPAddress(new Byte[] { 255, 255, 255, 255 }));
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Fill;
flp.AutoScroll = true;
flp.AutoSize = true;
flp.FlowDirection = FlowDirection.TopDown;
flp.WrapContents = false;
foreach (var ipa in ipAddresses)
{
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.AutoSize = true;
tlp.Dock = DockStyle.Fill;
tlp.RowCount = 0;
tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tlp.ColumnCount = 0;
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
Label lbl = new Label();
lbl.Dock = DockStyle.Fill;
Byte[] ba = ipa.GetAddressBytes();
lbl.Text = String.Format("{0}.{1}.{2}.{3}", ba[0], ba[1], ba[2], ba[3]);
tlp.Controls.Add(lbl, 0, 0);
Button b = new Button();
b.Text = "Router";
b.Dock = DockStyle.Fill;
b.Click += btnRouter_Click;
tlp.Controls.Add(b, 1, 0);
b = new Button();
b.Text = "Switch";
b.Dock = DockStyle.Fill;
b.Click += btnSwitch_Click;
tlp.Controls.Add(b, 2, 0);
b = new Button();
b.Text = "Steelhead";
b.Dock = DockStyle.Fill;
b.Click += btnSteelhead_Click;
tlp.Controls.Add(b, 3, 0);
b = new Button();
b.Text = "InPath";
b.Dock = DockStyle.Fill;
b.Click += btnInPath_Click;
tlp.Controls.Add(b, 4, 0);
b = new Button();
b.Text = "Server";
b.Dock = DockStyle.Fill;
b.Click += btnServer_Click;
tlp.Controls.Add(b, 5, 0);
b = new Button();
b.Text = "NCAP";
b.Dock = DockStyle.Fill;
b.Click += btnNCAP_Click;
tlp.Controls.Add(b, 6, 0);
flp.Controls.Add(tlp);
}
this.AutoSize = true;
this.Controls.Add(flp);
}
private void btnRouter_Click(Object sender, EventArgs e)
{
MessageBox.Show("You clicked the Router button!");
}
private void btnSwitch_Click(Object sender, EventArgs e)
{
MessageBox.Show("You clicked the Switch button!");
}
private void btnSteelhead_Click(Object sender, EventArgs e)
{
MessageBox.Show("You clicked the Steelhead button!");
}
private void btnInPath_Click(Object sender, EventArgs e)
{
MessageBox.Show("You clicked the InPath button!");
}
private void btnServer_Click(Object sender, EventArgs e)
{
MessageBox.Show("You clicked the Server button!");
}
private void btnNCAP_Click(Object sender, EventArgs e)
{
MessageBox.Show("You clicked NCAP the button!");
}
}
}
答案 1 :(得分:0)
我建议使用像DataGrid和DataGridView这样的东西,因为当查看超过50行时,排序和过滤功能对用户更有用。
无论如何,进入FlowLayout路线。您将要创建一个“查看”视图。要显示的每个IP地址的对象。您希望对象为您完成所有工作。你在地址上的循环可能如下所示:
foreach (IPAddress address in addresses)
{
//Assuming this outputs the Address.
IPAddressView aView = new IPAddressView(address.ToString());
aView.ButtonClicked += IPAddress_ButtonClicked;
flowLayoutPanel1.Controls.Add(aView.FlowPanel);
}
private void IPAddress_ButtonClicked(object sender, IPAddressView.ButtonType type)
{
IPAddressView aView = sender as IPAddressView;
switch (type)
{
case IPAddressView.ButtonType.Router:
{
DoSomething();
break;
}
...
}
}
这是IPAddressView类。
public class IPAddressView
{
public enum ButtonType
{
Router,
Switch,
Steelhead,
InPath,
Server,
NCAP
}
public FlowLayoutPanel FlowPanel {get; private set; }
public Label Address { get; private set; }
public Button Router { get; private set; }
public Button Switch { get; private set; }
public Button Steelhead { get; private set; }
public Button InPath { get; private set; }
public Button Server { get; private set; }
public Button NCAP { get; private set; }
public IPAddressView(string address)
{
FlowPanel = new FlowLayoutPanel();
Address = new Label() { Text = address };
Router = new Button();
Switch = new Button();
Steelhead = new Button();
InPath = new Button();
Server = new Button();
NCAP = new Button();
Router.Click += (s, e) => { OnButtonClicked(ButtonType.Router); };
Switch.Click += (s, e) => { OnButtonClicked(ButtonType.Switch); };
Steelhead.Click += (s, e) => { OnButtonClicked(ButtonType.Steelhead); };
InPath.Click += (s, e) => { OnButtonClicked(ButtonType.InPath); };
Server.Click += (s, e) => { OnButtonClicked(ButtonType.Server); };
NCAP.Click += (s, e) => { OnButtonClicked(ButtonType.NCAP); };
FlowPanel.Controls.AddRange(new Control[] { Address, Router, Switch, Steelhead, InPath, Server, NCAP });
}
public delegate void ButtonClickedHandler(object sender, ButtonType type);
public event ButtonClickedHandler ButtonClicked;
private void OnButtonClicked(ButtonType type)
{
if (ButtonClicked != null)
{
ButtonClicked(this, type);
}
}
}
希望这有帮助!