Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: [RESOLVED] More ComboBoxes :(

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Resolved [RESOLVED] More ComboBoxes :(

    Hey, I've posted about this problem in my previous thread but I don't think anyone can see it.
    I basically would just like to be pointed in the right direction to finding the property I need or whatever.
    What I wish to do is assign sort of like, a second string to each option on a combo box. Is this possible? Because when the user selects an option, I want to use the string that is related to the option chosen... not the text itself, if that makes sense?

  2. #2
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: More ComboBoxes :(

    Can you give an example?
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Okay. The program I need it for is to send commands to devices over the network.
    So if a user selects ON on the combo box.. I want the command for ON to be used.. which for example might be something like "POWR%1#"... so its not plain english it needs to be understandable for people who don't know the commands. So I can't use combobox.text because it would just use "ON".. you see?

    Thanks for replying

  4. #4
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: More ComboBoxes :(

    You could use

    Combobox1.selectedindex (I believe)

    So it wont matter on the text, just the selected item.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: More ComboBoxes :(

    I assume you're adding strings to the combobox. Strings can, obviously, only hold 1 string value, so they wont do you any good.

    Remember that a ComboBox can hold any type of Object, so you could create your own structure like this:

    VB.NET Code:
    1. Private Structure CommandItem
    2.         Public DisplayText As String
    3.         Public Command As String
    4.  
    5.         Sub New(ByVal text As String, ByVal com As String)
    6.             DisplayText = text
    7.             Command = com
    8.         End Sub
    9.  
    10.         Public Overrides Function ToString() As String
    11.             Return DisplayText
    12.         End Function
    13.     End Structure
    As you can see, it holds two members: DisplayText and Command.
    I guess you can figure out what they are intended to hold?
    Add items like this:
    VB.NET Code:
    1. ComboBox1.Items.Add(New CommandItem("ThisIsMyItem", "ThisIsMyCommand"))

    The ComboBox' SelectedItem property will return CommandItems, so you can easily retrieve the Command from the selected item.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Okay, thanks for your help.
    So I would need a new commanditem structure for every command?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: More ComboBoxes :(

    Quote Originally Posted by Eminee
    Okay, thanks for your help.
    So I would need a new commanditem structure for every command?
    Yeah.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Righto.
    So why would I need to add items with
    ComboBox1.Items.Add(New CommandItem("ThisIsMyItem", "ThisIsMyCommand"))
    If I have the structure... would it not be something like
    ComboBox1.Items.Add(New CommandItem(DisplayText, Command))
    ??
    I'm a bit confused :S Sorry.

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Hi.
    You can also have a look here. It's for a datagridview but i think it can be used for your needs also.

    http://www.vbforums.com/showthread.php?t=475854

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    I'm still not getting anywhere
    So confused..

  11. #11
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Ok.
    Here i'll make it easier.
    Code:
       Private Sub addcombo()
            Dim myDivisions As New ArrayList
            With myDivisions
                .Add(New StructureGeneralComboHelper("HELLO", 2))
                .Add(New StructureGeneralComboHelper("HELLO1", 2))
                .Add(New StructureGeneralComboHelper("TEST", 1))
                .Add(New StructureGeneralComboHelper("HELLO3", 2))
                .Add(New StructureGeneralComboHelper("TEST1", 1))
                .Add(New StructureGeneralComboHelper("SOMETHINGELSE", 0))
                .
            End With
            Me.ComboBox1.Sorted = False
            Me.ComboBox1.SuspendLayout()
            With Me.ComboBox1
                .DataSource = myDivisions
                .DisplayMember = "Name"
                .ValueMember = "Id"
            End With
            Me.ComboBox1.ResumeLayout()
        End Sub
    Now it's easy,non? You just add your string and a code id and put them in the combobox.
    ok?

  12. #12
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Sorry i forgot the structure.
    Here:

    Code:
     Public Structure StructureGeneralComboHelper
            Private divName As String
            Private divID As Integer
    
            Public Sub New(ByVal name As String, ByVal id As Integer)
                divName = name
                divID = id
            End Sub
    
            Public ReadOnly Property Name() As String
                Get
                    Return divName
                End Get
            End Property
    
            Public ReadOnly Property Id() As Integer
                Get
                    Return divID
                End Get
            End Property
        End Structure

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    But the thing is.. I don't need a code id. I need two strings? Can you do that too?
    Last edited by Eminee; Apr 4th, 2008 at 07:45 AM.

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    I think because of my code it will make this a bit more tricky to implement.
    See.. I already have two combo boxes. The first one has a list of devices and when you select a device the second combobox displays a list of commands that are used for that device. So you see the second combobox is already populated by the first combo box.
    Code:
       Public lists As New Dictionary(Of String, String())
    Code:
            Me.lists.Add("Panasonic Projector", New String() {"On", "Off"})
            Me.lists.Add("Other Projector", New String() {"On", "Off"})
            Me.lists.Add("Other", New String() {"Choice1", "Choice2"})
    
    
            For Each key As String In Me.lists.Keys
                Me.comDevice.Items.Add(key)
            Next key
    Code:
       Private Sub comDevice_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comDevice.SelectedIndexChanged
            Me.comButton1.DataSource = Me.lists(CStr(Me.comDevice.SelectedItem))
        End Sub
    Thats the code I'm using.
    Is there a way to make a structure that will have a device, with command options which each have an option of their own?

  15. #15
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    ????
    But all you need to do is add the items to the second combo.
    P.E.
    Panasonic project. Ok you select it and you do a combo add item "on" and another combo add item "off".
    You don't need a structure or anything. You just erase the previous items on your combo2 and add the items.
    All you have to do is have an arraylist or dictionary or something that will have the combo options.
    Panasonic -- on
    Panasonic - Off
    Other -- choice1
    Other -- choice 2
    etc.
    So when the combo gets "Panasonic" you will search for the exact string (you can use my structure to have the first combo to have a codeid so you can search for the id but if it's confising, scr#w it)
    and you will get the options of panasonic (on,off) then you will add them to the second combo.

    P.S. actually this is very primitive. You can use an xml file or access db to store your values, or even a txt file. But if it's a simple app with few data inputs then do it this way.

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    I have that all working already. I just need to add it so that I have
    Panasonic - On - "CONFUSINGCOMMANDFORON"
    Panasonic - Off - "CONFUSINGCOMMANDFOROFF"
    I already have the second ComboBox sorted... apart from the second values..

  17. #17

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Maybe the only way to do is loads and loads of IF statements?

  18. #18
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: More ComboBoxes :(

    Quote Originally Posted by Eminee
    Righto.
    So why would I need to add items with
    ComboBox1.Items.Add(New CommandItem("ThisIsMyItem", "ThisIsMyCommand"))
    If I have the structure... would it not be something like
    ComboBox1.Items.Add(New CommandItem(DisplayText, Command))
    ??
    I'm a bit confused :S Sorry.
    What do you mean by "why"? The structures constructor takes two arguments; both strings. The first string is the string that will be displayed in the ComboBox, the second is the "command" associated with the item.
    Is this not working for you?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  19. #19

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Yeah.. sorry I was having a blonde moment there. I understand the code you gave me now.
    I just can't seem to implement it with the code I already have, because of what I said above.

  20. #20
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: More ComboBoxes :(

    Quote Originally Posted by Eminee
    ... because of what I said above.
    What is that exactly? I cant find make anything out of your last posts.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  21. #21

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Well the code you gave me populates the combobox.. but I have already populated the combo box in my code through another combo box.
    So I have a device combo box... which defines which options are displayed in the second combo box which is the command one. I define the commands when I define the devices.
    So the when I use your code.. its just going to create another "ON" option... instead of adding the command to the existing one.
    Does this make sense? :S

  22. #22
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: More ComboBoxes :(

    Could you show me the code you use to populate your combobox?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  23. #23

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Quote Originally Posted by Eminee
    Code:
       Public lists As New Dictionary(Of String, String())
    Code:
            Me.lists.Add("Panasonic Projector", New String() {"On", "Off"})
            Me.lists.Add("Other Projector", New String() {"On", "Off"})
            Me.lists.Add("Other", New String() {"Choice1", "Choice2"})
    
    
            For Each key As String In Me.lists.Keys
                Me.comDevice.Items.Add(key)
            Next key
    Code:
       Private Sub comDevice_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comDevice.SelectedIndexChanged
            Me.comButton1.DataSource = Me.lists(CStr(Me.comDevice.SelectedItem))
        End Sub

    This code..

  24. #24
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Yes well now i'm also confused.
    What are you trying to do. Please clarify it.
    You say you have the panasonic etc values in combo1
    You have the on-off etc values in combo2.
    So?!
    If you don't want to have multiple commands when you re visit the combobox, just search the items to find a match. Also if what i understand is correct, you want all the command to be added to the 2nd combobox?! So you have on,off,choice1,choice2 etc?! If so, you can do what i told you and erase the combo every time. Noone will understand the difference. Or if don't want to the populate a arraylist or the dictionary you have and take the values from there.
    But other than that i can't think of what troubles you.

  25. #25

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Yes, I have what you've said. But what I want is for the second box options to have a second value (I think thats what you'd call it).. so that when the panasonics "On" option, for example, is selected.. the string "%POWR 1" is used to send to the device to turn it on. I obviously don't want to put "%POWR 1" in the combo box because users won't understand what it does.

    So.. the reason this is a problem is that I've already populated the second drop down box from the first one... so I can't then go and use Atheist's CommandItem code since it will just add another "ON" option.. instead of adding "%POWR 1" to the one which has already been created.

    Is that any clearer? :S
    Sorry I'm not being very clear.. I'm not good at explaining stuff. But thanks for both your help..

  26. #26
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(


    Ok so you have your first combobox.
    Assuming that you understand both my code and Atheist code and you can add 2 values into your combo, you do this:

    When you first add the p.e. panasonic value you also add another value to your FIRST combo. Then when you select your panasonic value, you have already a collection that it has 3 elements, The 2nd value of the first combo and the 2 values you want to add to the second combo (on,#$rtffxxsomething) so you have to write some code in order to read the 2nd value from the first combobox and then find it on your collection. After that you add the 2nd(ON) and 3rd(!@#something) value to your 2nd combo (you can use the arraylist example i gave you). OK?

    If you are still confused....Here is a map.

    1st combo ----> Panasonic, 1
    -----> Siemens,2
    etc
    collection
    ---->1,on,@#$asaz
    ---->1,off,%$%sda
    ---->2,choice1,$%%$44
    etc.

    arraylist ---->on,@#$asaz
    ---->off,%$%sda
    ---->choice1,$%%$44
    etc.

    So in combo2 you get the arraylist values you want and that's it.

  27. #27

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Okay.. I'll give this a go now.

  28. #28

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    I take it I'm going to be using the Collection.Item property to find the correct index in the collection?

  29. #29
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Yes most probably, if you have problems let me know. Although if i were at your shoes i would have used a dataset with 3 columns. But since you are trying this way. Go for it.

  30. #30

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Okay... I tried it with a collection.. and it didn't like it because of the before and after properties? You can't have both?

    Can all three of them be arraylists with their own structures? Thats how I've done it so far.
    I'm guessing then I need a for each statement? So like

    For each comDevice.SelectedItem in lists
    blahblahblah

    ?

    Except that doesn't work because the selecteditem is going to be Panasonic and not 1

    lol

  31. #31
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Let me check something and i'll get back to you.

  32. #32

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Okay, thankyou

  33. #33
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Sorry, i was on an ebay auction.
    So.
    In order to get the values and let's get wild and add 3 values so you can have multiple options, you must rework on the structure.

    Code:
     Public Structure StructureGeneralComboHelper
            Private divName As String
            Private divName2 As String
            Private divID As Integer
    
            Public Sub New(ByVal name As String, ByVal name2 As String, ByVal id As Integer)
                divName = name
                divName2 = name2
                divID = id
            End Sub
    
            Public ReadOnly Property Name() As String
                Get
                    Return divName
                End Get
            End Property
    
            Public ReadOnly Property Id() As Integer
                Get
                    Return divID
                End Get
              
            End Property
            Public ReadOnly Property name2() As String
                Get
                    Return divName2
                End Get
            End Property
        End Structure

    So you can get 3 values. But how to get them?!
    Well here is how:
    Code:
      Dim myDivisions As New ArrayList
           
            With myDivisions
                .Add(New StructureGeneralComboHelper("ON", "$%F", 2))
                .Add(New StructureGeneralComboHelper("OFF", "%^3d", 2))
                .Add(New StructureGeneralComboHelper("Check1", "%77", 1))
                .Add(New StructureGeneralComboHelper("check2", "%%g", 1))
                .Add(New StructureGeneralComboHelper("SOMETHINGELSE", "%#f", 0))
    
            End With
            Dim sg As New StructureGeneralComboHelper
            For Each sg In myDivisions
                If sg.Id = "2" Then
                    ' Found my Panasonic  value! Add it to the combo2 ! 
    '
    ' code for adding...
    '
                End If
            Next
    Of course in the place of "2" you will have the value that you get from the first combo so you can add.
    I think now it's easy?!

  34. #34
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Any problems i'll check back in a couple of hours.(going out for coffee )

  35. #35

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Yes.. I'd worked out that bit already.

    I was just wondering

    1. How you'd go about adding just one or two items of the array list to the combobox?
    2. How to refer to the ID of the selecteditem in the combo box... since it won't let me put comDevice.SelectedItem.Id?
    3. Why do I need the third array?

    I should start paying you by the hour :P
    I'm sorry >.<
    Last edited by Eminee; Apr 6th, 2008 at 11:04 AM.

  36. #36
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Hi.
    Your questions.

    3) you don't need it. I put it to explain further. You need a 2 item arraylist.
    1) You add the items to the combo with the code i gave you to posts #11 and #12
    2)Ok your're right. How to you refer to the id's.
    Put this code into the combobox selectedindexchanged event.

    Code:
      Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
         Dim s As String = Me.ComboBox1.Items.Item(Me.ComboBox1.SelectedIndex).Name
        Dim v As Integer = Me.ComboBox1.Items.Item(Me.ComboBox1.SelectedIndex).Id
    
    
        End Sub
    Where id and name are the variables you use on the structure.
    Also in the .id variable you probably would want to make it string and put the "%$@43" variables.



    Don't worry paying me, i'm rich ..Just kidding, although some reputation won't be bad

  37. #37
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    If you solved your problem then mark it as resolved.

  38. #38

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: More ComboBoxes :(

    Thankyou!
    I have it all working now except the values are being added to the drop down box multiple times. Not sure why... must be something to do with my for loop?

  39. #39
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: More ComboBoxes :(

    Yes thats probably your LOOP.
    If you don't find a solution then post the loop code.
    But it's better to grind yourself a little to get some experience

  40. #40
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: More ComboBoxes :(

    I find this easier, and simpler..
    (Replace cbComboBox with the name of your combo box)
    Code:
    Public sList As New List(Of String)
    Public sSplitChar As String = "~"
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        sList.Add("value1" & sSplitChar & "value2")
        sList.Add("command" & sSplitChar & "name")
        cbComboBox.AddRange(sList.ToArray)
    End Sub
    
    Private Sub cbComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbComboBox.SelectedIndexChanged
        Dim iID As Integer = cbComboBox.SelectedIndex
        Dim sValue1 As String = sList(iID).Split(sSplitChar)(0) '(1) would be the second part
    End Sub
    That's fairly simple..

    Cheers
    Icyculyr

Page 1 of 2 12 LastLast

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