Results 1 to 9 of 9

Thread: Result from Combobox as different Variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    29

    Result from Combobox as different Variable

    Hi,

    I have the following Combobox
    Name:  ScreenHunter_167 Dec. 29 17.22.jpg
Views: 152
Size:  21.1 KB

    The User can select a value and it works fine.
    But when I write the file, this value needs to be changed so I wrote the following code:

    Code:
        Private Sub EingabePosition_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EingabePosition.SelectedIndexChanged
            If EingabePosition.SelectedItem.ToString = "Center" Then
                Position = 1
            End If
            If EingabePosition.SelectedItem.ToString = "Linker Stürmer" Then
                Position = 2
            End If
            If EingabePosition.SelectedItem.ToString = "Rechter Stürmer" Then
                Position = 3
            End If
            If EingabePosition.SelectedItem.ToString = "Verteidiger" Then
                Position = 4
            End If
            If EingabePosition.SelectedItem.ToString = "Torhüter" Then
                Position = 5
            End If
        End Sub
    It does the trick but I was looking for an easier solution.
    Is there a better and shorter way to do this?

    Thanks

    Wasserlasser

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: Result from Combobox as different Variable

    If you can guarantee the items' order will not change, you can get the position using the combo box's selected index value
    Code:
    If EingabePosition.SelectedItem IsNot Nothing Then
                Position = EingabePosition.SelectedIndex-1
            End If
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,254

    Re: Result from Combobox as different Variable

    Quote Originally Posted by kebo View Post
    If you can guarantee the items' order will not change, you can get the position using the combo box's selected index value
    Code:
    If EingabePosition.SelectedItem IsNot Nothing Then
                Position = EingabePosition.SelectedIndex-1
            End If
    Shouldn't that be .SelectedIndex + 1? (top item index = 0.)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    29

    Re: Result from Combobox as different Variable

    Hi guys,

    thanks for the help. It was indeed +1 (I tested both options )

    So this solution will make it work in a way that we go always in steps by one, correct?
    Is there a way to give it a specific number?

    I have another box where the dropdown has values but line 1 would be value 12, line 2 would be value 25 etc.

    Thanks

    Wasserlasser

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: Result from Combobox as different Variable

    yea, my bad
    Good morning sleepy head
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Result from Combobox as different Variable

    If you pass in a DataTable as the ComboBox's .DataSource, you can set the DisplayMember to what you want the Combobox to display (eg "Center", "Linker Stürmer", etc.) and set the ValueMember to a different column that contains what each item in the DisplayMember column corresponds to. Then you'd look for the ComboBox's SelectedValue.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    29

    Re: Result from Combobox as different Variable

    So you mean use this option?
    Name:  ScreenHunter_167 Dec. 29 20.47.jpg
Views: 50
Size:  22.6 KB

    I do have an Excel sheet with all my values which looks like this

    Land Citizenship Country

    Alaska 83 82
    Alberta 0 1
    Arizona 1 93
    Australien 93 87
    Belgien 3 82
    Brasilien 82 4
    British Columbia 4 5
    China 85 8
    Colorado 8 9
    Connecticut 9 12
    DC 7 14
    Delaware 14 15
    Deutschland 21 22
    Dänemark 15 80
    England 80 16
    Estland 16 84

    How can I add the Excel sheet as the Datasource or is that not possible?

    Thanks

    Wasserlasser

  8. #8
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Result from Combobox as different Variable

    I was thinking less of using a wizard as they tend to confuse me more and I find it easier to know what is going on when I set up the data connections myself.... But to your main question, yes you can query the Excel spreadsheet like it is a database, fill a local DataTable object with the data, and set that as the ComboBox's DataSource. See here for a thread on that exact topic. Just change the specifics for your own use. Also I noticed that KevinInstructor didn't include the "ComboBox1.ValueMember" but that would be set up in the same way as the .DisplayMember (I like to set up the .DisplayMember, followed by the .ValueMember, and finally the .DataSource in that order). The nice thing about setting a DataTable as the DataSource is that you can actually use all of the data in every column, not just the DisplayMember and ValueMember columns... You'd just use the following to get at the data in a particular field:
    Code:
    ' Just change the italicized code to your own project
    myData = DirectCast(combBox.SelectedItem, DataRowView).Item("columnName").ToString
    Last edited by Pyth007; Dec 29th, 2014 at 05:13 PM. Reason: Added about using ValueMember

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    29

    Re: Result from Combobox as different Variable

    I will see what I can do with this :-)

    Thanks

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