CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    177

    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?

  2. #2
    Join Date
    Aug 2005
    Posts
    198

    Re: Dropdownlist add handler

    Use the 'AddHandler' operator to wire up event handling at run time.

    e.g.,

    AddHandler someObject.someEvent, AddressOf someMethod
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  4. #4
    Join Date
    Aug 2005
    Posts
    198

    Re: Dropdownlist add handler

    The OP's control is dynamically created - that's exactly what 'AddHandler' is for.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Dropdownlist add handler

    I guess I missed that part.. carry on
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Dropdownlist add handler

    Quote Originally Posted by David Anton View Post
    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.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  8. #8
    Join Date
    Sep 2009
    Location
    .NET 2003 FWK 1.1
    Posts
    24

    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

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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.

  10. #10
    Join Date
    Sep 2009
    Location
    .NET 2003 FWK 1.1
    Posts
    24

    Re: Dropdownlist add handler

    Sorry, you're right. I stand corrected.

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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
  •  





Click Here to Expand Forum to Full Width

Featured