|
-
Apr 4th, 2008, 04:57 AM
#1
Thread Starter
Member
[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?
-
Apr 4th, 2008, 05:05 AM
#2
Fanatic Member
-
Apr 4th, 2008, 05:09 AM
#3
Thread Starter
Member
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
-
Apr 4th, 2008, 05:14 AM
#4
Fanatic Member
Re: More ComboBoxes :(
You could use
Combobox1.selectedindex (I believe)
So it wont matter on the text, just the selected item.
-
Apr 4th, 2008, 05:23 AM
#5
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:
Private Structure CommandItem
Public DisplayText As String
Public Command As String
Sub New(ByVal text As String, ByVal com As String)
DisplayText = text
Command = com
End Sub
Public Overrides Function ToString() As String
Return DisplayText
End Function
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:
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.
-
Apr 4th, 2008, 06:20 AM
#6
Thread Starter
Member
Re: More ComboBoxes :(
Okay, thanks for your help.
So I would need a new commanditem structure for every command?
-
Apr 4th, 2008, 06:25 AM
#7
Re: More ComboBoxes :(
 Originally Posted by Eminee
Okay, thanks for your help.
So I would need a new commanditem structure for every command?
Yeah.
-
Apr 4th, 2008, 06:46 AM
#8
Thread Starter
Member
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.
-
Apr 4th, 2008, 06:52 AM
#9
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
-
Apr 4th, 2008, 07:21 AM
#10
Thread Starter
Member
Re: More ComboBoxes :(
I'm still not getting anywhere 
So confused..
-
Apr 4th, 2008, 07:34 AM
#11
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?
-
Apr 4th, 2008, 07:36 AM
#12
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
-
Apr 4th, 2008, 07:36 AM
#13
Thread Starter
Member
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.
-
Apr 4th, 2008, 07:41 AM
#14
Thread Starter
Member
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?
-
Apr 4th, 2008, 07:56 AM
#15
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.
-
Apr 4th, 2008, 08:16 AM
#16
Thread Starter
Member
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..
-
Apr 4th, 2008, 08:57 AM
#17
Thread Starter
Member
Re: More ComboBoxes :(
Maybe the only way to do is loads and loads of IF statements?
-
Apr 4th, 2008, 10:14 AM
#18
Re: More ComboBoxes :(
 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?
-
Apr 4th, 2008, 11:27 AM
#19
Thread Starter
Member
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.
-
Apr 4th, 2008, 12:28 PM
#20
Re: More ComboBoxes :(
 Originally Posted by Eminee
... because of what I said above. 
What is that exactly? I cant find make anything out of your last posts.
-
Apr 4th, 2008, 12:33 PM
#21
Thread Starter
Member
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
-
Apr 4th, 2008, 12:41 PM
#22
Re: More ComboBoxes :(
Could you show me the code you use to populate your combobox?
-
Apr 4th, 2008, 12:48 PM
#23
Thread Starter
Member
Re: More ComboBoxes :(
 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..
-
Apr 4th, 2008, 04:23 PM
#24
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.
-
Apr 4th, 2008, 04:42 PM
#25
Thread Starter
Member
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..
-
Apr 5th, 2008, 09:49 AM
#26
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.
-
Apr 6th, 2008, 07:51 AM
#27
Thread Starter
Member
Re: More ComboBoxes :(
Okay.. I'll give this a go now.
-
Apr 6th, 2008, 08:27 AM
#28
Thread Starter
Member
Re: More ComboBoxes :(
I take it I'm going to be using the Collection.Item property to find the correct index in the collection?
-
Apr 6th, 2008, 09:23 AM
#29
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.
-
Apr 6th, 2008, 09:40 AM
#30
Thread Starter
Member
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
-
Apr 6th, 2008, 09:55 AM
#31
Re: More ComboBoxes :(
Let me check something and i'll get back to you.
-
Apr 6th, 2008, 09:56 AM
#32
Thread Starter
Member
Re: More ComboBoxes :(
Okay, thankyou
-
Apr 6th, 2008, 10:38 AM
#33
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?!
-
Apr 6th, 2008, 10:40 AM
#34
Re: More ComboBoxes :(
Any problems i'll check back in a couple of hours.(going out for coffee )
-
Apr 6th, 2008, 10:44 AM
#35
Thread Starter
Member
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.
-
Apr 6th, 2008, 04:18 PM
#36
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
-
Apr 7th, 2008, 07:17 AM
#37
Re: More ComboBoxes :(
If you solved your problem then mark it as resolved.
-
Apr 8th, 2008, 10:19 AM
#38
Thread Starter
Member
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?
-
Apr 9th, 2008, 02:39 AM
#39
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
-
Apr 9th, 2008, 02:54 AM
#40
Frenzied Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|