mình có hai form 1 và 2 khi run form 1 chạy trước và gọi form 2 show.
vấn đề cần giải quyết từ form 2 làm sao có thể truy cập vào thuộc tính của các đối tượng ở form 1. chẳng hạn như từ form 2 kiểm tra checkbox ở form 1 có checked hay ko?
có bạn nào biết thì giúp mình nhé. thanks.
Sau đây là ví dụ dùng property. Giả sử ban đầu mình có Form1 và Form2. Trong Form1 mình thêm 1 cái textbox,1 cái button, trong Form2 mình thêm 1 cái textbox. Mục đích là khi nhập textbox ở Form1, sau đó nhấn button thì sẽ mở ra Form2 và nội dung của textbox ở Form2 = nội dung của textbox ở Form1:
Đối với Form 2, ta có đoạn code sau:
Trong Form1, ta chỉ việc có dòng code đơn giản sau mà thôi:Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication3 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } //set property để có thể lấy và nhận giá trị từ textbox1 của Form2 public TextBox TextBox1_Form2 { get { return textBox1; } set { textBox1 = value; } } } }
Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.TextBox1_Form2.Text = textBox1.Text; frm.Show(); } } }
Mọi người thử nghĩ cách dùng delegate đi
Anh Neverland giới thiệu qua về cái delegate đi, em chưa hiểu rõ về nó lắm.
Em thấy trong phần định nghĩa Form nếu trên form có 1 button thì có dòng như sau ngay dưới định nghĩa class
private Button button1;
Theo lý thuyết về class thì biến button1 ở trên là một biến có phạm vi cục bộ, nếu ta chuyển private thành public thì nó sẽ có thể truy nhập tới từ bên ngoài --> từ form nọ có thể gọi thuộc tính của form khác. Không biết dùng cách trên có được không?
Sau đây, mình xin giới thiệu thêm 2 cách nữa: dùng bộ khởi tạo và delegate
Ví dụ: tình huống với 2 Form như trên
DÙNG BỘ KHỞI TẠO (CONSTRUCTOR):
--------------------------------------
Trong Form 2:
Trong Form 1:Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form2 : Form { public Form2(string sInput) //thêm đối mục cho bộ khởi tạo mặc định do IDE phát sinh { InitializeComponent(); textBox1.Text = sInput; } } }
DÙNG DELEGATECode:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(textBox1.Text); frm.Show(); } } }
--------------------------------------
Trong Form 2: Xây dựng phương thức GetData để nhận dữ liệu từ textbox1 của Form1
Trong Form 1:Code:public void GetData(TextBox txtForm1) { this.textBox1.Text = txtForm1.Text; }
Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { //Khai báo 1 delegate dùng để vận chuyện dữ liệu giữa 2 Form public delegate void PassingData(TextBox text); public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); PassingData pd = new PassingData(frm.GetData); pd(this.textBox1); frm.Show(); } } }
mò suốt từ sáng đến giờ, cuối cùng nhờ thầy google, hướng dẫn của bạn giúp tui có hưng phấn trong ngày mới, cám ơn bạn nhiều.
Mình không hiểu khúc này lắm
C++ Code:
private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); PassingData pd = new PassingData(frm.GetData); pd(this.textBox1); frm.Show(); }
Sao khong gọi luôn như vầy
C++ Code:
private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); [B]frm.GetData(this.textBox1)[/B] //PassingData pd = new PassingData(frm.GetData); //pd(this.textBox1); frm.Show(); }
Làm như thế đều rất dài dòng mà chỉ gọi lấy được giá trị của một textbox.
Sao ko truyền thẳng đối tượng form1 vào contructor của form 2 luon cho gọn.
Mình từ Caulacbovb qua)
Giã từ VB6 đến với VB.Net, C# và Java.![]()