473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

adding eventhandler programatically

Hi all,

I have a question...

I'm creating tabs programmatically using vb.net . in each tab that is created there is listbox, and 4buttons (also added by coding).

when i change the listbox selected item, an event should be called, lets say a messagebox will appear with the selected index of that tab.

I want to know how can I make this handler work for all the listbox that I create in different tabs?

What do you think? any advice?

Regards,
HK
Feb 15 '08 #1
5 1806
dzenanz
45
1. make handler as target for SelectedIndexChanged event of all listboxes.
2. in handler, cast sender to listbox, and then use it's parent property (parent should be TabControl), which has TabIndex property, c#:
((TabControl)(((ListBox)sender).Parent)).TabIndex

HTH, Dženan
Feb 15 '08 #2
1. make handler as target for SelectedIndexChanged event of all listboxes.
2. in handler, cast sender to listbox, and then use it's parent property (parent should be TabControl), which has TabIndex property, c#:
((TabControl)(((ListBox)sender).Parent)).TabIndex

HTH, Dženan
how can I make 1? (do u have code)
Feb 15 '08 #3
dzenanz
45
doubleclicking on one of the listboxes will make a handler named something like listBox1_SelectedIndexChanged. click on second listbox, open it's property page, go to it's events, find SelectedIndexChanged, and in dropdown list next to it choose listBox1_SelectedIndexChanged. Repeat procedure for as many listboxes as you like.
If you want to programatically add event handlers, use syntax (copied from form1.desinger.cs):
Expand|Select|Wrap|Line Numbers
  1. listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
Finally, you should have handler that looks like this:
Expand|Select|Wrap|Line Numbers
  1. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3.     this.Text /*form title*/= ((TabControl)(((TabPage)(((ListBox)sender).Parent)).Parent)).SelectedIndex.ToString();
  4. }
and sets forms's title to 0, 1, 2, ... depending on tab's index in which listbox was nested.
note:
sender=listbox
sender.parent=tabpage
sender.parent.parent=tabcontrol
tabcontrol has property selectedindex, and that is what you need

ps. This is C# synthax.
Feb 15 '08 #4
Thanks for the reply..

the first one didn't work for me. but your hint helped me to make it working after doing some search where i found the solution. it is:

Expand|Select|Wrap|Line Numbers
  1. AddHandler ListBox1new.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
  2.  
and there is no need to remove the
Expand|Select|Wrap|Line Numbers
  1. Handles ListBox1.SelectedIndexChanged
from
Expand|Select|Wrap|Line Numbers
  1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
because when I removed it, the original ListBox1 didn't work ;)

and this worked very nice :)

Many thanks
Feb 16 '08 #5
dzenanz
45
I knew this is more the question of syntax, and less of algorithm, but C# code helps more than no code at all :D
Feb 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the...
5
by: Patrick | last post by:
Hello I have the following problem. I have an aspx Page. This page contains an ASP-Table Object. So in that table, I have a linkbutton. So, I can't edit the event of that button trough the...
11
by: Neo Geshel | last post by:
Greetings. Hopefully someone will be able to untie this Gordian Knot I have found myself bound up in. I am trying to add a submit button dynamically to a PlaceHolder. This button will help...
8
by: Neo Geshel | last post by:
Greetings. Hopefully someone will be able to untie this Gordian Knot I have found myself bound up in. I am trying to add a submit button dynamically to a PlaceHolder. This button will help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.