|
-
Sep 21st, 2006, 01:10 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] AddHandler Question
I need some reading material on how to set up an
event handler using AddHandler.
I'm having no luck using online help.
I think I could use an example.
I've added some button
controls at run-time but I havent yet
determined how to dynamically add their associated
handlers using AddHandler.
I'm using VB2005.net
Thanks
-
Sep 21st, 2006, 01:20 PM
#2
Re: AddHandler Question
like so
VB Code:
Dim Button1 As New Button
AddHandler Button1.Click, AddressOf Button1_click
Private Sub Button1_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'do stuff
End Sub
-
Sep 21st, 2006, 01:33 PM
#3
Thread Starter
Frenzied Member
Re: AddHandler Question
Well, I got that part during testing with a button of a specific name.
However, I'm allowing the user to name the buttons.
So I'm wondering if I can tailor the event name to the name the user gives the button.
Thanks
-
Sep 21st, 2006, 01:46 PM
#4
Re: AddHandler Question
I am not really sure what you are asking. So you want the user to be able to create a button and then define the text on the button correct? do all the created buttons run through the same handler? If so, you can get which button was pressed.
VB Code:
Dim btn as Button = CType(sender, Button)
Not really sure if this is what you are looking for, but it would help if you could post some of your code, so I can geta better idea
-
Sep 21st, 2006, 01:47 PM
#5
Thread Starter
Frenzied Member
Re: AddHandler Question
Ah, I see. Thank you bmahler.
I can just use the same handler for each button added.
Then determine within the event which button was clicked.
-
Sep 21st, 2006, 01:48 PM
#6
Thread Starter
Frenzied Member
Re: AddHandler Question
Of course, now I must find how to decide which button was clicked.
Keeping in mind that I won't know the names of
the buttons because they will be named at run-time.
-
Sep 21st, 2006, 01:49 PM
#7
Re: AddHandler Question
you got it, once you use the line that i posted above, you can get the btn.Text or btn.Tag or whatever you need.
If I have fully answered your question, please remember to mark this thread resolved.
-
Sep 21st, 2006, 01:51 PM
#8
Thread Starter
Frenzied Member
Re: AddHandler Question
I'll work with the line of code you provided.
I may have some more questions on how
to use that line of code.
Thanks
-
Sep 21st, 2006, 01:53 PM
#9
-
Sep 21st, 2006, 02:08 PM
#10
Thread Starter
Frenzied Member
Re: AddHandler Question
Ok, that works great.
Thanks.
I would like to read up on what was going on
there. We designated that button as
being of a type that can send messages?
Or what?
Anyway that will work for me.
Now, I need to name an array after the name
that the user entered for the button.
Any suggestions?
The user enters the button name and from that
point on when the user clicks on that button,
I must choose which array will be used to fill
a list.
-
Sep 21st, 2006, 02:17 PM
#11
Re: AddHandler Question
here is the msdn page that describes th addHandler event
http://msdn2.microsoft.com/en-us/library/7taxzxka.aspx
you could try to store the name of the array to be used in the tag of the button. I am not exactly sure how to help you with this part, because I have no idea what your code is doing.
-
Sep 21st, 2006, 02:25 PM
#12
Thread Starter
Frenzied Member
Re: AddHandler Question
What my code is doing:
I have a single DataGridView.
The DataGridView will be used to display the contents
of an array. Which array depends on the button that is
clicked (the same buttons the user named).
The user creates and names the button.
They then add items to the DataGridView.
Later they can click on any particular button and
retrieve the items that they added earlier.
It seems to me that the logical thing to do is to
name the arrays after the button name.
Then when the user clicks a button, I can compare the
button name with the array names (if possible)
and retrieve the correct array.
-
Sep 21st, 2006, 03:04 PM
#13
Re: AddHandler Question
I really need to see your code to see what is happening when the user creates these buttons and the arrays.
-
Sep 21st, 2006, 03:17 PM
#14
Re: AddHandler Question
how bout this, create a structure
Place these in a module
VB Code:
Public Structure lists
Dim name As String
Dim list() As String
End Structure
Public myLists() As lists
Then when a user creates a button you can add and item
VB Code:
Dim i As Integer = myLists.GetUpperBound(0) + 1
ReDim Preserve myLists(i)
myLists(i).name = "name of button user picked"
ReDim myLists(i).list(1) 'Redim this to the number of items that are going to be entered into the array
'Add items to the array
myLists(i).list(0) = "item"
myLists(i).list(1) = "item"
Then when the user clicks a button you can retrieve the array
VB Code:
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button
btn = CType(sender, Button)
For i As Integer = 0 To myLists.GetUpperBound(0)
If btn.Text = myLists(i).name Then
'you now have you array of data to do whatever with
'Array is myLists(i).list()
End If
Next
End Sub
-
Sep 21st, 2006, 03:38 PM
#15
Thread Starter
Frenzied Member
Re: AddHandler Question
I'm studying your code...
-
Sep 21st, 2006, 05:14 PM
#16
Thread Starter
Frenzied Member
Re: AddHandler Question
Thanks bmahler,
I already posted this once but it disappeared.
Everything works perfectly.
It would have taken me days to come up with that.
Thanks for taking the time.
-
Sep 21st, 2006, 05:49 PM
#17
Re: [RESOLVED] AddHandler Question
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|