Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a VB project I am working on and I can't, for the life of me, remember how to force call a SelectionChanged event. Below is the code I am trying to call...
VB
Private Sub cbxName_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles cbxName.SelectionChanged
    Try
        Dim cmb As ComboBox = sender
        Dim i As Integer = cmb.SelectedIndex
        Dim cust As Customer = Customers(i)
        Me.DataContext = cust
    Catch ex As Exception

    End Try
End Sub

...and here is the code where I am trying to call it. (I'm doing it this way, because for some reason the sub isn't being called when my If/Then statement fires.)
VB
Private Sub RetrieveProperties()
    Try
        If appDRAW.Documents.Count = 0 Then Exit Sub
        dcmnt = appDRAW.ActiveDocument
        If dcmnt.FileName <> "" Then txbPONum.Text = Left(dcmnt.FileName, Len(dcmnt.FileName) - 4) Else txbPONum.Text = ""
        If dcmnt.Properties.Exists("Customer", 1) Then cbxName.Text = dcmnt.Properties("Customer", 1)
        'FORCE CALL cbxName_SelectionChanged
        ...

    Catch ex As Exception
        MessageBox.Show("Can't Retrieve Properties:" & vbLf & ex.Message)
    End Try
End Sub

I'm thinking maybe something like...
If Customers.Contains(dcmnt.Properties("Customer", 1)) then cbxName.Text = [what ever the index of the query was.]

How do I figure out what the query selection index is? or how would I complete that if statement?
Posted
Updated 8-Dec-14 8:17am
v2
Comments
[no name] 8-Dec-14 11:56am    
Which if statement are you referring to? Have you tried debugging your code to see why it does not fire? See SelectedIndex Your question is illogical since you are firing the IndexChanged Event when you select it...
Sean Donnahoe 8-Dec-14 12:06pm    
The only If statement that pertains to cbxName object.
If dcmnt.Properties.Exists("Customer", 1) Then cbxName.Text = dcmnt.Properties("Customer", 1)
The combobox (cbxName) that is referenced, gets updated from the if statement, but the DataContext for the rest of the forms bound objects does not seem to get updated.
Sean Donnahoe 8-Dec-14 12:16pm    
I'm thinking maybe something like...
If Customers.Contains(dcmnt.Properties("Customer", 1)) then cbxName.Text = [what ever the index of the query was.]

How do I figure out what the query selection index is? or how would I complete that if statement?

1 solution

Why "force call" anything? Why not just move the code from your event handler into a separate method and call that from two places?
 
Share this answer
 
Comments
Sean Donnahoe 8-Dec-14 15:37pm    
I tried that, but I always get an 'Index was out of range' error. The combobox being checked has XML data bound to it, so the If/Then statement in question needs to match an object that is referenced in the XML.
OriginalGriff 8-Dec-14 15:39pm    
So use the sender parameter to identify the combobox that raided the event.
Sean Donnahoe 8-Dec-14 15:41pm    
How do you mean?
OriginalGriff 8-Dec-14 15:55pm    
When you get an event, the sender parameter holds the class instance that raised the event. In the case of a Control event handler, it's the instance of the control that the user interacted with. All you have to do is cast it to the appropriate Control class.
Sean Donnahoe 8-Dec-14 15:58pm    
Did I mention I'm new to this? LOL. Sorry that went straight over my head. I mean, I understand about sender, but not sure how to use it out of the context of the Sub functionality.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900