public string ReturnValue1 {get;set;}
public string ReturnValue2 {get;set;}
then set this inside your sub-form ok button click handler
private void btnOk_Click(object sender,EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.Close();
}
ข้างล่างนี่คือคำสั่งจากฟอร์มที่เรียกใช้งาน
using (var form = new frmImportContact())
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
string val = form.ReturnValue1; //values preserved after close
string dateString = form.ReturnValue2;
//Do something here with these values
//for example
this.txtSomething.Text = val;
}
}
Public Property ReturnValue1() As String
Get
Return m_ReturnValue1
End Get
Set
m_ReturnValue1 = Value
End Set
End Property
Private m_ReturnValue1 As String
Public Property ReturnValue2() As String
Get
Return m_ReturnValue2
End Get
Set
m_ReturnValue2 = Value
End Set
End Property
Private m_ReturnValue2 As String
Private Sub btnOk_Click(sender As Object, e As EventArgs)
Me.ReturnValue1 = "Something"
Me.ReturnValue2 = DateTime.Now.ToString()
'example
Me.Close()
End Sub
Code (VB.NET)
Using form = New frmImportContact()
Dim result = form.ShowDialog()
If result = DialogResult.OK Then
Dim val As String = form.ReturnValue1
'values preserved after close
Dim dateString As String = form.ReturnValue2
'Do something here with these values
'for example
Me.txtSomething.Text = val
End If
End Using
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
If e.RowIndex = -1 Then Exit Sub
With DataGridView1
Rent.txtMemID.Text = .Rows(e.RowIndex).Cells(0).Value.ToString
Rent.cboTitle.Text = .Rows(e.RowIndex).Cells(1).Value.ToString
Rent.txtName.Text = .Rows(e.RowIndex).Cells(2).Value.ToString
Rent.txtLName.Text = .Rows(e.RowIndex).Cells(3).Value.ToString
Rent.txtMemIDCard.Text = .Rows(e.RowIndex).Cells(4).Value.ToString
Rent.txtAddress.Text = .Rows(e.RowIndex).Cells(5).Value.ToString
Rent.txtPhoneNumber.Text = .Rows(e.RowIndex).Cells(6).Value.ToString
Rent.txtEMail.Text = .Rows(e.RowIndex).Cells(7).Value.ToString
'สร้างไว้ส่งค่าไปยังอีกฟอร์ม
End With
Me.Close()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim r As New SMem
r.ShowDialog(DataGridView1) 'เปิดฟอร์มค้นหาพร้อมทั้งเรียกใช้การค้นหาจาก datagridview1 จากฟอร์มดังกล่าว
End Sub
Module Module1
Public connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DATA\PostCode.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091"
Public Function GetTableForm(_SQL As String) As System.Data.DataTable
Dim ds As New System.Data.DataSet()
Dim conn1 As New System.Data.OleDb.OleDbConnection(connectionString)
conn1.Open()
Dim adapter1 As New System.Data.OleDb.OleDbDataAdapter(_SQL, conn1)
adapter1.Fill(ds)
Return ds.Tables(0)
End Function
End Module
Code (VB.NET)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindingSource1.DataSource = GetTableForm("SELECT * FROM Sett_Post_Province")
ProvinceTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Province", True))
PvnIDTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "pvnID", True))
SymbolTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Symbol", True))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form2
f.ShowDialog()
If (f.PvItem <> 0) Then
Me.BindingSource1.Filter = "PvnID = " & f.PvItem
End If
End Sub
End Class
Code (VB.NET)
Public Class Form2
Public PvItem As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = GetTableForm("SELECT * FROM Sett_Post_Province").DefaultView
End Sub
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
PvItem = Integer.Parse(DataGridView1(0, e.RowIndex).Value)
Close()
End Sub
End Class
หลักการบ้านที่ผมใช้
-ยัด data มารวมใน Module เพื่อให้เรียกใช้ได้ทุก class
-Form2 จะมีตัวแปร Public มาใช้งานอาจจะเป็น Int list array แล้วแต่ศรัทธา
ที่นำเสนอแบบนี้เพราะสามารถให้ user ใส่ SQL ใน DataGridView ง่ายๆได้สะดวกในการค้นหา
-Form1 เรียกใช้ Form2 แบบ ShowDialog แล้วเอาตัวแปร Public Form2 มาใช้
Module Module1
Public connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DATA\PostCode.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091"
Public Function GetTableForm(_SQL As String) As System.Data.DataTable
Dim ds As New System.Data.DataSet()
Dim conn1 As New System.Data.OleDb.OleDbConnection(connectionString)
conn1.Open()
Dim adapter1 As New System.Data.OleDb.OleDbDataAdapter(_SQL, conn1)
adapter1.Fill(ds)
Return ds.Tables(0)
End Function
End Module
Code (VB.NET)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindingSource1.DataSource = GetTableForm("SELECT * FROM Sett_Post_Province")
ProvinceTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Province", True))
PvnIDTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "pvnID", True))
SymbolTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Symbol", True))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form2
f.ShowDialog()
If (f.PvItem <> 0) Then
Me.BindingSource1.Filter = "PvnID = " & f.PvItem
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim f As New Form2
f.ShowDialog()
For i As Integer = 0 To f.DataGridView1.RowCount - 1
If Convert.ToBoolean(f.DataGridView1(0, i).Value) = True Then
DataGridView1.Rows.Add(f.DataGridView1(1, i).Value, f.DataGridView1(2, i).Value, f.DataGridView1(3, i).Value)
End If
Next
End Sub
End Class
Code (VB.NET)
Public Class Form2
Public PvItem As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = GetTableForm("SELECT * FROM Sett_Post_Province").DefaultView
End Sub
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
PvItem = Integer.Parse(DataGridView1(0, e.RowIndex).Value)
Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
End Class
หลักการบ้านที่ผมใช้
-ยัด data มารวมใน Module เพื่อให้เรียกใช้ได้ทุก class
-Form2 จะมีตัวแปร Public DataGridView1 แล้วเพ่ม DataGridViewCheckBoxColumn เพื่อให้ Form1 เรียกใช้ได้
-Form1 เรียกใช้ Form2 แบบ ShowDialog แล้วเอาตัวแปร Public Form2 มาใช้
-พอ Form1 เปด Form2 มาเลือกแล้วปด Form2 ก็ loop มาเพื่อเชคและเพ่มค่าที่ selected ไว้