Reputation: 113
Suppose I have two forms i.e Form1
and Form2
. Form1 contains dynamically created DataGridView
control and Form2
contains two Textbox
controls (Textbox1
and Textbox2)
and a Button
control.
When I DoubleClick
on the DataGridView
's cell it will open Form2
and the data from current selected cell passes to Form2
's Textbox1
Here is the code:
I Added a handlear to my dynamically created DatagridView like this:
AddHandler dg.CellMouseDoubleClick, AddressOf dg_DataGridEdit
Private Sub dg_DataGridEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Dim dgView As DataGridView = DirectCast(sender, DataGridView) Form2.TextBox1.Text = dgView.CurrentCell.Value.ToString() Form2.ShowDialog() End Sub
When i click on the button from Form2, the value of current selected cell will change like TextBox2 from Form2 has. But the problem is i can't use the code from button1 from From2 like
Form1.dgView.CurrentCell.Value = TextBox2.Text
How can i pass value from textbox2 to current selected cell?
Upvotes: 0
Views: 3707
Reputation: 3940
You may pass the datagridview of Form1
to Form2
through Form2
's constructor. Then you'll be able to access the CurrentCell
of the datagridview of Form1
as like your last code snippet.
You can have a constructor in Form2
which'll take the datagridview as argument.
Public Class Form2
Private dgView As New DataGridView
Public Sub New(ByRef _dgView As DataGridView)
dgView = _dgView
End Sub
End Class
When you create the instance of Form2
in Form1
, pass the dgView to Form2 like:
Public Class Form1
Dim Form2 As New Form2(dgView)
End Class
Now when you click the button in Form2
, just set the CurrentCell
of the dgView in the button event handler like:
dgView.CurrentCell.Value = TextBox2.Text
Upvotes: 0
Reputation: 11773
I would not use the default instances. Here is an example that uses a textbox on each form:
form1
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form2(TextBox1F1) 'pass ref to form2
f.ShowDialog()
End Sub
End Class
form2
Public Class Form2
Dim txtbox As TextBox
Public Sub New(tb As TextBox)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
txtbox = tb 'get ref from calling form
End Sub
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TextBox1F2.Text = txtbox.Text
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1F2.TextChanged
txtbox.Text = TextBox1F2.Text
End Sub
End Class
Any changes to the textbox on form2 will be reflected in the textbox on form1.
Upvotes: 1
Reputation: 113
Ok I got it. At first i have created a public variable called
Public UpdateData As String = ""
Then change the code-
Private Sub dg_DataGridEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Dim dgView As DataGridView = DirectCast(sender, DataGridView) Form2.TextBox1.Text = dgView.CurrentCell.Value.ToString() Form2.ShowDialog() dgView.CurrentCell.Value =UpdateData UpdateData="" End Sub
In form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click form1.UpdateData = TextBox2.Text Me.Hide() End Sub
Upvotes: 0
Reputation: 27322
When you create your DataGridView, store a reference to it:
private _myDgv as DataGridView
Sub Form_load
_myDgv = New dataGridView
Me.Controls.Add(_myDgv)
'etc.
End Sub
Then Add a ReadOnly Property to get a reference to it from elsewhere:
Public ReadOnly Property DynamicDgv As DataGridView
Get
Return _myDgv
End Get
End Property
Then you can do this in Form2:
Form1.DynamicDgv.CurrentCell.Value = TextBox2.Text
Upvotes: 1
Reputation: 1670
Try copying to clipboard as a workaround so you don't have to pass the text from one method to the next
System.Windows.Forms.Clipboard.SetText(...)
System.Windows.Forms.Clipboard.GetText(...)
Upvotes: 0