Visual Basic Dynamic Textbox - vb.net

App & Code
Private Sub PriceBox_Click(sender As Object, e As EventArgs) Handles PriceBox.Click
If CmbSize.SelectedIndex = 0 Then
PriceBox.Text = "£30"
ElseIf CmbSize.SelectedIndex = 1 Then
PriceBox.Text = "£40"
ElseIf CmbSize.SelectedIndex = 2 Then
PriceBox.Text = "£50"
End If
End Sub
This Code works but only when clicked, What sub do i need to use for it to change automatically when Combobox index is selected

If you only want user changes of the index and not programmatic changes then use
SelectionChangeCommitted
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectionchangecommitted?view=netframework-4.7.2

The combobox has SelectedIndexChanged-event you can use:
Private Sub CmbSize_Changed(sender As Object, e As EventArgs) Handles CmbSize.SelectedIndexChanged
End Sub

You will need to handle the SelectedIndexChanged on the CmbSize control as shown in https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindexchanged?view=netframework-4.7.2

Related

How to set a combo box to a specific item with the press of a button (Visual Basic)

I'm a beginner so please excuse the dumb question.
So basically I have a "Dummy" Button that creates a new Patient by filling in Name, Age, Date etc.
But I don't know how to make it so that the combobox also gets set to a specific item. Can yall help me out? The combobox is second to last, code looks like this:
Private Sub Btn_dummy_Click(sender As Object, e As EventArgs) Handles Btn_dummy.Click
Me.Txt_Name.Text = "Mustermann"
Me.Txt_Vorname.Text = "Max"
Me.Dtp_Gebdat.Value = Today().AddDays(-365 * 12)
Me.Txt_Strasse.Text = "Musterstrasse"
Me.Txt_Hausnr.Text = "123A"
Me.Mtb_Plz.Text = "12345"
Me.Txt_Ort.Text = "Musterort"
Me.Cmb_Krankenkasse =
Me.Txt_Versnr.Text = "987654"
End Sub
This really depends on how you've setup your combobox. If you added the options through the designer then you would want to use SelectedIndex.
Me.Cmb_Krankenkasse.SelectedIndex = 1 ' Or whatever index you want to select
If you are using a binding to populate your combobox then you could use SelectedItem.
Me.Cmb_Krankenkasse.SelectedItem = selectedPersonEntry
Either way you're going to set the Selected property of the combobox.
Example:
Private Sub Btn_dummy_Click(sender As Object, e As EventArgs) Handles Btn_dummy.Click
Me.Txt_Name.Text = "Mustermann"
Me.Txt_Vorname.Text = "Max"
Me.Dtp_Gebdat.Value = Today().AddDays(-365 * 12)
Me.Txt_Strasse.Text = "Musterstrasse"
Me.Txt_Hausnr.Text = "123A"
Me.Mtb_Plz.Text = "12345"
Me.Txt_Ort.Text = "Musterort"
Me.Cmb_Krankenkasse.SelectedIndex = 1
Me.Txt_Versnr.Text = "987654"
End Sub
The user should choose something from the comboBox, right?
It is best to subscribe to the selected item event.
it works like this:
Public NotInheritable Class Form1
Private gesetzliche_KK As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange({"Viactiv", "Barmer", "Techniker"})
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
gesetzliche_KK = CStr(ComboBox1.SelectedItem)
End Sub
End Class
You should, at least for this English-speaking forum, translate your variable names into English. Du solltest, zumindest für dieses englisch-sprachige Forum, deine Variablennamen ins Englische übertragen.

How can I make textboxes appear/disappear depending on a checkbox state?

I have an application that I'm developing for a school project. This is what the application looks like
Essentially whenever one of the CheckBoxes is checked, a TextBox.Visible property is changed to true and is supposed to appear underneath the checkbox. You can have all three of them checked (or any combination checked) if you like, as long as when you uncheck it the TextBox disappears and the CheckBox appears empty/unchecked.
I've gotten to the point where I can make the TextBoxes appear and disappear but the TextBoxes are never empty. There's always a black square there that looks like this
Those black squares don't go away and I'm not sure exactly what the problem is. The TextBox also only appears when the CheckBox has that square as opposed to an actual check which is what is required. I have used a combination of If...ElseIf statements and Select Cases, which haven't done it. I've tried a few different events like CheckChanged and Click.
This is the code that I currently use that allows me to toggle the boxes.
Private Sub chkBox_Click(sender As Object, e As EventArgs) Handles chkBox.Click
If chkBox.Checked = False Then
txtBox.Visible = False
txtBox.Text = ""
Else
txtBox.Visible = True
End If
chkBox.Checked = True
End Sub
Private Sub chkLawn_Click(sender As Object, e As EventArgs) Handles chkLawn.Click
If chkLawn.Checked = False Then
txtLawn.Visible = False
txtLawn.Text = ""
Else
txtLawn.Visible = True
End If
chkLawn.Checked = True
End Sub
Private Sub chkPav_Click(sender As Object, e As EventArgs) Handles chkPav.Click
If chkPav.Checked = False Then
txtPav.Visible = False
txtPav.Text = ""
Else
txtPav.Visible = True
End If
chkPav.Checked = True
End Sub
If you guys can think of a solution or could point me in the right direction I would appreciate that.
I'd recommend this in the form load to setup a relationship between the checkboxes and the textboxes:
chkBox.Tag = txtBox
chkLawn.Tag = txtLawn
chkPav.Tag = txtPav
Then one handler:
Private Sub chkBox_Click(sender As Object, e As EventArgs) Handles chkBox.Click, chkPav.Click, chkLawn.Click
CType(sender.Tag, TextBox).Visible = CType(sender, Checkbox).Checked
End Sub
Try to remove the chkPav.Checked = True, chkLawn.Checked = True and chkBox.Checked = True in your .click events.
Also, i would recommand to use the "CheckStateChanged" vb.net event.
This will handle all your CheckBox.CheckChanged events. It finds the TextBox based on the name of the CheckBox. So just name them the same as you have (i.e. chkA and txtA).
Private textBoxPrefix As String = "txt"
Private checkBoxPrefix As String = "chk"
Private Sub chk_CheckedChanged(sender As Object, e As EventArgs) Handles chkBox.CheckedChanged, chkLawn.CheckedChanged, chkPav.CheckedChanged
Dim chk = CType(sender, CheckBox)
Dim suffix = chk.Name.Substring(checkBoxPrefix.Length)
Dim txt = Me.Controls().Find(textBoxPrefix & suffix, True).Single()
txt.Visible = chk.Checked
txt.Text = If(chk.Checked, "", txt.Text)
End Sub
Making it a little more scaleable, add handlers to all CheckBoxes in the GroupBox in Form_Load programmatically. (remove Handles chkBox.CheckedChanged, chkLawn.CheckedChanged, chkPav.CheckedChanged from the event handler declaration)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' assuming the GroupBox is named gbTickets, add all handlers programmatically
For Each chk As CheckBox In Me.gbTickets.Controls.OfType(Of CheckBox)
AddHandler chk.CheckedChanged, AddressOf chk_CheckedChanged
Next
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.

How to change selected item text in list box at run time?

I tried with code like this:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
The form is looked like this:
I need to change that list text while user typing in the text box. Is it possible to do that at run time?
You are using the form's leave event MyBase.Leave, so when it fires, it is useless to you.
Try using the TextChanged event of the TextBox instead.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
Make sure to check if an item is actually selected in the ListBox:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
Use Double click to select line (item) inside list box and change or modify.
Instead of using text box use ListBox1_MouseDoubleClick event
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
then add this code inside this event
Dim intIndex As Integer = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox1.Items.Insert(intIndex, objInputBox)
End If
OR
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = objInputBox
End If

Filling a TableAdapter from Bound ToolStripComboBox

I am trying to fill a TableAdapter based on a selection from a ToolStripComboBox.
First I want to fill the ToolStipComboBox by binding it to a datasource. Then once it is filled, I want to fill the TableAdapter.
This is my code:
Private Sub ToolStripComboBox_MessageType_Click(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.Click
Me.ToolStripComboBox_MessageType.ComboBox.DataSource = DataSet_UToolDb.XML_MESSAGE_TYPE
Me.ToolStripComboBox_MessageType.ComboBox.DisplayMember = "MessageType"
Me.ToolStripComboBox_MessageType.ComboBox.ValueMember = "MTId"
End Sub
Private Sub ToolStripComboBox_MessageType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.SelectedIndexChanged
Me.TableAdapter_XML_MESSAGE_STRUCTURE.Fill(DataSet_UToolDb.XML_MESSAGE_STRUCTURE, Me.ToolStripComboBox_MessageType.ComboBox.SelectedValue)
End Sub
For some reason (if I step through my code) the code jumps from the where the datasource is set, to where the TableAdapter is filled. This is causing an exception as the TableAdapter's select query is looking for a value based on the value that was selected from the ToolStipComboBox.
I suspect your code is jumping to where the TableAdapter is filled because by setting the DataSource of the ComboBox you're causing the SelectedIndexChanged event to be fired.
So, you need to tell the SelectedIndexChanged handler to return should the ToolStripComboBox not be populated yet, which you could do by setting a Boolean flag when the ToolStripComboBox has been populated. For example:
Dim m_ToolStripComboBoxPopulated As Boolean
Private Sub ToolStripComboBox_MessageType_Click(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.Click
Me.m_ToolStripComboBoxPopulated = False
Me.ToolStripComboBox_MessageType.ComboBox.DataSource = DataSet_UToolDb.XML_MESSAGE_TYPE
Me.ToolStripComboBox_MessageType.ComboBox.DisplayMember = "MessageType"
Me.ToolStripComboBox_MessageType.ComboBox.ValueMember = "MTId"
' Indicate ToolStripComboBox has been populated
Me.m_ToolStripComboBoxPopulated = True
End Sub
Private Sub ToolStripComboBox_MessageType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.SelectedIndexChanged
If (Me.m_ToolStripComboBoxPopulated = False) Then
Return
End If
Me.TableAdapter_XML_MESSAGE_STRUCTURE.Fill(DataSet_UToolDb.XML_MESSAGE_STRUCTURE, Me.ToolStripComboBox_MessageType.ComboBox.SelectedValue)
End Sub