Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Events with dynamic listbox creation.

Author  Topic 

olay80
Yak Posting Veteran

62 Posts

Posted - 2005-06-08 : 10:26:09
hey guys,
i'm dynamically creating listboxes and assigning events to each.
lb.Click += new EventHandler(change);
the function change is invoked upon the click on one of those listboxes.
what i need is how to know what listbox was clicked, knowing that i create the listboxes as follows:

for (int i = 0; i < 4; i++)
{
ListBox lb = new ListBox();
lb.Location = new System.Drawing.Point(j,20);
lb.Size = new System.Drawing.Size(75,75);
lb.Name = lbNames[i];

lb.Click += new EventHandler(change);
this.groupBox1.Controls.Add(lb);
}
this.Controls.Add(this.groupBox1);


i need to let know the function change which listbox was clicked.

thanx,
Oliver

jhermiz

3564 Posts

Posted - 2005-06-08 : 10:49:38
lb.name ?



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-08 : 11:43:00
Make sure your event handler has the correct signature; most likely it is something like this:

private void OnChange(object sender, EventArgs e)

(when you write the code to attach the event handler, the UI should give you the option to press TAB to build this for you)

If the function has the correct declaration, then you can cast the "sender" variable to a ListBox and get whatever property you need from there.

Here's an example:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

for (int i=0; i<4;i++)
{
Button b = new Button ();
b.Text = "Button " + i.ToString();
b.ID = "Button" + i.ToString();
b.Click +=new EventHandler(OnClick);
Page.FindControl("Form1").Controls.Add(b);
Page.FindControl("Form1").Controls.Add (new LiteralControl("<BR>"));
}

}

private void OnClick(object sender, EventArgs e)
{
Response.Write("The button clicked was : " +( (Button) sender).ID + "<BR>");
}
- Jeff
Go to Top of Page

olay80
Yak Posting Veteran

62 Posts

Posted - 2005-06-09 : 01:31:10
thanx alot Jeff.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2005-06-09 : 01:47:32
I like to assign a single instance of the handler..

EventHandler changehandler = new EventHandler(OnClick);
for
{
b.Click += changehandler;
}



DavidM

A front-end is something that tries to violate a back-end.
Go to Top of Page

olay80
Yak Posting Veteran

62 Posts

Posted - 2005-06-09 : 04:18:01
hey guys,

what if i want to see the sender of the next event?meaning:
i have generated 5 dynamic listboxes each attached to an event i want to know when i click on the second listbox for example to be able to see the sender object of the third listbox???

thanx,
Oliver
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-09 : 07:49:49
quote:
Originally posted by byrmol

I like to assign a single instance of the handler..

EventHandler changehandler = new EventHandler(OnClick);
for
{
b.Click += changehandler;
}



DavidM

A front-end is something that tries to violate a back-end.



That would be much better; great call ...

Oliver -- That kind of makes no sense what you are asking ... you can't look into the future to predict which control a user is going to click! The best you can do is store a reference to the first control clicked in a member variable, and then in the second event you now have references to both controls and can do what you want.

I get the feeling, however, that you are making things more complicated than they need to be ... if you can give us a specific example of what you are trying to do that would be helpful.

- Jeff
Go to Top of Page
   

- Advertisement -