Click here to Skip to main content
15,903,388 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Help Needed !!! URGENT Pin
Dave Kreskowiak17-May-05 9:31
mveDave Kreskowiak17-May-05 9:31 
GeneralRe: Help Needed !!! URGENT Pin
Rizwan Bashir17-May-05 21:15
Rizwan Bashir17-May-05 21:15 
Generalmouse wheel disable Pin
kalyanramu17-May-05 7:03
kalyanramu17-May-05 7:03 
GeneralRe: mouse wheel disable Pin
Christian Graus17-May-05 12:49
protectorChristian Graus17-May-05 12:49 
Generaldatagrid Pin
beginnervb.net17-May-05 6:49
beginnervb.net17-May-05 6:49 
GeneralRe: datagrid Pin
Fernando Soto17-May-05 8:18
Fernando Soto17-May-05 8:18 
GeneralRe: datagrid Pin
beginnervb.net118-May-05 19:28
beginnervb.net118-May-05 19:28 
GeneralRe: datagrid Pin
Fernando Soto19-May-05 3:34
Fernando Soto19-May-05 3:34 
The way to do what you want is by using delegates. In Form1 where the DataGrid is you add a handler that tells Form2 the address of the sub that will update the text boxes. You also have to write that sub. In Form2 you define an event that has the same signature as the subroutine you created in Form1. In the button click event you raise the event you defined passing in the text boxes to be updated. See the code below.

Public Class Form1
Inherits System.Windows.Forms.Form

Private f2 As New Form2
Private dt As New DataTable("Four Columns")
Private dr As DataRow
Private dc As DataColumn

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(24, 16)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(480, 176)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(544, 273)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load


' Add columns
dc = New DataColumn
With dc
.ColumnName = "Column 1"
.DataType = System.Type.GetType("System.String")
.ReadOnly = True
End With
dt.Columns.Add(dc)

dc = New DataColumn
With dc
.ColumnName = "Column 2"
.DataType = System.Type.GetType("System.String")
.ReadOnly = True
End With
dt.Columns.Add(dc)

dc = New DataColumn
With dc
.ColumnName = "Column 3"
.DataType = System.Type.GetType("System.String")
.ReadOnly = True
End With
dt.Columns.Add(dc)

dc = New DataColumn
With dc
.ColumnName = "Column 4"
.DataType = System.Type.GetType("System.String")
.ReadOnly = True
End With
dt.Columns.Add(dc)

' Connect Datagrid to table
DataGrid1.DataSource = dt.DefaultView

' Hard code DataRow
dr = dt.NewRow
dr(0) = "Saverio"
dr(1) = "1962"
dr(2) = "45"
dr(3) = "USC"
dt.Rows.Add(dr)

dr = dt.NewRow
dr(0) = "Jonathan"
dr(1) = "1983"
dr(2) = "22"
dr(3) = "Routers"
dt.Rows.Add(dr)

dr = dt.NewRow
dr(0) = "Jose"
dr(1) = "1947"
dr(2) = "58"
dr(3) = "US Navy"
dt.Rows.Add(dr)

f2.Show()
' Add the event handler that communicates form2
AddHandler f2.TransferData, AddressOf SendDataToForm

End Sub

' Sends the data to form2 when the button on form2 is clicked
Private Sub SendDataToForm(ByRef tb1 As TextBox, ByRef tb2 As TextBox, _
ByRef tb3 As TextBox, ByRef tb4 As TextBox)

' Set the textboxes in form2 with the currently selected row
' in the datagrid
tb1.Text = dt.Rows(DataGrid1.CurrentRowIndex).ItemArray(0).ToString()
tb2.Text = dt.Rows(DataGrid1.CurrentRowIndex).ItemArray(1).ToString()
tb3.Text = dt.Rows(DataGrid1.CurrentRowIndex).ItemArray(2).ToString()
tb4.Text = dt.Rows(DataGrid1.CurrentRowIndex).ItemArray(3).ToString()

End Sub

End Class

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(152, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(152, 72)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = ""
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(152, 112)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = ""
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(152, 152)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.TabIndex = 3
Me.TextBox4.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(32, 88)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 4
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

' The event that is raised when the button is clicked
Public Event TransferData(ByRef tb1 As TextBox, ByRef tb2 As TextBox, _
ByRef tb3 As TextBox, ByRef tb4 As TextBox)

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

' Semds the reference of the four textboxs to form1 and fills them
' they are sent ByRef so what form1 puts in to them will be displayed
' automatically
RaiseEvent TransferData(TextBox1, TextBox2, TextBox3, TextBox4)

End Sub

End Class

I hope that this is of some help

GeneralRe: datagrid Pin
rudy.net17-May-05 18:29
rudy.net17-May-05 18:29 
GeneralRe: datagrid Pin
Josue Avila19-May-05 9:37
Josue Avila19-May-05 9:37 
QuestionPrinting - OWC Pivot Tables Using VB6 ? Pin
J.Jay17-May-05 6:42
J.Jay17-May-05 6:42 
GeneralHelp In switching between forms Pin
GegoTheWizard17-May-05 4:53
GegoTheWizard17-May-05 4:53 
GeneralRe: Help In switching between forms Pin
rudy.net17-May-05 18:48
rudy.net17-May-05 18:48 
Generalmoney masked textBox Pin
hakanaktan17-May-05 3:31
hakanaktan17-May-05 3:31 
GeneralRe: money masked textBox Pin
Christian Graus17-May-05 13:39
protectorChristian Graus17-May-05 13:39 
GeneralException comes in setpixel in VB .Net Pin
meghadwivedi17-May-05 1:44
meghadwivedi17-May-05 1:44 
GeneralMonitor 3rd party app (hooking/subclassing) Pin
Derckie217-May-05 0:53
Derckie217-May-05 0:53 
GeneralRe: Monitor 3rd party app (hooking/subclassing) Pin
rudy.net17-May-05 19:42
rudy.net17-May-05 19:42 
GeneralRe: Monitor 3rd party app (hooking/subclassing) Pin
Derckie217-May-05 21:01
Derckie217-May-05 21:01 
GeneralRe: Monitor 3rd party app (hooking/subclassing) Pin
Tim McCurdy17-May-05 23:27
Tim McCurdy17-May-05 23:27 
GeneralRe: Monitor 3rd party app (hooking/subclassing) Pin
Derckie217-May-05 23:43
Derckie217-May-05 23:43 
GeneralData Grid Control Pin
meetaqadir17-May-05 0:34
meetaqadir17-May-05 0:34 
Generalsimple datagrid code Pin
hakanaktan17-May-05 3:45
hakanaktan17-May-05 3:45 
Generalremote configuration file ??? and why to use vbc compilation Pin
Rizwan Bashir17-May-05 0:02
Rizwan Bashir17-May-05 0:02 
GeneralCrystal Report Landscape Mode Pin
dpagka16-May-05 23:11
dpagka16-May-05 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.