2

I've been dealing with this for hours!!! I have 2 forms: in one form (form1) I have my layout with textboxes, etc... in the other (datagrid_form2) I have a datagridview where I choose the item, with a doubleclickcell event, to be loaded in a specific textbox of the first form (form1).

I have a button next to the textbox of the form1 that call the datagrid_form2, once the element in the datagrid_form2 is chosen the textbox of the form1 is loaded with that value.

  Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick

    Dim form1panel As New form1

    form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString

    Debug.WriteLine(form1panel.txtmybox.Text )

    Me.Close()

End Sub

As you can see I have the cellDoubleclick event that should load the value of the selected cell into the textbox of my form1, but it doesn't display anything in the textbox(txtmybox). in the debug the value is chosen correctly, so is not a problem of code, simply the value is not being passed at the textbox.

Any ideas? hints?

thanks in advance

p.s. I'm working with visual studio 2010 .vb project!

dromeo
  • 33
  • 1
  • 7

3 Answers3

0

It seems that you are messing with the forms.
You are creating a new instance of Form1 but you don't show it.
I suggest to read this.
Also your question is similar to this

Edit: It is not clear from your questions how you want to achieve what you ask. You have a form with a dataGridView (I name it frmDgv) and a second form (form1) that you want to show the cell content from yours datagrid. Is this form (form1) already opened? or you want to open a new one each time that you double click? And if you want to open it each time you want multiple instances or one in modal ?
So I'll try to include everything:

->The form will open each time
frmDgv

Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As      System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick

   Dim f1 as new form1
   f1=DirectCast(mLinkForm1,Form1)
   f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
   'If you want to open a Form1 each time you double click in an cell
    f1.Show 
   'If you want a modal style info
   'f1.ShowDialog
   'f1.Dispose
End Sub

->The form is already open (I wouldn't follow this)
frmDgv

   Private mLinkForm1 As Form1
Public Property LinkForm1 
    Get
        Return mLinkForm1 
    End Get
    Set(value)
        mLinkForm1  = value
    End Set
End Property

'When you open this form for first time you will set:

 Dim f1 as new Form1
 mLinkForm1=f1
 f1.Show

Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As      System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
   Dim f1 as new form1
   f1=DirectCast(mLinkForm1,Form1)
   f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
End Sub

Edit 3 (I don't have Visual Studio right now , so my code is not tested)
Form: datagridview

 Private mLinkForm1 As Form1
   Public Property LinkForm1 
     Get
        Return mLinkForm1 
     End Get
     Set(value)
        mLinkForm1  = value
     End Set
End Property

Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As      System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
    LinkForm1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
    Me.Close()
End Sub

Form: Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    datagridview.LinkForm1=Me
    datagridview.Show
End Sub

Try this and inform me.

Community
  • 1
  • 1
Nianios
  • 1,391
  • 3
  • 20
  • 45
  • Hello Nianos, thanks for your good reply. In fact I was trying with the public property... to set a variable as string and to return is value and then to set the variable = texbox.text. The problem for me is the event (the double click cell).. let's say for example I declare in my form1: `Public Property get_values Get Return values End Get Set(ByVal value) values = value End Set End Property` and in the form2 `form1panel.get_values = mydata.SelectedCells.Item(0).Value.ToString`. Even if I set get_values = textbox.text is not working – dromeo May 16 '13 at 05:09
  • Hello Nianos, in fact what I want to achieve is the opposite. I have my form1 opened (with the layout and the textbox to be fill), and in the form1(next to the textbox) I have a button to call my datagrid `(datagrid.show())` and once the element in the datagrid is clicked, the textbox in the form1 is filled with data form the datagridview. – dromeo May 16 '13 at 15:01
  • Ok, you can use the same logic. Are you create a new instance of datagrid (I mean the form) each time you click the button (dim datagridForm as new datagrid - datagridForm.Show) , you work it as modal ( dim datagridForm as new datagrid - datagridForm.ShowDialog) or you open the actual form (datagrid.Show)?I fyou answer to my question I can provide a similar solution – Nianios May 16 '13 at 16:26
  • Hey, thanks for the quick reply!! I open the actual form (datagrid.show), with a button placed in the form1. – dromeo May 16 '13 at 16:44
  • ok, but the question is how you want this form to behave?(Open once,open each time with different instances,open the actual form?) – Nianios May 17 '13 at 07:04
  • No, just open the actual form! I only need to open the actual form (of the datagridview). In the datagrid I already have the code to load all the data from the database. `da = New MySqlDataAdapter("select * from mydata", connection) dt = New DataTable da.Fill(dt) data.DataSource = dt` and when I select an item of the datagrid this will have to be loaded in the textbox of the form1 (the main form with layout and textboxes)...thanks!!! – dromeo May 17 '13 at 16:30
  • Perfect!!! It's working...thanks for your patience. The only thing to be corrected on the code is `LinkForm1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString`. The right one is: `mLinkForm1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString`. The other thing I didn't understand too much is why you had to equal the datagridview to the form1?? `datagridview.LinkForm1=Me` again: thank you!! – dromeo May 20 '13 at 14:58
  • @Dromeo. You don't equal the two Forms(this would be smthng like datagridview=me). You have just a property to say that your datagridView form is linked to a form1. Have a nice day. – Nianios May 21 '13 at 08:41
0

Change this part

form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString

to

form1panel.txtmybox.Text = data.CurrentCell.Value

This is for string value ...

matzone
  • 5,703
  • 3
  • 17
  • 20
  • I did it but still nothing! :-( although the value is the right one (in the debug output) the textbox does not seem to load the value. thanks!! – dromeo May 15 '13 at 15:02
  • it says 3 and the length is correct also. I have 3 numbers in the item selected. Is like the textbox is not called in the right way so the value is not loaded. But I don't understand way? – dromeo May 15 '13 at 17:16
  • @dromeo : have you figured out this question ? – matzone May 16 '13 at 09:06
  • mmhh not really, because I don't think is a matter of property. At the beginning I thought this could be the problem (the properties of the textbox placed in the form1)... but now, I really think that I'm not making reference of my textbox in the right way. – dromeo May 16 '13 at 15:08
  • At least when you did checked txtmybox.len before and after have a different value, meant that the reference were right .. – matzone May 16 '13 at 16:13
0

You need to show your child form:

Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As 
     System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick

    Dim form1panel As New form1
        form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
        Debug.WriteLine(form1panel.txtmybox.Text)

        form1panel.Show();
        //Me.Close()

    End Sub
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
  • Hi skumar, thanks for your reply. I don't understand why I need to show the child form, cause the value should be loaded in the other form(in the textbox of the other form1), once I double click the cell of the datagridview. Anyway I tried your advice but still nothing! the value is not displayed. This is very strange!! – dromeo May 15 '13 at 14:42