0
votes

I am new to VB and .NET, and I am trying to display a message box when the user double clicks on a data grid, to display selected data.

Normally I can just use MessageBox("Hello") from windows control with the button click event.

But when I type MessageBox("Hello") from datagridview's double click event, I get a compile error

'MessageBox' is a class type and cannot be used as an expression.

I also tried to create a second blank form on visual studio, on button click event I typed in: Form2.Show(), and I did the same on datagridview double click event. When I click the button Form2 is displayed but when I double click on datagridview, Form2 is not displayed.

Any reason why datagridview control behave differently than other windows control?, and what is the best and easy way to display pop-up windows or display 2nd form when user double click on datagridview?

What I am trying to do is when the user double clicks on datagridview, I would like to create a pop-up form, and fill in some of the data from selected datagridview row on the pop-up form.


I am not sure how to reply to Alex suggestion, I try to add comment but it says more than 125 characters and I could not find button to reply the thread, so I just put it here. (let me know if there is better way to do this instead of keep adding to my questions).

Hi Alex,

this is the code that I have on form1 class, form2 class is just a simple blank form, I just create add new form right click add form from Visual Studio. After I click the button form2 is pop up then I close it Form2, then double click datagridview cell, nothing happen, the I click the button form2 is pop up.

Thank you.

Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'TestDB1DataSet.t_emp' table. You can move, or remove it, as needed. Me.T_empTableAdapter.Fill(Me.TestDB1DataSet.t_emp)

End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    ' put some code here
    Form2.Show()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.Show()

End Sub

End Class

1
Can you post some example of what are you doing?Jose Guilherme Monteiro
MessageBox is is a type. Use the .Show() method: MessageBox.Show(...)Ňɏssa Pøngjǣrdenlarp
Use MsgBox("Hello") insteadSSS
A VERY late reply and it's probably too late... but I couldn't understand what you said in that edit - is the problem solved or is it still persisting? I don't imagine it matters though.ABPerson

1 Answers

1
votes

These are very easy to make mistakes:

  1. The MessageBox("Hello") should be MessageBox.Show("Hello") - as simple as that. MessageBox is a type; Show is the method that you call.

  2. The reason, I believe, the double click event on the datagridview did not work is because you need to check if they are clicking on the cells as well as checking if they double click on the grey area of it (which is what the double click method does!) So use this code here - be sure to replace 'Datagridview1' in the part after where it says Handles to whatever the name of the DataGridView is:

    Private Sub DataGridView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseDoubleClick
        DoubleClickDataGrid()
    End Sub
    
    Private Sub TYPEANYNAMEHERE(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
    DoubleClickDataGrid()
    End Sub
    
    Sub DoubleClickDataGrid()
        ' Type the code here
    End Sub
    

Whatever code you want it to do when you double click will go where it says 'Type the code here'

(For Example: Form2.Show or MessageBox.Show("Hello")) etc.