|
-
August 13th, 2010, 10:44 PM
#1
Dropdownlist add handler
Hi,
I would like to add handler to a dropdownlist which is dynamically created. I have the following code:
Dim ddl1 as new Dropdownlist
ddl1.DataTextField = "Hours"
ddl1.DataValueField = "HourID"
ddl1.DataSource = GetHours()
ddl1.DataBind()
How can I add handler to the ddl1 so that when ddl1 selected index changed, then the text in the ddl1 will be assigned to Textbox1.text?
Can anyone advice?
-
August 14th, 2010, 07:22 PM
#2
Re: Dropdownlist add handler
Use the 'AddHandler' operator to wire up event handling at run time.
e.g.,
AddHandler someObject.someEvent, AddressOf someMethod
-
August 15th, 2010, 08:56 AM
#3
Re: Dropdownlist add handler
SelectedIndexChanged is an included event, no need to write and new one.
Double click on the combo box and it should place you in the code block for the event you mention. Just put your code to update the textbox in that event.
Always use [code][/code] tags when posting code.
-
August 15th, 2010, 10:31 AM
#4
Re: Dropdownlist add handler
The OP's control is dynamically created - that's exactly what 'AddHandler' is for.
-
August 15th, 2010, 06:16 PM
#5
Re: Dropdownlist add handler
I guess I missed that part.. carry on
Always use [code][/code] tags when posting code.
-
August 16th, 2010, 11:43 AM
#6
Re: Dropdownlist add handler
 Originally Posted by David Anton
The OP's control is dynamically created - that's exactly what 'AddHandler' is for.
Hi David.
Actually, I don't agree with using the AddHandler here. Why? Well, why hasn't this dropdownlist control not set up to Inherit from ComboBox? If it is indeed inheriting from CombBox, why re invent the wheel with new handlers and so on. This is exactly why Inheritance exists, so that the child class can make use of the base class' methods and properties.
-
August 16th, 2010, 01:00 PM
#7
Re: Dropdownlist add handler
I have not tried this but I thought I would throw it out there.
Have you tried to define the dropdownlist with events? I am thinking that this should give you access to all the events you would have with a drop down list or am I missing something here?
Always use [code][/code] tags when posting code.
-
August 18th, 2010, 05:37 AM
#8
Re: Dropdownlist add handler
What exactly is a dropdown list? Assuming it is some kind of customised ComboBox you should do this:
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents _dropDownList1 As ComboBox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
_dropDownList1 = New ComboBox
_dropDownList1.Size = New Size(200, 40)
_dropDownList1.Location = New Point(20, 100)
_dropDownList1.Parent = Me
_dropDownList1.Show()
_dropDownList1.Items.Add("Item1")
_dropDownList1.Items.Add("Item2")
AddHandler _dropDownList1.SelectedIndexChanged, AddressOf _dropDownList1_SelectedIndexChanged
End Sub
Private Sub _dropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Do something when the selected index changes
End Sub
End Class
-
August 18th, 2010, 06:10 AM
#9
Re: Dropdownlist add handler
When you declare an object using WithEvents you do not need AddHandler at all, as the events will alread be "loaded" as well. We have established that. The only two options would be to either Inherit from the ComboBox class or using WithEvents.
-
August 18th, 2010, 06:26 AM
#10
Re: Dropdownlist add handler
Sorry, you're right. I stand corrected.
-
August 18th, 2010, 07:17 AM
#11
Re: Dropdownlist add handler
That's how we learn
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
|