Winforms ListBox Control Not Updating After Source Changes - vb.net

I have a ListBox (LB) with a DataTable (DT) DataSource in the Form Class globally populated in the Form_Load event.
Private Sub frmEditPresets_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DT.Columns.Add("DisplayText")
DT.Columns.Add("PresetID")
For Each TSI As ToolStripItem In Presets.DropDownItems
If TSI.Name.IndexOf("preset_") > -1 Then
DT.Rows.Add(TSI.Text, TSI.Name)
End If
Next
LB.DataSource = DT
LB.DisplayMember = "DisplayText"
End Sub
When I use my Rename button. It updates the menu item and the Data Source but the Listbox doesn't refresh until I click another item in the listbox.
Rename code:
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
Dim R As DataRowView = LB.SelectedItem
Dim S As String = InputBox("Preset Name", "Rename", R("DisplayText"))
If S.Trim.Length = 0 Then Exit Sub
If Presets.DropDownItems.ContainsKey(R("PresetID").ToString) Then
Presets.DropDownItems(R("PresetID").ToString).Text = S
End If
R("DisplayText") = S
End Sub
I'm sure this is a simple question with a simple answer but I can't seem to figure it out. I've tried Refresh(). I've tried setting the DataSource again. I read this StackOverflow question Winforms listbox not updating when bound data changes but ResetBindings() doesn't seem to be an available method in this context.
*Edit. I gave Steve credit for the answer as he mentioned BindingContext. Although, that led me to find BindingContext(DT).EndCurrentEdit() which updated my LB display and maintained the selection.

Tried with this, and it works.....
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
Dim R As DataRowView = LB.SelectedItem
Dim S As String = InputBox("Preset Name", "Rename", R("DisplayText"))
If S.Trim.Length = 0 Then Exit Sub
If Presets.DropDownItems.ContainsKey(R("PresetID").ToString) Then
Presets.DropDownItems(R("PresetID").ToString).Text = S
End If
R("DisplayText") = S
BindingContext(DT).EndCurrentEdit()
End Sub

Related

How do I load a form inside another form?

I created a registration form and added a form with features "Update, Delete, Refresh," along with a DataGridView to show data from the registration form.
Here's my form:
Since I inserted a TreeView, when I click the Update button as in the picture, I get the error:
Object Reference Not set to an instance of an object
I think my code in the TreeView form is wrong.
This is what I entered:
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "Update/Delete Student" Then
Dim regstu As New Registered_Students
regstu.MdiParent = Me
regstu.Show()
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "Update/Delete Student" Then
Dim regstu As New Registered_Students
Form_father.IsMdiContainer = True
Form_father.TopLevel = False
regstu.MdiParent = New Form_father
Form_father.Panel1_Container.Controls.Add(regstu)
regstu.Show()
End If
End Sub

Get Selected row item from gridview2

I have a one gridcontrol (GridControl1)
Inside thid gridcontrol, there is two Grid Views (GridView1 & GridView2)
I want to get value of Selected row item in GridView2 and put it in a textbox.
on GridView1 I Can Get That using This Code:
txtEmpId.Text = GridView1.GetFocusedRowCellDisplayText(colEmp_Id)
But if I Select any Row on GridView2, nothing I will get.
Is there any method to do that.
If you have using the master-detail please review the help article describing in detail
In you GridControl you must handle the Grid_MasterRowExpanded and then add programmaticuly a handle to gridView.SelectionChanged ,this code will help you
Private Sub Grid_MasterRowExpanded(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs) Handles Grid.MasterRowExpanded
Dim view As GridView = sender
Dim detail As GridView = view.GetDetailView(e.RowHandle, e.RelationIndex)
detail.OptionsSelection.MultiSelect = True
If e.RowHandle = 0 Or e.RowHandle = 1 Then
AddHandler detail.SelectionChanged, AddressOf detail_SelectionChanged
End If
End Sub
Private Sub detail_SelectionChanged(ByVal sender As System.Object, ByVal e As DevExpress.Data.SelectionChangedEventArgs)
viewSelected = sender
Dim ro As DataRowView = viewSelected.GetFocusedRow
txtEmpId.Text = ro.Item("colEmp_Id")
End Sub

textbox value not getting another form button click event in winforms

I am working on windows form application and I have two forms. 1 is visitorinfo 2 is vistorexitsign.
In the visitorinfo I have save button, while cliking save button I want to get textboxvalue from vistirexitsign form.
Both forms are running at the same time, I have given code like this in save button of visitor info form:
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Dim obj As New VisitorExitsign
Dim vs As String = obj.txtvisitoridExit.Text
Dim Visitorid As String = My.Forms.VisitorExitsign.txtvisitoridExit.Text
But I am always getting here txtvisitoridexit.text value null. Not getting the text value.
What is wrong with my code?
You are creating new instance in every click event.
Dim obj As New VisitorExitsign
So the values are set in the new objects but not in existing object.
So actually you have to refer to existing object of VisitorExitsign.
EDIT:
For example:
You are creating form VisitorExitsign in some method.
So whenever you are creating store its reference in some global variable.
VisitorExitsign obj = new VisitorExitsign
at the place where you are creating form
then in click event use obj and assign text.
When you refer to My.Forms.VisitorExitSign.txtvisitoridExit.Text you are referencing the form itself rather than an instance of the form, if that makes sense. So, you are trying to access the default form rather than one which the user has entered text into.
What you probably want to do is to change
Dim Visitorid As String = My.Forms.VisitorExitsign.txtvisitoridExit.Text`
into
Dim Visitorid As String = obj.txtvisitoridExit.Text
What that will do is make sure that the Visitorid is getting it's value from an instance of VisitorExitSign.
Try Like This
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim forms As FormCollection = Application.OpenForms
For Each form As Form In forms
If form.Name = "VisitorExitsign" Then
Dim vs As String = CType(form, VisitorExitsign).txtvisitoridExit.Text
End If
Next
End Sub
Suggesstion:
frmVX = New VisitorExitsign
frmVX.Location = New Point(781, 0)
frmVX.MdiParent = Me
frmVX.Show()
frmVE = New VisitorInfo()
frmVE.Location = New Point(0, 0)
frmVE.MdiParent = Me
frmVE.Tag=frmVX
frmVE.Show()
Button_Click Event
Dim vs As String = CType(me.Tag, VisitorExitsign).txtvisitoridExit.Text
Hope this will works
create a module
Module modTextValue
Public _textVal As String
End Module
then goto txtvisitoridexit's LostFocus event on your form vistirexitsign
Private Sub txtvisitoridexit_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
_textVal = txtvisitoridexit.Text
End Sub
on btnSave'click
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Dim str As String
str = _textVal
End Sub
Try this :
in button save update your code to :
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Dim obj As New VisitorExitsign
obj.ShowDialog()
Dim vs As String = obj.txtvisitoridExit.Text
End sub
when you close VisitorExitsign, the variable vs will take the value of obj.txtvisitoridExit

Exposing DataGridViewComboBoxEditingControl

I would like to know how to use the DataGridViewComboBoxEditingControl with vb.net
i need a routteen to run when the user select an item from the datagridviewcomboboxcolumn that i have configured. I am unsure how to attach the object to the column i create manually
I have implemented the following from examples on the internet but this only appears to trigger when the user click on the combobox within the column.
Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
If editingComboBox IsNot Nothing Then
AddHandler editingComboBox.SelectedValueChanged, AddressOf EditingComboBox_DropDown
End If
End Sub
Private Sub EditingComboBox_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.WriteLine("A ComboBox in the DataGridView just dropped down.")
End Sub
any help would be appreciated as i cant seem to find much reference material for this
Thanks in advance
Just realised that i have never posted the answer to my question
Placed the following in the EditingControlShowing to capture request
Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
Try
If dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
Dim cmbox As ComboBox = TryCast(e.Control, ComboBox)
AddHandler cmbox.SelectionChangeCommitted, AddressOf Update_StockInfo
strSelectedText = cmbox.SelectedText
End If
Catch ex As Exception
End Try
End Sub
then this added items to the combobox cell
Private Sub Update_StockInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim cmbClickedCell = DirectCast(sender, DataGridViewComboBoxEditingControl)
Dim cmbComboBox = DirectCast(sender, ComboBox)
If dgvTicketDetail.CurrentRow.Index = cmbClickedCell.EditingControlRowIndex And dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
Debug.WriteLine(cmbClickedCell.EditingControlRowIndex & cmbComboBox.SelectedValue)
Dim dtStock As DataTable = CropTrackMod.GetWeight(cmbComboBox.SelectedValue)
Dim dgvcc As DataGridViewComboBoxCell
dgvcc = dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells(2)
dgvcc.Items.Clear()
For Each row As DataRow In dtStock.Rows
dgvcc.Items.Add(row.Item("UnitName"))
Next row
If CropTrackMod.IsStockVATAble(cmbComboBox.SelectedValue) = True Then
dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = CropTrackMod.dblVATRate
Else
dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = "0.00"
End If
End If
End Sub
It works really well for me, unfortunately the user wanted something different in the end ha. Oh well at least i learnt something

How do you get the control that was clicked to open a ContextMenuStrip?

I'm using a ContextMenuStrip for multiple controls and I'm trying to figure out the best way to get the control that was actually clicked on to open the Context Menu. The sender just gives the ToolStripMenuItem reference, which has an Owner property that references the ContextMenuStrip, but I cannot figure out how to tell which control the click came from. There must be a simple way to check this, right? I'm checking it in the ToolStripMenuItem's click event.
Friend WithEvents mnuWebCopy As System.Windows.Forms.ToolStripMenuItem
...
Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click
I found a similar post about this, but that mentions using a SourceControl property which I do not see on here.
I'm using Visual Studio 2008, VB.Net winforms.
Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click
Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)
MessageBox.Show(cms.SourceControl.Name)
End Sub
Your sender is a ToolStripMenuItem -- cast it.
Its owner is a ContextMenuStrip -- get it.
SourceControl is a property on the ContextMenuStrip and references the last control from which the ContextMenuStrip was displayed.
Private Sub kdgToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles kdgToolStripMenuItem.Click
Dim sms = (sender.GetCurrentParent()).SourceControl.name
MsgBox(sms)
End Sub
'///Faster
Private Sub cmsRightClick_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmsRightClick.MouseClick
Dim s As String = CType(sender, ContextMenuStrip).GetItemAt(CType(sender, ContextMenuStrip).DisplayRectangle.X, _
CType(sender, ContextMenuStrip).DisplayRectangle.Y + e.Y).Text.Trim()
MsgBox(s)
Select Case s
Case Is = "Select Summary Total"
Dim x = 0
Case Is = "Select Collections"
Dim x = 1
Case Is = "UnSelect"
Dim x = 2
Case Is = "Reconcile"
Dim x = 3
Case Is = "Undo Reconciliation"
Dim x = 4
End Select
End Sub
On VB.NET 2013 this work so fine:
Dim cms As ContextMenuStrip = CType(sender, ContextMenuStrip)
MessageBox.Show(cms.SourceControl.Name)