 |
|
[VB.NET] Datagridview ส่งค่าข้ามฟอร์มไปยัง textbox ของอีกฟอร์มไม่ได้ |
|
 |
|
|
 |
 |
|
ประกาศตัวแปรใน form 2 ให้เป็น public หรือ ตัวแปรประเภท property ให้เป็น public ก็ได้แล้วครับ
|
 |
 |
 |
 |
Date :
2012-11-26 11:15:42 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่างไงหรอครับ งง
|
 |
 |
 |
 |
Date :
2012-11-26 11:37:35 |
By :
หนึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Property Statement
Code (VB.NET)
//อันนี้เป็นตัวแปรแบบ ปกติที่ใช้กัน
public AA as string
|
 |
 |
 |
 |
Date :
2012-11-26 11:50:30 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ delegate เป็น
ก็ส่งค่าด้วย delegate ก็ได้
|
 |
 |
 |
 |
Date :
2012-11-26 12:39:54 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนยกตัวอย่าง จากที่ผมถามไว้ได้ไหมครับ พอดีมือใหม่
|
 |
 |
 |
 |
Date :
2012-11-26 12:52:09 |
By :
หนึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนด้วยครับ
|
 |
 |
 |
 |
Date :
2012-11-27 08:01:25 |
By :
หนึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขั้นแรกสร้าง class event argument ขึ้นมาใช้ (อยากได้ properties อะไรก็เขียนเอา)
จากที่เขียนไว้คุณต้องการค่าจาก Cells(0) กับ Cells(1) ซึ่งผมไม่รู้ว่าชื่ออะไร
งั้นผมเขียนเป็นกลางๆ ไว้แล้วกัน
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public delegate void ChooseEventHandler(object source, ProcessCompleteEventArgs e);
public class ChooseEventArgs : EventArgs
{
private string _cell0;
private string _cell1;
public ProcessCompleteEventArgs(string cell0, string cell1)
{
_cell0 = cell0;
_cell1 = cell1;
}
public string Cell0Value
{
get { return _cell0; }
}
public string Cell1Value
{
get { return _cell1; }
}
}
จากนั้นไปทำที่ form3 เอาโค้ดด้านล่างไปใส่
Code (C#)
public event ChooseEventHandler OnChoose;
private void DataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.RowIndex != -1)
{
// โค้ดตรงนี้ประมาณว่าเกิด choose event
if (OnChoose != null)
{
OnChoose(this, new ChooseEventArgs(dgv.Rows[e.RowIndex].Cells[0].Value.ToString(), dgv.Rows[e.RowIndex].Cells[1].Value.ToString()));
}
}
}
แล้วก็กลับไปทำที่ form 2 เอาโค้ดด้านล่างไปใส่
Code (C#)
private void Button1_Click(object sender, EventArgs e)
{
choose.OnChoose += new ChooseEventHandler(choose_OnChoose);
}
private void choose_OnChoose(object sender, ChooseEventArgs e)
{
// เอาค่าไปใช้
TextBox1.Text = e.Cell0Value;
TextBox2.Text = e.Cell1Value;
}
|
 |
 |
 |
 |
Date :
2012-11-27 09:12:34 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้โค้ด form2 หน่อย ตกไปบรรทัดนึง (เขียนสด)
Code (C#)
private void Button1_Click(object sender, EventArgs e)
{
Choose choose = new Choose(); // <-- มันน่าจะมีตรงนี้ด้วย
choose.OnChoose += new ChooseEventHandler(choose_OnChoose);
choose.ShowDialog(); // <-- เขียนตกไป
}
private void choose_OnChoose(object sender, ChooseEventArgs e)
{
// เอาค่าไปใช้
TextBox1.Text = e.Cell0Value;
TextBox2.Text = e.Cell1Value;
}
|
 |
 |
 |
 |
Date :
2012-11-27 09:15:53 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โอ้ยๆ แก้ class event ด้วยเขียนผิด
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public delegate void ChooseEventHandler(object source, ProcessCompleteEventArgs e);
public class ChooseEventArgs : EventArgs
{
private string _cell0;
private string _cell1;
public ChooseEventArgs(string cell0, string cell1) // <-- นี่ไงปล่อยไก่ไปแล้ว
{
_cell0 = cell0;
_cell1 = cell1;
}
public string Cell0Value
{
get { return _cell0; }
}
public string Cell1Value
{
get { return _cell1; }
}
}
|
 |
 |
 |
 |
Date :
2012-11-27 09:18:13 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โทษทีผิดอีกที (จะโดนข้อหา spam ไหมหว่า)
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public delegate void ChooseEventHandler(object source, ChooseEventArgs e); // <-- แก้แล้ว
public class ChooseEventArgs : EventArgs
{
private string _cell0;
private string _cell1;
public ChooseEventArgs(string cell0, string cell1) // <-- นี่ไงปล่อยไก่ไปแล้ว
{
_cell0 = cell0;
_cell1 = cell1;
}
public string Cell0Value
{
get { return _cell0; }
}
public string Cell1Value
{
get { return _cell1; }
}
}
|
 |
 |
 |
 |
Date :
2012-11-27 09:20:18 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|