How do I set a Combox with a default value when the Drop Down Style is set to "Dropdownlist - vb.net

I am using Visual Studio 2019, and I've been trying to get my combo box to set a default value using a record from a Database. I have the Drop Down Style set to "Drop Down List" and this is the code I am Using.
First I am loading the listbox from the state data table into the combo box
Private Sub frmABS3_Shown(sender As Object, e As EventArgs) Handles Me.Shown
modABS.clsABS.ExecQuery("SELECT * FROM tblABSStates")
If Not String.IsNullOrEmpty(modABS.clsABS.Exception) Then MsgBox(modABS.clsABS.Exception) : Exit Sub
For Each r As DataRow In modABS.clsABS.DBDT.Rows
cboState.Items.Add(r("StateAbbrev"))
Next
End Sub
Next I am populating the fields on the Form. This is the part I am having trouble.
Private Sub GetRecord()
' Fail if No Records Found or position is out of Range
If modABS.clsABS.DBDT.Rows.Count < 1 OrElse CurrentRecord > modABS.clsABS.DBDT.Rows.Count - 1 Then Exit Sub
'Return first user found
Dim r As DataRow = modABS.clsABS.DBDT.Rows(CurrentRecord)
'Populate Fields
cboState.Text = r("StateAbbrev").ToString
txtCity.Text = r("City").ToString
End Sub
When I run the code the combo box doesn't show the state abbrev and the item isn't selected on the list. It works the way its suppose to when I reset the Drop Down Style set to "Drop Down", however I don't want the user to be able to add a value that is not in the combo listbox.

This code needed to be in the form load event not the form shown event:
Private Sub frmABS3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
modABS.clsABS.ExecQuery("SELECT * FROM tblABSStates")
If Not String.IsNullOrEmpty(modABS.clsABS.Exception) Then MsgBox(modABS.clsABS.Exception) : Exit Sub
For Each r As DataRow In modABS.clsABS.DBDT.Rows
cboState.Items.Add(r("StateAbbrev"))
Next
txtZipCode.Text = modABS.clsABS.ZipCode
End Sub

Related

How to pass the 2nd selected row in datagridview to textbox?

How do i pass my 2nd selected row in datagridview to textboxt. I only know how to put the first data. How do i pass the 2nd selected to textbox?
Dim i As Integer
i = DataGridView2.SelectedRows(0).Index
Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value
you can use this code it worked for me
Dim test As String = DataGridView1.SelectedRows(0).Cells(2).Value.ToString
You need to only change the .cell(Here write the index value of the cell)
Then you can use the string value to fill up textbox
Try using the following:
Dim secondRow as integer = 0
Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
'assuming that by second selected row, you mean the row after the selected row
secondRow = e.RowIndex + 1
End Sub
Private Sub RowToTextBox()
Try
For i = 0 to DataGridView2.ColumnCount - 1
txtEmployeeID.Text &= DataGridView2.Item(i, secondRow)
Next
Catch ex as Exception
MsgBox("You have selected the final row")
End Try
End Sub
I don't think Me. is needed when referring to a textbox in the same form.
Since you don't know how many rows your user will select, I think looping thought the SelectedRows collection might work. I used the Name column because that is what I happened to have in my grid. Instead of a MessageBox you could add the values to a ListBox.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each GridRow As DataGridViewRow In DataGridView1.SelectedRows
MessageBox.Show($"Value is {GridRow.Cells("Name").Value}")
Next
End Sub

How to make a for loop which can check/edit multiple VB.net TextBoxes

For instance I have 5 textboxes in VB designer with different names
TextBox1, TextBox2 .... TextBox5
Is there any simple way to load/check the TextBox1, 2 ...5, by using 1 line recursively?
I am trying to load the textboxes with a list of list data structure.
So every time I have to put a TextBox(number).Text to load it.
Private Sub addbutton_click() handles addbutton.click
TextBox1.Text=MyList1(Some_constant)(other_constant).Name(0)
TextBox2.Text=MyList1(Some_constant)(other_constant).Name(1)
End Sub
I want to use a for loop which should look something like this so that I can iterate over the Name(0), (1) and the TextBox(number).Text. I am not able to iterate over the TextBox(number) as the textBox is a VB tool item.
Private Sub addbutton_click() handles addbutton.click
For i As Integer = 0 to 5
TextBox(i).Text = MyList1(Some_constant)(other_constant).Name(i)
Next i
End Sub
Add your index values into the Tag property of the TextBox.
Loop through the controls when you need to do your recursive processing:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
'get your list of text boxes
For Each tb As TextBox In Me.Controls.OfType(Of TextBox)
Dim indexValue As Integer
'get your index value out the tag
Integer.TryParse(tb.Tag, indexValue)
'use it
tb.Text = MyList1(Some_constant)(other_constant).Name(indexValue)
Next
Catch ex As Exception
MessageBox.Show(String.Concat("An exception occurred: ", ex.Message))
End Try
End Sub

How to remove a row from datagridview and reset a particular column (vb.net 2005)

I am using VS 2005 edition. I am developing a winform desktop application. Let's say I have a unbound datagridview, with table like below:
Original datagrid before delete:
If I removed Stuff D (Item No 5), the "Item No" column supposed to reset itself accordingly. The expected output should be:
After delete row:
The "Item No" column is not an autonumber, it's just the number I assigned incrementally as the user add in a new row(new Stuff). I tried using the following code in rowremoved event but failed to achieve the expected output. Please help. Thanks.
If the goal is just showing the record index in cell, then you don't need to assign a value to cell and it's enough to set e.Value = e.RowIndex + 1 in CellFormatting event.
But based on your comment it seems you need those cells have values as well. So you need to handle RowsRemoved and RowsAdded event of DataGridView and refresh the cell value with row number:
Public Sub RefreshRowNumbers(g As DataGridView)
For Each r As DataGridViewRow In g.Rows
r.Cells(1).Value = r.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, _
e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, _
e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub

get ID from datagridview and show the data to another form in textboxes

Im kind of new in vb.net. I have a datagridview that shows the Delivery Number, Date and supplier. Now, I want the Admin to view the details of every delivery to another form. I just want to know how will I get the id of the selected row and then will be able to display the equivalent data of that selected ID. Thanks.
Here's my code for the Deliveries Form.
Private Sub dgvDeliveryReport_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDeliveryReport.CellContentDoubleClick
If e.RowIndex < 0 Then Exit Sub
Dim id As Int32 = dgvDeliveryReport.CurrentRow.Cells(0).Value
Dim viewDelivery As New frmDeliveryFormReport
frmDeliveryFormReport.Show()
End Sub
In your frmDeliveryFormReport class add a new field to store current row:
private _currentDeliveryReportRow as DataGridViewRow
Look for the constructor:
Public Sub New frmDeliveryFormReport()
...
End Sub
(If you cannot find it just proceed with the next step).
Change/Add the constructor so it takes the DataGridViewRow parameter and store the given row:
Public Sub New frmDeliveryFormReport(deliveryReportRow as DataGridViewRow)
_currentDeliveryReportRow = deliveryReportRow
End Sub
Adapt your existing dgvDeliveryReport_CellContentDoubleClick to call the new constructor:
Private Sub dgvDeliveryReport_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDeliveryReport.CellContentDoubleClick
If e.RowIndex < 0 Then Exit Sub
Dim viewDelivery As New frmDeliveryFormReport(dgvDeliveryReport.CurrentRow)
frmDeliveryFormReport.Show()
End Sub
You can then access all columns of the DeliveryReportRow in the frmDeliveryFormReport via
_currentDeliveryReportRow.Cells(<CellIndex>)
Additional information about this topic:
Passing variables between windows forms in VS 2010
VB.Net Passing values to another form
http://www.dreamincode.net/forums/topic/332553-passing-data-between-forms/
Try.
Dim newFrmName as new yourForm
For each row as DataGridViewRow in SampleGrid
if row.selected = true then
Dim whatValueYouWant as string = row.cells("ID").value.toString()
if newFrmName.NameOfTextBoxInForm.Text <> vbEmpty Then
'NameOfTextBoxInForm is textbox that existing in yourform
newFrmName.NameOfTextBoxInForm.text = ", " & whatValueYouWant
Else
newFrmName.NameOfTextBoxInForm.text = whatValueYouWant
End If
End IF
Next
newFrmName.Show()

Data doesn't display when working with multiple forms

I'm new to VB.NET and have been struggling all afternoon with something. I've found similar questions on the forum but none of them seemed to describe my problem exactly. I'm fairly sure that I'm missing something very basic.
I have made a main form which currently holds only one button which purpose is to open up a second form and close the main form. Based on the settings the user will select on the 2nd form the first form might have to be adapted to match with the new settings. But the problem occurs even before that.
The 'settings' form has 15 textboxes which I drew onto the form in development mode. They are called ID1, ID2,..,ID15. The values which I want to display in there are saved in an array:
Dim ids(15) as integer
Next, I created a module to simulate what you could call a control array as I used to use them in VB6.
Public sources() As TextBox = [frmSettings.ID1, frmSettings.ID2, //and so on
I did this to be able to iterate through all the 15 textboxes:
For i = 0 To 14
Sources(i).Text = ids(i + 1)
Next
Then I added on the main form this code to the Button1_Click() event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frmSettings.Show()
Me.Close()
End Sub
I did the same thing for the 'exit ' button on the frmSettings form.
This seems to work, but only once. I launch the application, push the button and frmSettings pops up and shows all the values from the array in the textboxes. When I push the 'close' button, I return to the main page.
So far so good, but if I try to return to frmSettings a second time, all the textboxes remain blank as if the code I added to the form never gets executed.
Any help would be greatly appreciated!
First, make sure the array that holds your data is accessible to both forms:
Module Module1
Public ids(15) As Integer
End Module
There should not be a declaration for "ids" in either form.
Next, make frmSettings itself responsible for loading and saving the data:
Public Class frmSettings
Private Sub frmSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim matches() As Control
For i As Integer = 0 To 14
matches = Me.Controls.Find("ID" & (i + 1), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim TB As TextBox = DirectCast(matches(0), TextBox)
TB.Text = ids(i)
End If
Next
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim valid As Boolean = True
Dim matches() As Control
For i As Integer = 0 To 14
matches = Me.Controls.Find("ID" & (i + 1), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim TB As TextBox = DirectCast(matches(0), TextBox)
Dim value As Integer
If Integer.TryParse(TB.Text, value) Then
ids(i) = value
Else
MessageBox.Show(TB.Name & ": " & TB.Text, "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Warning)
valid = False
End If
End If
Next
If valid Then
Me.Close()
End If
End Sub
End Class