Filling a TableAdapter from Bound ToolStripComboBox - vb.net

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

Related

VB.NET Trigger Datagridview Cell Click on Button CLick

I'm trying to trigger this command when the button is clicked
Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
TextBox1.Text = row.Cells(0).Value.ToString
TextBox2.Text = row.Cells(1).Value.ToString
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClickDataGridview()
End Sub
but sadly I received two error
Argument not specified for parameter 'sender' of 'Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)'.
Argument not specified for parameter 'e' of 'Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)'
Should I make it an if statement to work? or should I try something else to trigger this event
There is a way but you have to careful about selection of cells . If you have to do only row operations then it is ok with this. I recommend don't do this instead that put button in gridview for each row and perform operation
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
If DataGridView1.SelectedCells.Count > 0 Then
'Here you can change Datagridview row selection property and get selectedrows instead of selected cells
Dim i_rowindex As Integer = DataGridView1.SelectedCells(0).RowIndex
Dim i_colIndex As Integer = DataGridView1.SelectedCells(0).ColumnIndex
DataGridView1_CellMouseClick(sender, New DataGridViewCellMouseEventArgs(i_colIndex, i_rowindex, 0, 0, New MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)))
End
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
As I may have mentioned once or twice, don't call event handlers directly. Put the common code in it's own method and then call that method from each event handler as appropriate. In this case:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GetRowValues(DataGridView1.CurrentRow)
End Sub
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.Button = MouseButtons.Left AndAlso e.RowIndex >= 0 Then
GetRowValues(DataGridView1.Rows(e.RowIndex))
End If
End Sub
Private Sub GetRowValues(row As DataGridViewRow)
TextBox1.Text = row.Cells(0).Value.ToString()
TextBox2.Text = row.Cells(1).Value.ToString()
End Sub
I may be missing something, however… I do not understand why you would want to force the user to “click” a button to set the text boxes. If you wire up the grids “SelectionChanged” event and update the text boxes in that event, then the user neither has to click on a button or a cell. If the user uses the Arrow keys, Enter key, Tab key or even “clicks” a cell, the text boxes will change automatically without the user having to click a button.
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If (DataGridView1.CurrentRow IsNot Nothing) Then
TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
End If
End Sub
Or... better yet... if the grid uses a data source, then "Binding" the text boxes to that "same" data source is all that is needed. You will not have to wire up any grid events.
TextBox1.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName1"))
TextBox2.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName2"))

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.

Custom Textbox Zoom - Return text to active datagridview cell?

I have 2 Datagridview controls in same form. Each Datagrid has some columns where user will write long texts, so I designed form with RichTextBox that opens when user double-clicks these columns to enlarge text-entry. Code works, but I want to use same form for both Datagrids, so I should somehow return text to active datagridview cell. Here is my code (for Datagridview1):
TextZoomForm:
Public Class TextZoomForm
Public OpenedForm1 As New Form1
Private Sub RichTextBox1_DoubleClick(sender As Object, e As EventArgs) Handles RichTextBox1.DoubleClick
OpenedForm1.DataGridView1.CurrentCell.Value = RichTextBox1.Text
OpenedForm1.Label24.Focus()
Me.Close()
End Sub
Private Sub TextZoom_Load(sender As Object, e As EventArgs) Handles Me.Load
RichTextBox1.Text = OpenedForm1.DataGridView1.CurrentCell.Value
End Sub
End Class
DataGridView1_CellMouseDoubleClick in Form1:
Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
If e.ColumnIndex = 1 Then
Dim cp = Cursor.Position
cp.Y += CInt(Cursor.Size.Height * (-0.5))
cp.X += CInt(Cursor.Size.Width * 0.8)
Dim f As New TextZoomForm()
f.OpenedForm1 = Me
f.Show()
f.Location = New Point(cp)
End If
End Sub
Any ideas on how to return text to active datagridview cell?
Change your zoomed form so that it doesn't know where its data comes from. Instead the control using it will pass the data.
Public Class TextZoomForm
Public Property ZoomedText As String
Get
Return RichTextBox1.Text
End Get
Set(value As String)
RichTextBox1.Text = value
End Set
End Property
Private Sub RichTextBox1_DoubleClick(sender As Object, e As EventArgs) Handles RichTextBox1.DoubleClick
Me.Close()
End Sub
End Class
To call the form change your code to the following:
Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
...
Dim f As New TextZoomForm()
f.ZoomedText = DataGridView1.CurrentCell.Value
f.ShowDialog()
'Great breakpoint location.
DataGridView1.CurrentCell.Value = f.ZoomedText
Label24.Focus()
....
End Sub
Using ShowDialog prevents the user from changing the current cell part way through your call.
If you need it modeless then you should:
store the cell that the user has selected
handle the FormClosing event.
Test the DialogResult to make sure the user pressed ok
write the data back to the stored cell.

VB.Net how to prevent listbox firing selectedindexchanged selectedvaluechanged on load

Help, When I set datasource to listbox it fires selectedindexchanged and selectedvaluechanged. How can I prevent this because this resets my value to the first item all the time
Dim SQLString As String
Dim dt As New DataTable
Dim Properties As New Properties
SQLString = "select v.attributevalue,v.value"
SQLString += " from Table "
SQLString += " Where v.attributename ='new_vehicle'"
SQLString += " order by v.value"
getDataBySQL(SQLString, dt)
lstCategory.ValueMember = "attributevalue"
lstCategory.DisplayMember = "value"
lstCategory.DataSource = dt
Dim i As Integer = ReadDataFromDatabase
If i <> 0 Then
lstCategory.SelectedValue = i
End If
Private Sub lstCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCategory.SelectedIndexChanged
End Sub
Private Sub lstCategory_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstCategory.SelectedValueChanged
SetDataToDatabase(lstCategory.SelectedValue.ToString)
End Sub
also when I set valuemember to the listbox it fires selectedvaluechanged.
Essentially I want to read the database value so when I have the listbox loaded it switch the selected value to what is the value in the database
Variable i is the selectedValue I want.
Typically when I develop an application, I will create a Boolean variable called IsLoading and set it to false by default. I will flag this as true before any code that may be loading data and mark it as false when completed. In most cases this is in my Page Load.
Private Sub Form_Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
IsLoading = True
'Code to load my data goes here
IsLoading = False
End Sub
I will then add it to my dropdowns like so:
Private Sub lstCategory_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstCategory.SelectedValueChanged
If Not IsLoading Then
SetDataToDatabase(lstCategory.SelectedValue.ToString)
End If
End Sub
So anything the IsLoading flag is false, the code will be allowed to run.
Hope this helps.

textbox on form within form not updating

I'm trying to understand more about VB.NET and Multiple forms so I can make my code better.
I have a SQL database table that holds the live data for all 14 processes, the monitor program updates a form showing the progress of all the processes. Years ago in MS Access I would have simply used a rolling subform to show the contents of the table.
However, my first attempt in VB.NET is to have "many" textboxes, basically 14 lines of textboxes and my code has 14 very similar parts updating all the textboxes. There has to be a better way :(
For Example:
txtProcessID1.Text TxtStatus1.Text ProgressBar1 ......
txtProcessID2.Text TxtStatus2.Text ProgressBar2 ......
txtProcessID3.Text TxtStatus3.Text ProgressBar3 ......
So, I'm trying to come up with a code where I create a SubForm that looks like one controller line, then create 14 instances of this subform on my mainform.
My test code seems to work but the textboxes on the subforms are not updating the contents on screen!! Even though when I call back the contents of .text it is what I expect.
Why does this example code not work, and is my solution the best way to complete this?
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SubForm.SetText = Me.TextBox1.Text ' Try to change contents of a TextBox on the SubForm
Me.TextBox2.Text = SubForm.SetText ' Data comes back as expected, but the subform textbox remains unchanged.
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim objSubForm As New SubForm()
objSubForm.TopLevel = False
Me.Panel1.Controls.Add(objSubForm)
objSubForm.Show()
End Sub
End Class
Public Class SubForm
Public Property SetText() As String
Get
SetText = TextBox1.Text
End Get
Set(ByVal Value As String)
Me.TextBox1.Text = Value ' Control is not updated of the SubForm.
Debug.Print("Value = " & Value) ' The Value is Passed Correctly.
Debug.Print("Text = " & TextBox1.Text) ' And the property of the control has been updated.
End Set
End Property
End Class
Many Thanks
Kev
In your button click you are referencing the class name SubForm, this in VB.NET is called as the default automatic instance of a form, but this is not the same instance that you display in your Form_Load. Here you create a different instance called objSubForm and this is the instance that you display.
To fix you need to keep the objSubForm as a global instance and refer to this global when you click
Public Class MainForm
Dim objSubForm As SubForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
objSubForm.SetText = Me.TextBox1.Text ' Try to change contents of a TextBox on the SubForm
Me.TextBox2.Text = objSubForm.SetText ' Data comes back as expected, but the subform textbox remains unchanged.
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
objSubForm = New SubForm()
objSubForm.TopLevel = False
Me.Panel1.Controls.Add(objSubForm)
objSubForm.Show()
End Sub
End Class
Keep in mind that after this change you are responsible to effectively close and dispose the global instance.
Private Sub Form1_FormClosed(sender as Object, e as FormClosedEventArgs) _
Handles Form1.FormClosed
if objSubForm IsNot Nothing
objSubForm.Close
End If
objSubForm = Nothing
End Sub
Thanks Steve, I applied the new code in just half an hour based on your fix.
Here's a basic example of my code that gives me 14 subforms I can update.
I've moved away from creating a property in the subform in favour of directly updating the TextBox control.
The solution has given me "MUCH" less code and now I can add another detail to the one subform and show that for all processes.
Thanks Again!
Kev
Public Class MainForm
Dim objSubForm(14) As SubForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Update the 14 forms
For n = 0 To 13
objSubForm(n).TextBox1.Text = Me.TextBox1.Text & " " & n
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' create 14 subforms
For n = 0 To 13
objSubForm(n) = New SubForm()
objSubForm(n).TopLevel = False
Me.Panel1.Controls.Add(objSubForm(n))
objSubForm(n).Location = New Point(0, 20 * n)
objSubForm(n).Show()
Next
End Sub
Private Sub MainForm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
' CLear up
If objSubForm IsNot Nothing Then
For n = 0 To 13
objSubForm(n).Close()
Next
End If
For n = 0 To 13
objSubForm(n) = Nothing
Next
End Sub
End Class