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

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.

Related

Visual Basic Dynamic Textbox

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

Replacing listview items with another list using combo box

Pardon for being novice at vb.net. I have a combo box and a list view. What I needed is when I change the category in the combo box and pressed 'OK', the old list added before will be replaced by a new list.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub
What should I need to add?
What you are doing there is adding a different row on each case. What you ask is to fill the listview with different items depending on the item you pick at the combobox.
Basically, you need to clear the listview on each case. I'd do something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
ListView1.Items.Clear
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub

vb.net combobox hide item

I have a combobox that when the item is selected and it's the same as a button.text in other form the button.text changes for the name that the user typed in a textbox. But if the item is different of the button.text I want to hide it, so the user can't select it or see it.
cafetariacombo is the combobox Form3.cafetaria2.Text is the button I'm changing
If cafetariacombo.SelectedItem = "cafetaria2" Then
Form3.cafetaria2.Text = TextBox1.Text
My.Settings.cafetaria2guardar = Form3.cafetaria2.Text
My.Settings.Save()
end if
I use this to name the button, I just need to know if I can hide the combobox item.
Help me please :)
Update with some code
I inserted the list of items myself in the combobox.
I solved my earlier problem, but now i need to save the state of the combobox items when I leave the form.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If cafetariacombo.SelectedItem <> Form3.cafetaria1.Text Then
cafetariacombo.Items.Remove("cafetaria1")
End If
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox2.SelectedItem = Form3.cafetaria1.Text Then
Form3.cafetaria1.Text = "cafetaria1"
My.Settings.cafetaria1guardar = Form3.cafetaria1.Text
My.Settings.Save()
adicionarproduto.cafetariacombo.Items.Add("cafetaria1")
end if
end sub
When I remove the item from the combobox I'm in form1 and when I add the item again I'm in form2. Just need to save the combobox with the deleted item when I leave the form1.
You can use a DataTable as data source, then it's as easy as changing DefaultView.RowFilter, please study this example and let me know if you have any questions:
Public Class Form1
Private Sub Button1_Click(sender As System.Object,
e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Rows.Add("1")
dt.Rows.Add("2")
dt.Rows.Add("3")
ComboBox1.DisplayMember = "id"
ComboBox1.DataSource = dt 'show all items by default
End Sub
Private Sub Button2_Click(sender As System.Object,
e As System.EventArgs) Handles Button2.Click
DirectCast(ComboBox1.DataSource, DataTable).DefaultView.
RowFilter = "id <> 2" 'hide item=2 from the view
End Sub
End Class

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

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub