WeldonT

I'm trying to open a new instance of a form with LinkLabel. Right now it looks something like this (left the code at home so not exact)

private void OnClick(object sender, System.EventArgs e)
{
Form2 recipe = new Form2(name);
}

[...]
LinkLabel clickme = new LinkLabel();
clickme.LinkClick += new System.EventHandler(OnClick(recipe.Name)); //I think the event handler I used was 2-3 words s //longer than this one, but I can't remember it right now.

The problem is I can not understand from C# Language specification how to pass OnClick() a value and also cause it to open a new form. How do event handlers work, and is there an easier way to make a link label open a new form




Re: Visual C# Express Edition new form with LinkLabel?

paoloTheCool

You mean like this:

Code Block

private void Form1_Load(object sender, EventArgs e)

{

Form form2 = new Form();

form2.Text = "Title";

form2.Size = new Size(100, 300);

LinkLabel lb = new LinkLabel();

lb.Text = "Click Me";

lb.Location = new Point(20, 20);

lb.Click += new EventHandler(lb_Click);

form2.Controls.Add(lb);

form2.Show();

}

private void lb_Click(object sender, EventArgs e)

{

//Code Here

}

Thanks,

paoloTheCool





Re: Visual C# Express Edition new form with LinkLabel?

WeldonT

Paolo,

Thanks for pointing me in the right direction! But I have a question about your code. Won't it open up form2 when form1 loads I want to open up form2 when I click on 'lb'.





Re: Visual C# Express Edition new form with LinkLabel?

paoloTheCool

Yah thats what my example does...but if you would like to make it open on lb_click Smile

Thanks,

paoloTheCool





Re: Visual C# Express Edition new form with LinkLabel?

WeldonT

paoloTheCool wrote:

Yah thats what my example does...but if you would like to make it open on lb_click

Thanks,

paoloTheCool



Great! So my second question is the syntax in LB_Click(object sender, blahblahblah){}

Is "object sender" something I can change to send it my own object.. or is it part of the call. My form2 has a constructor that requires a string and I need the onclick event to send "name" to the method call that will open up form2. I can't figure out via the object browser if that's even possible.





Re: Visual C# Express Edition new form with LinkLabel?

WeldonT

Something like this.
paoloTheCool wrote:

Code Block

private void Form1_Load(object sender, EventArgs e)

{

[...]

lb.Click += new EventHandler(lb_Click(recipe.Name)); //possible

[..]

}

private void lb_Click(string recipe.Name, object sender, EventArgs e) //Wrong..

{

Form2 recipe = new Form2(recipe.Name)

}

Thanks,

paoloTheCool






Re: Visual C# Express Edition new form with LinkLabel?

paoloTheCool

Haha...sorry, but I can't understand what you are trying to do. Could you explain it a little better

Thanks,

paoloTheCool





Re: Visual C# Express Edition new form with LinkLabel?

WeldonT

Hmm.. ok I'll try to give it another shot with no code this time.

I am making a windows form that will have 20 to 30 labels on it. I want to make those labels actually LinkLabels. Each label will be the name of a recipe.. and when you click the label it will open up a new form with the recipe printed out on it. All the info for the recipes is stored in a .xml document so when I click on the label I want to be able to send just the "name" of the recipe to the new form. Then the new form, using the name, can look it up and display all the info.

Is that clearer Sorry about all the confusion, I think this is a relatively simple matter but my book doesn't explain how to do it right and everything I've found online shows how to set up web links.. not launch new forms. hm.

Weldon





Re: Visual C# Express Edition new form with LinkLabel?

jrboddie

The sender argument contains the information about the label that was clicked. In the event handler you can access this information by casting sender to the proper type like this:

(sender as LinkLabel).Text

This will give you the text property of the LinkLabel that was clicked. From your previous posts, your text property may contain the the name and description. If so, you might consider using the tag property of the LinkLabel. Than is, when the LinkLabel is created assign just the name to the tag property like this.

LinkLabel lab = new LinkLabel();

lab.Tag = name;

then in the event handler, access the value like this:

(sender as LinkLabel).Tag.ToString()

Since you are passing this to a new form, it would be:

Form2 f2 = new Form2((sender as LinkLabel).Tag.ToString());

Where Form2 is expecting a string in its constructor.





Re: Visual C# Express Edition new form with LinkLabel?

WeldonT

jrboddie wrote:

The sender argument contains the information about the label that was clicked. In the event handler you can access this information by casting sender to the proper type like this:

(sender as LinkLabel).Text

This will give you the text property of the LinkLabel that was clicked. From your previous posts, your text property may contain the the name and description. If so, you might consider using the tag property of the LinkLabel. Than is, when the LinkLabel is created assign just the name to the tag property like this.

LinkLabel lab = new LinkLabel();

lab.Tag = name;

then in the event handler, access the value like this:

(sender as LinkLabel).Tag.ToString()

Since you are passing this to a new form, it would be:

Form2 f2 = new Form2((sender as LinkLabel).Tag.ToString());

Where Form2 is expecting a string in its constructor.



That did it! Many thanks from me and my wife (she's been sweeping up my torn out hair for two days now). Thanks PaoloTheCool and JRBoddie for all the help!

Although now I'm curious to find out what's going on behind this eventhandler stuff so I can use it correctly in the future.