CheckBox Data to ListBox (VBNet) - vb.net

the problem with my code is that when i initially select a particular checkbox it works well and displays selected item in listbox, but when selected again it creates another entry within the listbox and when i remove it i have to uncheck the same checkbox the times it has been displayed in the listbox when i click the btnSubmit.
Can someone tell me what's wrong with the code, thank you so much
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If CheckBox1.Checked Then
ListBox1.Items.Add(CheckBox1.Text)
Else
ListBox1.Items.Remove(CheckBox1.Text)
End If
If CheckBox2.Checked Then
ListBox1.Items.Add(CheckBox2.Text)
Else
ListBox1.Items.Remove(CheckBox2.Text)
End If
If CheckBox3.Checked Then
ListBox1.Items.Add(CheckBox3.Text)
Else
ListBox1.Items.Remove(CheckBox3.Text)
End If
If CheckBox4.Checked Then
ListBox1.Items.Add(CheckBox4.Text)
Else
ListBox1.Items.Remove(CheckBox4.Text)
End If
If CheckBox5.Checked Then
ListBox1.Items.Add(CheckBox5.Text)
Else
ListBox1.Items.Remove(CheckBox5.Text)
End If
If CheckBox6.Checked Then
ListBox1.Items.Add(CheckBox6.Text)
Else
ListBox1.Items.Remove(CheckBox6.Text)
End If
End Sub

You can do it like this:
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
ListBox1.Items.Clear
CheckObj(CheckBox1)
CheckObj(CheckBox2)
CheckObj(CheckBox3)
CheckObj(CheckBox4)
CheckObj(CheckBox5)
CheckObj(CheckBox6)
End Sub
Sub CheckObj (obj as Checkbox)
if obj.checked then ListBox1.Items.Add (obj.text)
End Sub

Related

pls help me fix it. I need a way that I have to use an else to press a button that will move me to another form

I need a way that I have to use an else to press a button that will move me to another form (Sorry for my bad English ^-^)
Else
Form3.Show()
Me.Hide()
Exit Sub
End Sub
You don't need an Exit Sub directly above an End Sub.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If 1 <> 1 Then
'never True
Else
Me.Hide()
Form3.Show()
End If
End Sub

Check for certain text in multiple TexBoxes

I have a prank antivirus program I'm making for a friend. Part of the software requires "activation" to remove the "virus". I have 4 TextBoxes when I click the button I want all 4 TexBoxes to be checked for the text "0000". When I have one TextBox it works great, but I need all 4 boxes to get checked before the message box appears. I hope this makes sense. See image here
[Edit] I'm going to mention i'm a total noob at programming.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "0000" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
There are many ways to do what you want. Here's a very simple one which you can build on:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' check all the TextBoxes in the array. Return if one isn't valid
For Each textbox As TextBox In {TextBox1, TextBox2, TextBox3, TextBox4}
If textbox.Text <> "0000" Then
Return
End If
Next
' If all TextBox contains the valid string, this will appear
MsgBox("Registered")
Me.Hide()
End Sub
Have fun!
EDIT:
To have 4 different strings: just chain 4 checks. Like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' If all TextBox contains the valid string, this will appear
If TextBox1.Text = "0000" AndAlso TextBox2.Text = "1111" AndAlso TextBox3.Text = "2222" AndAlso TextBox4.Text = "3333" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub

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!

DataGridView deleting row save

my problem is that i have datagridview, and i select row i click delete button but when i refresh it is back
Private Sub btnDlt_Click(sender As Object, e As EventArgs) Handles btnDlt.Click
For Each row As DataGridViewRow In dgwData.SelectedRows
dgwData.Rows.Remove(row)
SQL.DBDA.Update(SQL.DBDT)
Next
End Sub
and my upadte is working good, it is saving everything, here is code
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
If MsgBox("Nazaj si prajete uložiť tieto zmeny?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
SQL.DBDA.Update(SQL.DBDT)
loadGrid()
btnUpdate.Enabled = False
If SQL.hasException() Then
Exit Sub
End If
Else
btnUpdate.Enabled = False
loadGrid()
End If
End Sub
The problem should be with your datasource, check your delete query if it work fine.

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.