Combo box functionality - vb.net

I have 4 combobox in my visual basic;
ComboBox1 has 3 items: Vehicle, Motorbikes, None
ComboBox2 has 4 items: sportbike, casual bikes and sportcar,casual cars
ComboBox3
ComboBox4
I KINDLY need a code that will let me do the following:
Make combobox 2,3,4 invisible until I make a selection on combobox 1 i.e. I will choose vehicle and later progress to select sportscar meanwhile combobox 3 and 4 are invisible. In short, the next combo box only appears after making a selection on the previous one.
On combobox 1, if "none" is selected the other 2,3,4 remains invincible .

On the forms Load event you need to set combo boxes 2,3 and 4 to visible=False. Then on the ComboBox1_SelectedIndexChanged event you can change combobox2 to visible=True and then do the same for each progression. The code is shown below. You will also need to decide how you want to reset previous box. In other words do you want to comboboxes 2, 3, and 4 to once again become invisible if you change from Motorbikes to none in combobox1?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox2.Visible = False
ComboBox3.Visible = False
ComboBox4.Visible = False
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem="None" then
ComboBox2.Visible=False
ComboBox3.Visible=False
ComboBox4.Visible=False
Else
ComboBox2.Visible = True
End If
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox3.Visible = True
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox4.Visible = True
End Sub
End Class

In your designer file, set the Visible property of 2, 3, and 4 to false.
Then in your code file use the following code:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim showComboBox = Not String.IsNullOrWhitespace(ComboBox1.Text) AndAlso ComboBox1.Text <> "None"
ComboBox2.Visible = showComboBox
ComboBox3.Visible = showComboBox
ComboBox4.Visible = showComboBox
End Sub
What this does is set a Boolean variable equal to if the Text of ComboBox1 is not an empty string and not "None". It then sets the Visible property of the other ComboBox controls to the result.

Related

Is there any recommended code to show a specific value in a separate label if any three checkboxes out of six are selected in Vb?

I have created 6 checkboxes in Vb and I want to code that if any three of them are checked then a certain value will be shown in a separate label.
I'd build an Array of your CheckBoxes and wire them up to a common CheckChanged() handler. Then you can use a simple LINQ query to count the number of them that are checked.
Something like:
Public Class Form1
Private CheckBoxes() As CheckBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
' change the below to the names of your six checkboxes:
CheckBoxes = {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5, CheckBox6}
For Each cb In CheckBoxes
AddHandler cb.CheckedChanged, AddressOf CheckBoxes_CheckedChanged
Next
Label1.Text = "Select at least three checkboxes."
End Sub
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs)
If CheckBoxes.Where(Function(cb) cb.Checked).Count >= 3 Then
Label1.Text = "Thank you!"
Else
Label1.Text = "Select at least three checkboxes."
End If
End Sub
End Class

how can i show specific values in the combobox based on the checkbox state

I want to show related values in the combo box when the user selects the checkboxes.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If CheckBox1.Checked = True Then
ComboBox3.GetItemText("10")
End If
If CheckBox2.Checked = True Then
ComboBox3.Items.Add("20")
End If
End Sub
Any code suggestion?
On the CheckedChanged event, you may use the following code:
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
If sender.Text = "Select 10" Then
ComboBox1.SelectedItem = "10"
Else
ComboBox1.SelectedItem = "20"
End If
End Sub
This is coded only for two CheckBoxes. The ComboBox1.SelectedItem tries to find if there's an option given in the string available in the ComboBox control. An example output is given:
Hope it helps!

Changing a Combobox selected Item

Here's what I need to do:
I have a ComboBox that has a whole list of items, for this example lets just call them 1, 2, 3.
If someone selects 3 I want to reset the ComboBox.
So if I was to select 3, the ComboBox would then return to its default blank state.
I just want to make this clear that I don't want the actual ComboBox to reset, that is, I don't want to remove the items I have listed in it. I just want the selection to go blank again.
As LarsTech said, set the SelectedIndex = -1. Here is a complete example with just a ComboBox on the form.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Enumerable.Range(1, 10).ToList()
ComboBox1.SelectedIndex = -1
End Sub
Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
If CInt(Me.ComboBox1.SelectedValue) = 3 Then
ComboBox1.SelectedIndex = -1
End If
End Sub

Multiple selection in datagridview

I work on a new project that need a multiple row selection/deselection by the user in a datagridview with only a tap on a touch screen.
The form should look like this:
For exemple, if the user want to delete row 2 and 5, he only need to tap once on each line to select/deselect them. After the selection is done, he tap on "Delete Row" button.
I've already try to play with the CellClick event without success!!
Can someone have a clue how can I handle this problem?
After setting MultiSelect property to True and SelectionMode to FullRowSelect you can use a List to store which row of your DataGridView is selected.
On CellClick you can add/remove rows from your List, on RowPostPaint you can select a row if it's included in the List and on RowsRemoved you have to clear the List.
Private intSelectedRows As New List(Of Integer)
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With CType(sender, DataGridView)
Dim intRow As Integer = .CurrentRow.Index
If Not Me.intSelectedRows.Contains(intRow) Then
Me.intSelectedRows.Add(intRow)
Else
.CurrentRow.Selected = False
Me.intSelectedRows.Remove(intRow)
End If
End With
End Sub
Private Sub DataGridView1_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If Me.intSelectedRows.Contains(e.RowIndex) Then
CType(sender, DataGridView).Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
Me.intSelectedRows.Clear()
End Sub
If you want to clear selection you can use this code:
Private Sub btnClearSelectedRows_Click(sender As System.Object, e As System.EventArgs) Handles btnClearSelectedRows.Click
For Each intSelectedRow As Integer In Me.intSelectedRows
Me.DataGridView1.Rows(intSelectedRow).Selected = False
Next intSelectedRow
Me.intSelectedRows.Clear()
End Sub

Select item from ComboBox to open web links on WebBrowser?

i've been looking around on how to make a combobox list choice to access a webpage on webbrowser. For example, if i choose the first item in the combobox wich is named "Google" then i would press on the button next to it to access google on the webbrowser.
I got this code but it doesn't work, once i choose the first option, nothing happens.
If ComboBox1.SelectedIndex = 1 Then
WebBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl")
End If
I seems so close, but i have no clues why it doesn't work..
Try this...
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case ComboBox1.SelectedItem
Case "Please Select"
MsgBox("ERROR - No selection made in dropdown box!")
Case "Google"
WebBrowser1.Navigate("www.google.com")
Case "Microsoft"
WebBrowser1.Navigate("www.microsoft.com")
Case "Stack Overflow"
WebBrowser1.Navigate("www.stackoverflow.com")
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
ComboBox1.Items.Add("Please Select")
ComboBox1.Items.Add("Google")
ComboBox1.Items.Add("Microsoft")
ComboBox1.Items.Add("Stack Overflow")
ComboBox1.SelectedIndex = 0
'
End Sub
Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
ProgressBar1.Visible = True
With ProgressBar1
.Minimum = 0
.Maximum = 50
.Step = 5
End With
For index As Integer = 0 To 50 Step 5
ProgressBar1.Value = index
System.Threading.Thread.Sleep(35)
Next
End Sub
End Class
Is the item that's selected first? The index is 0-based. Meaning the first item in the list is index #0. Try it with selectedindex = 0.