|
-
Mar 27th, 2009, 04:29 PM
#1
Thread Starter
Member
[RESOLVED] ComboBox (DropDownList)
Hi everyone,
I have a 2 ComboBox with DropDownList style. 1st one collection is 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 and the other one collection is 2,3,4,5,6,7,8,9,10,11,12,13,14,15. For example when I choose "2" in 1st ComboBox, 2nd one must be change to "2" if the selected item smaller then 1st ComboBox and when I choose "6" in 2nd ComboBox first ComboBox must be only one smaller then it like "5". These are examples. My problem is everything is perfect if I choose 1 to 8 but if I choose 9 to 15 doesn't change the other ComboBox 
Here is my Code:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = 3
ComboBox2.SelectedIndex = 5
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim XxXStart, XxXEnd
XxXStart = ComboBox1.SelectedItem
XxXEnd = ComboBox2.SelectedItem
If XxXEnd < XxXStart Then
ComboBox1.SelectedIndex = ComboBox2.SelectedIndex + 1
End If
End Sub
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Dim YyYStart, YyYEnd
YyYStart = ComboBox1.SelectedItem
YyYEnd = ComboBox2.SelectedItem
If YyYStart > YyYEnd Then
ComboBox2.SelectedIndex = ComboBox1.SelectedIndex - 1
End If
End Sub
I'm confused in that code, I realy don't know how can I fix it
-
Mar 27th, 2009, 04:50 PM
#2
Re: ComboBox (DropDownList)
ComboBox items are text, so you need to convert textual values into integer values when performing some kind of calculation. When you do "If XxXEnd < XxXStart Then" your comparing two text strings not two numbers.
Without checking it, Im guessing the problem will be fixed if you convert the text values to integers like this;
Dim XxXStart, XxXEnd As Integer
XxXStart = CInt(ComboBox1.SelectedItem)
XxXEnd = CInt(ComboBox2.SelectedItem)
and
Dim YyYStart, YyYEnd As Integer
YyYStart = CInt(ComboBox1.SelectedItem)
YyYEnd = CInt(ComboBox2.SelectedItem)
You can also make your code simpler by doing this;
Code:
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ComboBox2.SelectedIndexChanged
If CInt(ComboBox2.SelectedItem) < CInt(ComboBox1.SelectedItem) Then _
ComboBox1.SelectedIndex = ComboBox2.SelectedIndex + 1
End Sub
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedValueChanged
If CInt(ComboBox1.SelectedItem) > CInt(ComboBox2.SelectedItem) Then _
ComboBox2.SelectedIndex = ComboBox1.SelectedIndex - 1
End Sub
You should also check that the new index does not go out of range.
Last edited by Bulldog; Mar 27th, 2009 at 04:56 PM.
-
Mar 27th, 2009, 04:57 PM
#3
Thread Starter
Member
Re: ComboBox (DropDownList)
Hi,
I try to change like your code but now it is not running. also 1 to 8 not working too.
Code:
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim XxXStart, XxXEnd As Integer
XxXStart = CInt(ComboBox1.SelectedItem)
XxXEnd = CInt(ComboBox2.SelectedItem)
If XxXEnd < XxXStart Then
ComboBox1.SelectedItem = ComboBox2.SelectedItem + 1
End If
End Sub
-
Mar 27th, 2009, 05:03 PM
#4
Thread Starter
Member
Re: ComboBox (DropDownList)
Oh I'm sorry It's working now! 
Thank you very much.
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
|