i have 2 combobox, one is for the start time and the other is for the end time. my problem is on how to set the time interval.. for example if user select 6:00am as start time and 6:00 am as end time it must prompt that is in invalid..…
After 39 posts, I think mitja explain more detail and easy to understand than OP. :)
Jx_Man
Senior Poster
3,534 posts since Nov 2007
Reputation Points: 1,488
Solved Threads: 524
Skill Endorsements: 64
Jx, can you understand what does he want? I think you cannot - not from this kind of explanation.
I did an example code from what I think he wants, but the problem is he wants something different every time he makes a new post.
I the last of them, there was 15 minutes included too, which havent been before.
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
Finally we agree.
Here is the solution that works - as we came to the concludion:
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
comboBox1.Items.Add("6:00 am")
End Sub
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim times As List(Of DateTime) = Data_Combo2(comboBox1.SelectedItem.ToString())
comboBox2.Text = ""
comboBox2.Items.Clear()
For Each time As DateTime In times
comboBox2.Items.Add(time.ToString("t"))
Next
End Sub
Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim time As DateTime = Data_Combo1(comboBox2.SelectedItem.ToString())
comboBox1.Text = ""
comboBox1.Items.Clear()
comboBox1.Items.Add(time.ToString("t"))
End Sub
Private Function Data_Combo1(input As String) As DateTime
Dim time As DateTime = DateTime.Parse(input, System.Globalization.CultureInfo.InvariantCulture)
Return time
End Function
Private Function Data_Combo2(input As String) As List(Of DateTime)
Dim time As DateTime = DateTime.Parse(input, System.Globalization.CultureInfo.InvariantCulture)
Dim list As New List(Of DateTime)()
Select Case time.Minute
Case 0
If True Then
list.Add(time.AddMinutes(30))
list.Add(time.AddMinutes(45))
list.Add(time.AddMinutes(50))
list.Add(time.AddMinutes(60))
Exit Select
End If
Case 30
If True Then
list.Add(time.AddMinutes(15))
list.Add(time.AddMinutes(20))
list.Add(time.AddMinutes(30))
Exit Select
End If
Case 45
If True Then
list.Add(time.AddMinutes(5))
list.Add(time.AddMinutes(15))
Exit Select
End If
Case 50
If True Then
list.Add(time.AddMinutes(10))
Exit Select
End If
End Select
Return list
End Function
End Class
Let me know how its gonna work.
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
Just remove or comment this line: comboBox1.Text = "" from comboBox2_SelectedIndexChanged events.
Change to:
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim times As List(Of DateTime) = Data_Combo2(comboBox1.SelectedItem.ToString())
comboBox2.Text = ""
comboBox2.Items.Clear()
For Each time As DateTime In times
comboBox2.Items.Add(time.ToString("t"))
Next
End Sub
Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim time As DateTime = Data_Combo1(comboBox2.SelectedItem.ToString())
'comboBox1.Text = ""; remove or comment this line
comboBox1.Items.Clear()
comboBox1.Items.Add(time.ToString("t"))
End Sub
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
now you could vote some +1.
after such a hard work :)
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
Sure, use this
Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim time As DateTime = Data_Combo1(comboBox2.SelectedItem.ToString())
comboBox1.Items.Clear()
comboBox1.Text = comboBox2.SelectedItem.ToString() 'put this line of code!
comboBox1.Items.Add(time.ToString("t"))
End Sub
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
This is exactly how my code now works. I mean exactly. When user does a selection in B, this time immediatelly appears in A (literaly, when used does a selection in B, this same value appears in A).
So this is what you want to. And thisis how my code works.
I will remove ones again what I have added in last change. So it should be like:
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim times As List(Of DateTime) = Data_Combo2(comboBox1.SelectedItem.ToString())
comboBox2.Text = ""
comboBox2.Items.Clear()
For Each time As DateTime In times
comboBox2.Items.Add(time.ToString("t"))
Next
End Sub
Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim time As DateTime = Data_Combo1(comboBox2.SelectedItem.ToString())
comboBox1.Items.Clear()
comboBox1.Items.Add(time.ToString("t"))
End Sub
READ CAREFULLY:
Now you can select A (6.00am) and then select B (7.00am).
A still has the same selected value (even if there is a new value bellow TextBox - but you cannot see it yet, since you didnt do a pick).
Next - IMPORTANT: When you click on A again, there is still 6.00am inside a textBox, but in a drop down list is now available new value - 7.00AM).
If you will select it the textBox will get new value 7.00am, if you will not select it, 6.00am will remain.
IMPORTNT: As soon as you will choose a new value from A (so 7.00am), the B will get new values.
This is it. I cannot think of a better logic.
Hope it helps,
bye
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
When you select an item from comboBox2, comboBox1 items will be cleared yes (but there will still be an item in a textBox of combobox). This is how it goes.
You wanted this kind of code.
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13