Datagridview - Focus on Cell that was right clicked - vb.net

I have a datagridview that I have put a ContextMenuStrip1 on. I would like it to remove a row in the datagridview when the row is right clicked on and they click on "delete row". I have the delete working and the menu is showing up but this isn't firing when you right click on the datagridview.
This is where I am setting the row to edit:
Private Sub ModifyRowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyRowToolStripMenuItem.Click
If Not datagridview_TagAssignment.CurrentRow Is Nothing Then
datagridview_TagAssignment.CurrentCell = datagridview_TagAssignment.Item(0, datagridview_TagAssignment.CurrentRow.Index)
datagridview_TagAssignment.BeginEdit(True)
End If
End Sub
I am always ending up on row(0) and never the row I right clicked on.
Private Sub datagridview_TagAssignment_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles datagridview_TagAssignment.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
datagridview_TagAssignment.Rows(e.RowIndex).Selected = True
End If
End Sub
Anyone have any suggestions?

Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
ContextMenuStrip1.Items.Add(rowClicked.ToString)
ContextMenuStrip1.Show(DataGridView1, e.Location)
ContextMenuStrip1.Items.Clear()
End If
End Sub
Edit: Updated to handle a context menu strip.
That should give you the row index of the row that was right clicked using the mouse coordinates. Which should let you delete the row based on knowing the index.
Edit
Per Your comment on it not working this is my code
I have a Solution with a WinForm with a dataGridView added to it. and this is the code in the form.
Public Class Form1
Dim bindS As New BindingSource
Dim rowClicked As Integer
Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
ContextMenuStrip1.Items.Add(rowClicked.ToString)
ContextMenuStrip1.Show(DataGridView1, e.Location)
ContextMenuStrip1.Items.Clear()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s As New List(Of String)
s.Add("String one")
s.Add("String Two")
bindS.DataSource = s
DataGridView1.DataSource = bindS
End Sub
End Class
Right Clicking on a row shows the correct row index
Make sure that the event args that you are handling are the System.Windows.Forms.MouseEventArgs I noticed that you're handling the cell click

Related

Latency picking up click event

I am having a little problem with latency when I check a checkbox on and try to drag and drop. When I select one checkbox and try to move it over it won't move. If I have click that checkbox and click on a different row then try to move it will work. It works the same no matter how many I check it won't get the newest row without clicking somewhere else first. Do I need to add another event to handle or pick up that the box now has been checked?
Private Sub datagridview_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseDown
mouseDownPosition = e.Location
End Sub
Private Sub datagridview_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseMove
If CheckMouseMovement(sender, datagridview, e) Then
listofBuilds = New List(Of Build)
For Each row As DataGridViewRow In dataGridView.Rows
If Convert.ToBoolean(row.Cells.Item(0).Value) Then
Dim t As Build = DirectCast(row.DataBoundItem, Build)
listofBuilds.Add(t)
End If
Next
If listofBuilds.Count > 0 Then
dataGridView.EndEdit()
dataGridView.DoDragDrop(sender, dropEffect)
End If
End If
End Sub
Private Sub TabControl_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragEnter
e.Effect = DragDropEffects.All
End Sub
Private Sub TabControl_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragDrop
Dim DropPage As TabPage = GetTabPageByTab(TabControl.PointToClient(New Point(e.X, e.Y)))
If DropPage IsNot TabControl.SelectedTab Then
If DropPage Is Page1 Then
If DropPage Is Page2 Then
If DropPage Is Page3 Then
//etc
End If
End If
End Sub
Private Function GetTabPageByTab(ByVal point As Point) As TabPage
For i As Integer = 0 To TabControl.TabPages.Count - 1
If TabControl.GetTabRect(i).Contains(point) Then
Return TabControl.TabPages.Item(i)
End If
Next
Return Nothing
End Function
Adding a call to datagridview.EndEdit() at the beginning of the datagridview_MouseMove method will commit the current edit operation and update the source data so that you can see the updated value in your code.

vb2010 Getting Button Name on MouseDown Event

How do I capture the name of a button so I can use it in another form to query a database. I am new to vb and still at the very early learning stage so any help would be greatfully appreciated.
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
frmRacks
Here is where I need to capture name to query database
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim name As String = DirectCast(sender, Button).Name
End Sub
There are many ways to do this, one is to declare public field/variable in frmRack, another is using ShowDialog instead of Show:
I'll go with the first one (public) first:
Public buttonName as String
And in the button click from your frmMain you pass the value like:
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.buttonName = "btnA" ' Or you could use DirectCast as proposed by dbasnett
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
And then in Loading your frmRacks you have now the option of assigning button Name, like:
Private Sub frmRacks_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim query as String = "SELECT * FROM " + buttonName 'This is only an example you could make your own here
End Sub
It should be MouseButtons.Left instead of Windows.Forms.MouseButtons.Left
If e.Button = MouseButtons.Left Then
MsgBox("Left Button Clicked") 'OR WHATEVER YOU WANT IT TO DO?!
End If

Values are not populating in the Form

Using VB.Net (Windows Application)
In the form (called as FirstForm), i am using textbox, add-form Button, search button.
When i click the add-form button, it will give the new form (same as FirstForm)
Code for adding new form
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim SecondForm As New FirstForm
SecondForm.Show()
End Sub
Search Button Code
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
If FirstForm.Focus = True Then
FirstForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
Else
Dim SecondForm As New FirstForm
SecondForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
End Sub
The above code is working, but If i am in second Form when i click the search button and selected the value, then the value is appearing in the FirstForm textbox, it is not appearing in the SecondForm textbox.
If SecondForm is showing, the selected Value should appear in the SecondForm textbox not in the FirstForm Textbox.
How to solve this issue.
Need Vb.net code Help
Use Me - Reference variable which hold ref. of current form.
Dim frm As FirstForm
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(frm) OrElse frm.IsDisposed Then
frm = New FirstForm
End If
frm.Show()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub
Using "me" will not solve the problem?? why are you referring to the form in a static way?
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub

Drag drop a usercontrol in vb.net

I have, what should be , a very simple problem, but now I have used 5 hours without results.
I have a usercontrol, UserControl1, which I want to drag and drop on my form, Form1.
That’s it. It should be simple, but I have googled for hours without results. Does anybody have a sample code to fix this?
I dont know what a user control is (I'm still learning) but I found something that might help.
In this code, add two TextBox controls to a form and set the AllowDrop property of the second TextBox control to True.
Then use this code to enable drag and drop
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
I hope that you can use this for a usercontrol. Good luck and comment!
Here is the code, I have used, to get it work.
Now I have a form, Form1, and a usercontrol, Usercontrol1. To drag the usercontrol, I inserted a panel in the top of the usercontrol, and only if the user pressed the panel (panel1), the control should to move - like normal windows forms.
Public Class UserControl1
Shared mypositionX As Integer
Shared mypositionY As Integer
Shared mBlnFormDragging As Boolean
Shared drawBeginX As Integer
Shared drawBeginY As Integer
Shared drawing As Boolean
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If mBlnFormDragging = True Then
Dim position As Point = Form1.PointToClient(MousePosition)
Me.Location = New Point(position)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = False
Dim position As Point = Form1.PointToClient(MousePosition)
Location = New Point(position)
End Sub
Public Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
'Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = True
End Sub