Visual Basics, Store comboBox value in variable - vb.net

I want to save the comboBox value in a variable.But whenever I change the comboBox value the value gets set is null, and the selected index shows as '-1'.Below is my code.
Private Sub SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim form As CreateEvalForm = New CreateEvalForm //windows Form
Dim str As String = form.ComboBox1.SelectedIndex
MessageBox.Show(str) //shows null
Dim openingId As Integer = Val(form.ComboBox1.Text)
End Sub
Could anyone please suggest a solution?

Private Sub SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim str As String = ComboBox1.SelectedIndex
MessageBox.Show(str)
Dim openingId As Integer = Val(ComboBox1.Text)
End Sub
Does this work? But if you wanted a value from the combobox's selected value, you should try the following:
dim openingId as Integer = Val(comboBox1.SelectedValue)

Dim form As CreateEvalForm = New CreateEvalForm //windows Form
With this line you are creating a new form, so the combobox on that new form will be new as well and will not have a selected index.
You can just use the combobox in this way:
Dim str As String = ComboBox1.SelectedIndex
or
Dim str As String = Me.ComboBox1.SelectedIndex

Related

The row is overwriting and not adding rows listview

Im sending the data from form1(Home) to form2 (StatusReport) but the information taken from form 1 is not adding to the next row in the form2, instead it is overwriting the same row. I did it on the form 1, the adding per row but from sending the data from form 1 to form 2, it's not adding properly.
Form 1 code
Dim recipientName As String = TextBox5.Text
Dim address As String = TextBox6.Text
Dim contactNumber As String = TextBox7.Text
Dim deliveryMode As String = ComboBox3.SelectedItem
Dim deliveryDate As Date = DateTimePicker1.Value.Date
Form 2 code
Dim recipientName As String = Home.TextBox5.Text
Dim address As String = Home.TextBox6.Text
Dim contactNumber As String = Home.TextBox7.Text
Dim deliveryMode As String = Home.ComboBox3.SelectedItem
Dim deliveryDate As Date = Home.DateTimePicker1.Value.Date
Dim orderStatus As String = "Pending"
Dim str(6) As String
Dim lvItem As ListViewItem
str(0) = recipientName
str(1) = address
str(2) = contactNumber
str(3) = deliveryMode
str(4) = deliveryDate
str(5) = orderStatus
lvItem = New ListViewItem(str)
ListView1.Items.Add(lvItem)
Creating a new ListViewItem using a String Array creates a single ListViewItem with the array signifying the sub items.
Maybe simpler to create the ListViewItems individually and add them, like this:
lv.Items.Add(New ListViewItem(recipientName))
lv.Items.Add(New ListViewItem(Address))
lv.Items.Add(New ListViewItem(contactNumber))
lv.Items.Add(New ListViewItem(deliveryMode))
lv.Items.Add(New ListViewItem(recipientName))
etc.
You may want to clear the items first:
lv.Items.Clear()
It is unclear how you are going back and forth between the 2 forms but this should get you started. Set up the columns in the ListView before adding any items. You can either do this once in code or at design time. ListViews hold ListViewItems. The first column is the Text property and the following columns are SubItems.
Private Sub CreateLVColumns()
'you will probably want to do this at design time
With ListView1.Columns
.Add("Name")
.Add("Address")
.Add("Number")
.Add("Mode")
.Add("Date")
.Add("Status")
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lvi As New ListViewItem(Home.TextBox5.Text)
With lvi.SubItems
.Add(Home.TextBox6.Text)
.Add(Home.TextBox7.Text)
.Add(Home.ComboBox3.Text)
.Add(Home.DateTimePicker1.Value.Date.ToString) 'you can add a format to the ToString
.Add("Pending")
End With
ListView1.Items.Add(lvi)
End Sub
found the answer, this is just the base. Just added some codes to fit into the required system.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim itm As New ListViewItem(TextBox1.Text)
itm.SubItems.Add(TextBox2.Text)
itm.SubItems.Add(TextBox3.Text)
Form2.ListView1.Items.Add(itm)
Form2.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
End Sub
End Class
Leaving this here as it can help others too.

type the NAME of a TextBox into a TextBox and then be able to retrieve the contents of the named TextBox

for example, I want to declare the textbox based on what I write in the box. if I write textbox1. if I type textbox2.text then it will be textbox2.text
Dim txt As TextBox = TextBox2.Text.ToString
Dim txt As String = TextBox2.Text
none of the methods is valid
Your verbiage, although confusing, makes it seem like you want to type the NAME of a TextBox into a TextBox and then be able to retrieve the contents of the named TextBox. If that's correct, you're probably looking for Controls.Find() with the recurse option:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtBoxName As String = TextBox1.Text.Trim
If txtBoxName.Length > 0 Then
Dim ctl As Control = Me.Controls.Find(txtBoxName, True).FirstOrDefault
If Not IsNothing(ctl) AndAlso TypeOf (ctl) Is TextBox Then
Dim tb As TextBox = DirectCast(ctl, TextBox)
' ... do something with "tb" ...
Dim value As String = tb.Text
MessageBox.Show(value)
Else
MessageBox.Show("Control not found, or incorrect type!")
End If
End If
End Sub

Edit Selected Datagridview Row vb.Net

I have a Datagridview grid with four columns. When a cell is double-clicked, the data on the selected row is passed to four textboxes for the user to make changes if any.So how do i pass the changes made to the selected row instead of having the changes added as new row?
P.S. The Data isn't from a database
You can either "remember" the DataGridViewRow by setting a module-level variable, or you can find the row again by looking for its primary key.
Public Class Form1
'Add to form:
' DataGridView called DataGridView1
' 4 Textboxes called TextBox1, TextBox2, TextBox3, and TextBox4
' Button called btnEdit
' Button called btnSave
Private mintRowWeAreEditing As Integer = -1
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If DataGridView1.DataSource Is Nothing Then
'set initial data
Dim dtb As New DataTable
dtb.Columns.Add("Col1")
dtb.Columns.Add("Col2")
dtb.Columns.Add("Col3")
dtb.Columns.Add("Col4")
dtb.Rows.Add("R1C1", "R1C2", "R1C3", "R1C4")
dtb.Rows.Add("R2C1", "R2C2", "R2C3", "R2C4")
dtb.Rows.Add("R3C1", "R3C2", "R3C3", "R3C4")
dtb.Rows.Add("R4C1", "R4C2", "R4C3", "R4C4")
DataGridView1.DataSource = dtb
End If
'copy data from grid to textboxes
mintRowWeAreEditing = DataGridView1.CurrentCell.RowIndex
Dim drw As DataRow = DirectCast(DataGridView1.Rows(mintRowWeAreEditing).DataBoundItem, DataRowView).Row
TextBox1.Text = drw("Col1").ToString
TextBox2.Text = drw("Col2").ToString
TextBox3.Text = drw("Col3").ToString
TextBox4.Text = drw("Col4").ToString
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
'copy data from textboxes to grid
If mintRowWeAreEditing = -1 Then Exit Sub 'haven't clicked Edit button yet
Dim drw As DataRow = DirectCast(DataGridView1.Rows(mintRowWeAreEditing).DataBoundItem, DataRowView).Row
drw("Col1") = TextBox1.Text
drw("Col2") = TextBox2.Text
drw("Col3") = TextBox3.Text
drw("Col4") = TextBox4.Text
End Sub
End Class
So, for example, you could do something like this on your button click event.
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim refNo As Integer = txtRefNo.Text ' Change DataType and TextBox name as appropriate
Dim firstName as String = txtFName.Text
' Repeat setting variables for each field in the row that you're updating
For Each dgr As DataGridViewRow in DataGridView1.Rows
If dgr.Item("RefNo") = refNo Then
dgr.Cells(0).Value = firstName 'Instead of using (0) you can use the column name
dgr.Cells(1).Value = newVar
End If
Next
' Commit the changes/refresh here
End Sub
I don't have the IDE to hand to test this but any problems let me know, I'll have a look into it.

Insert Arrays into Combobox

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim window As New System.IO.StreamReader("Pfad", True)
Dim lines As String
Dim Element As String()
Dim SplitArray As String()
lines = window.ReadToEnd
Element = lines.Split("!")
Element.Count
For i = 0 To Element.Count - 1
SplitArray = Element(i).Split(Chr(13))
Next
ComboBox1.Items.Add(SplitArray)
End Sub
Hello I'm a Newbie. I want to fill my Combobox with the results of the split.
Use addrange to fill combobox items with array of strings
ComboBox1.Items.AddRange(SplitArray)

Pass variable to new form with Datatable and Listbox

I am currently trying to write an application like address book. Listbox works properly, it shows everything corretly. But I need to pass id of chosen listbox item to another form. I got code like this in Form2:
Private myTable As New DataTable()
Public Sub LoadXml(sender As Object, e As EventArgs) Handles Me.Load
With myTable.Columns
.Add("DisplayValue", GetType(String))
.Add("HiddenValue", GetType(Integer))
End With
myTable.DefaultView.Sort = "DisplayValue ASC"
ListBox1.DisplayMember = "DisplayValue"
ListBox1.ValueMember = "HiddenValue"
ListBox1.DataSource = myTable
Dim doc As New Xml.XmlDocument
doc.Load("c:\address.xml")
Dim xmlName As Xml.XmlNodeList = doc.GetElementsByTagName("name")
Dim xmlSurname As Xml.XmlNodeList = doc.GetElementsByTagName("surname")
Dim xmlId As Xml.XmlNodeList = doc.GetElementsByTagName("id")
For i As Integer = 0 To xmlName.Count - 1
Dim nazwa As String = xmlName(i).FirstChild.Value + " " + xmlSurname(i).FirstChild.Value
myTable.Rows.Add(nazwa, xmlId(i).FirstChild.Value)
MsgBox(myTable.Rows(i).Item(1).ToString)
Next i
ListBox1.Sorted = True
End Sub
Later in the code I have event:
Public Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
End Sub
I would like to know how can I call id from DataTable for selected listbox item. I hope u understand what I mean since my english is not perfect :)
Since you have added the XML value id to the data table column HiddenValue and you have assigned HiddenValue as the ValueMember for the listbox, once a record is selected in the listbox, id will be available in the listbox's [SelectedValue][1] member. For example:
Public Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
MsgBox("Selected Id: " & ListBox1.SelectedValue.ToString())
End Sub