PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Combobox Selection-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Combobox Selection

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    471

    Resolved [RESOLVED] Combobox Selection

    I have a bound combobox that I use to select, by name, and employee from a table of employees. In addition to assign the name to variable (as well as a control on the table), I also want to Assign the value of another column to another variable.

    I had thought that this would be easy and did this:

    Code:
        Private Sub cmbManager_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cmbManager.SelectionChangeCommitted
            glbstrFullName = CStr(CType(Me.tblEmployeeBindingSource.Current, DataRowView).Item("chrFullName"))
            glbintEmployeeID = CInt(CType(Me.tblEmployeeBindingSource.Current, DataRowView).Item("intEmployeeID"))
        End Sub
    The problem is that when I execute the event routine, I have to select from the combobox twice to get the values I want.

    I am sure that there are much better ways to do this, this was something off the top of my head and has the usual outcome of that.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,449

    Re: Combobox Selection

    When talking about a "bound combobox" you have to be clearer. The comboBox control can be bound to BindingsourceA and the combobox list items will be bound to BindingsourceB. So what is tblEmployeeBindingsource used for?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    471

    Re: Combobox Selection

    Not a problem. The combobox is bound to a table in my dataset...… Just did some looking and I never set the datasource, display member, and value member properties. I did that and still have the same problem of having to select twice to get the values I want. Pf course I also have a question that comes from that.

    1. If I had not set the above mentioned properties for the combobox then why was I getting the listed column values when I clicked and selected from the combobox. The obvious answer is that I set those manually somewhere and just cannot find it.

    In any case, the combobox functions such that the list is a column of names and when one of the names is selected the value of that selection is (on update) saved to the appropriate column in the table that is bound to most of the controls on the form.

    So what I was after was to make the selection from the combobox and it was my na´ve thought that once that was done I could just assign values of a different column from the same row. Apparently, I can do that, but it has to be done twice to make it happen.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,718

    Re: Combobox Selection

    From what I can gather, what should be the case is that the cmbManager control's properties should set like this, either in code or in the designer:
    vb.net Code:
    1. cmbManager.DisplayMember = "chrFullName"
    2. cmbManager.ValueMember = "intEmployeeID"
    3. cmbManager.DataSource = tblEmployeeBindingSource
    If so then you don't need to get getting a DataRowView via the BindingSource. Your code should be something like this:
    vb.net Code:
    1. glbstrFullName = CStr(cmbManager.Text)
    2. glbintEmployeeID = CInt(cmbManager.SelectedValue)
    As far as having to make the selection twice, it's not 100% clear what you mean but I'm going to make an educated guess. In your code, you are handling the SelectionChangeCommitted event, which is specifically intended to detect changes made by the user via the UI. If the first selection that you're talking about is not made by the user then it's not going to cause the SelectionChangeCommitted event to be raised. If you want to react to every change, whether made by the user or not, then you need to handle the SelectedIndexChanged event instead. If that is indeed the problem then the solution was reading the documentation for the SelectionChangeCommitted event. If that isn't the problem then I think that you'll need to provide more details about exactly how the control is bound and what steps are performed to produce the unexpected behaviour.

    Based on this:
    So what I was after was to make the selection from the combobox and it was my na´ve thought that once that was done I could just assign values of a different column from the same row.
    It sounds like you should just be using more data-binding. You can bind the same list to your ComboBox and other controls so that making a selection in the ComboBox displays other properties/columns of the same item in those other controls. For example, to display the Name in a ComboBox and associated Description in a TextBox:
    vb.net Code:
    1. With nameComboBox
    2.     .DisplayMember = "Name"
    3.     .ValueMember = "Id"
    4.     .DataSource = BindingSource1
    5. End With
    6.  
    7. descriptionTextBox.DataBindings.Add("Text", BindingSource1, "Description")
    Just as with the ComboBox, you can bind TextBoxes and other controls in the designer as well as in code. Note that, if you drag a table from the Data Sources window onto a form, you can specify in the Data Sources window whether it should generate a DataGridView or individual controls. In the latter case, TextBoxes will be generated by default in most cases and they will be automatically bound in the way I've shown.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    471

    Re: Combobox Selection

    From what I can gather, what should be the case is that the cmbManager control's properties should set like this
    They are. It took me awhile to find them, but I found the routine I used to set those properties.
    If so then you don't need to get getting a DataRowView via the BindingSource. Your code should be something like this:
    What I ended up with was similar. It was this:

    Code:
        Private Sub cmbManager_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles cmbManager.SelectedIndexChanged
            Dim intRow As Integer = cmbManager.SelectedIndex
            glbstrFullName = _MasterBase_1_1DataSet.tblEmployee(intRow).chrFullName
            glbintEmployeeID = _MasterBase_1_1DataSet.tblEmployee(intRow).intEmployeeID
        End Sub
    So at this point, it works and provides me with the two row values I wanted to assign to variables. However, I am getting the following error

    Name:  Error.jpg
Views: 93
Size:  24.5 KB

    At this location in the dataset designer (at the return line):

    Code:
            Public Default ReadOnly Property Item(ByVal index As Integer) As tblEmployeeRow
                Get
                    Return CType(Me.Rows(index),tblEmployeeRow)
                End Get
            End Property
    So now my question is, why am I looking for a row at index = -1?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,718

    Re: Combobox Selection

    Quote Originally Posted by gwboolean View Post
    So now my question is, why am I looking for a row at index = -1?
    Because there's no item selected in the control. When SelectedItem is Nothing, SelectedIndex is -1. The obvious solution is an If statement, so that you don't try use a selection when there isn't one.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    471

    Re: Combobox Selection

    I understand all of that. What I don't understand is why the event is run when I run the Load Event and again when I run the Close Event. I would have thought that the event was only run when I made a selection from the combobox.

    The obvious solution is an If statement
    Possibly it is obvious. However since I am still thinking about why the this event routine is run, when I believe it shouldn't be, and hadn't thought about that yet. No guarantee that it would have been obvious to me. Anyway, thanks.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,718

    Re: Combobox Selection

    Quote Originally Posted by gwboolean View Post
    I would have thought that the event was only run when I made a selection from the combobox.
    Still relying on intuition instead of reading the documentation? From MSDN:
    Occurs when the SelectedIndex property has changed.
    The whole point of there being a SelectionChangeCommited event is that SelectedIndexChanged is raised ANY and EVERY time the SelectedIndex property value changes, regardless of the reason. If you want to only react to changes made by the user through the UI then you handle SelectionChangeCommitted. I don't know what you're doing in your Load event handler but, whatever it is, it's causing the SelectedIndex property to change, thus the event is raised. When the form closes, part of the process is to clear the DataSource of the control. If an item was selected and all items are removed then that means the selection is being cleared, which means that the SelectedIndex property will change from something else to -1, which will raise the SelectedIndexChanged event.

    If you add items to a ComboBox manually, no item will be selected by default so the SelectedIndexChanged event will not be raised. If you bind data to the ComboBox, i.e. set its DataSource property, then the first item in the bound list will be selected by default. That WILL raise the SelectedIndexChanged event because the SelectedIndex will change from -1 to 0. If you don't want an item selected by default when the user sees the form then you need to add code to unselect that first item, which is done by setting the SelectedItem or SelectedValue to Nothing or the SelectedIndex to -1. That will change the SelectedIndex again and thus raise the SelectedIndexChanged event again. None of this will raise the SelectionChangeCommitted event but, if you allow the first item to be selected by default then an item is selected and the SelectionChangeCommitted event will not have notified you of it, which may have been your problem originally.

    You need to understand how the control works as far as selecting items and raising events, then handle the more appropriate of the two events and then add the appropriate code to do what you need done. It appears that you haven't got that combination right yet. I can't tell you what the specific combination should be because I don't know exactly what you're trying to achieve.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width