你给Form2添加一个带参数的构造函数不就可以么? Form1: private void button2_Click(object sender, EventArgs e) { Form2 form = new Form2("your id"); form.ShowDialog(); } Form2:private string ID; public Form2(string id) { InitializeComponent(); this.ID = id; //Get the data from database by ID. }
可是我form1上的显示数据不知道怎么搞,linklabel个数是不确定的,Form2 form = new Form2("your id");中your id应该如何获取呢?要show form2的话应该是点击某一个linklabel,id应该保存到linklabel的哪个属性去啊。
string[] urls = new string[] { "http://www.google.com", "http://www.facebook.com", "http://www.twitter.com", "http://www.youtube.com", "http://www.myspace.com", "http://www.amazon.com" }; for (int i = 0; i < urls.GetLength(0); i++) { LinkLabel ll = new LinkLabel(); ll.Name = "ll" + i.ToString(); ll.Text = urls[i]; ll.Location = new System.Drawing.Point(30, i * 50); ll.Size = new System.Drawing.Size(224, 16); ll.Link.LinkData = urls[i]; ll.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked); ll.Visible = true; Controls.Add(ll); } private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) { // Determine which link was clicked within the LinkLabel. this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true; // Display the appropriate link based on the value of the // LinkData property of the Link object. string target = e.Link.LinkData as string; // If the value looks like a URL, navigate to it. // Otherwise, display it in a message box. if(null != target && target.StartsWith("www")) { System.Diagnostics.Process.Start(target); } else { MessageBox.Show("Item clicked: " + target); } }
Form1: private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2("your id");
form.ShowDialog();
}
Form2:private string ID;
public Form2(string id)
{
InitializeComponent();
this.ID = id;
//Get the data from database by ID.
}
可是我form1上的显示数据不知道怎么搞,linklabel个数是不确定的,Form2 form = new Form2("your id");中your id应该如何获取呢?要show form2的话应该是点击某一个linklabel,id应该保存到linklabel的哪个属性去啊。
{ "http://www.google.com", "http://www.facebook.com", "http://www.twitter.com",
"http://www.youtube.com", "http://www.myspace.com", "http://www.amazon.com" };
for (int i = 0; i < urls.GetLength(0); i++)
{
LinkLabel ll = new LinkLabel();
ll.Name = "ll" + i.ToString();
ll.Text = urls[i];
ll.Location = new System.Drawing.Point(30, i * 50);
ll.Size = new System.Drawing.Size(224, 16);
ll.Link.LinkData = urls[i];
ll.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
ll.Visible = true;
Controls.Add(ll);
} private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
// Determine which link was clicked within the LinkLabel.
this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true; // Display the appropriate link based on the value of the
// LinkData property of the Link object.
string target = e.Link.LinkData as string; // If the value looks like a URL, navigate to it.
// Otherwise, display it in a message box.
if(null != target && target.StartsWith("www"))
{
System.Diagnostics.Process.Start(target);
}
else
{
MessageBox.Show("Item clicked: " + target);
}
}
这个就可以区分不同的名字了。this.Controls["ll0"] 就是第一个。关于linkLabel1_LinkClicked,可以通过 sender 参数区分。