 |
|
WinApp C# ผมอยากให้ class ที่อยู่ใน class ใช้ control ของ class แม่ได้ครับ |
|
 |
|
|
 |
 |
|
ได้แน่อนคับ ไม่เชื่อลองดูครับ
|
 |
 |
 |
 |
Date :
2017-08-21 16:21:46 |
By :
Thaidevelopment.NET |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แบบนี้ไม่ได้คับ ถ้าจะเอาไปแสดงผลได้ ต้องเอา RitchTextBox ไปไว้้ที่ TestControl Class นี้แทนครับ
เพราะเกืดการ New ขึ้นมาจาก new TestControl().Test_("Set Text 555"); มันเลยแสดงผลไม่ได้ใน Class แม่ครับ ถ้าทำได้ต้องให้มัน Return ออกมา
public partial class SelectForm2 : Form
{
RichTextBox richTextBox1 = new RichTextBox();
public SelectForm2()
{
InitializeComponent();
richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
richTextBox1.Location = new System.Drawing.Point(20, 20);
richTextBox1.Width = 300;
richTextBox1.Height = 200;
richTextBox1.Name = "DynamicRichTextBox";
richTextBox1.Font = new Font("Georgia", 16);
this.Controls.Add(this.richTextBox1);
}
class TestControl
{
String app = "";
public String Test_(string s)
{
for (int i = 0; i < 10; i++)
app+="\n " + s + " " + i;
return app;
}
}
private void SelectForm2_Load(object sender, EventArgs e)
{
richTextBox1.AppendText(new TestControl().Test_("Set Text 555"));
}
}
|
 |
 |
 |
 |
Date :
2017-08-22 09:44:13 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าจะเอาแบบนั้นจริงๆ ก็ทำได้ครับ แค่ส่ง COntrol ไปให้มันด้วยครับ
public partial class SelectForm2 : Form
{
RichTextBox richTextBox1 = new RichTextBox();
public SelectForm2()
{
InitializeComponent();
richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
richTextBox1.Location = new System.Drawing.Point(20, 20);
this.Controls.Add(this.richTextBox1);
}
class TestControl
{
public void Test_(string s,RichTextBox richTextBox1)
{
for (int i = 0; i < 10; i++)
richTextBox1.Text += "\n " + s + " " + i;
}
}
private void SelectForm2_Load(object sender, EventArgs e)
{
new TestControl().Test_("Set Text 555", richTextBox1);
}
}
|
 |
 |
 |
 |
Date :
2017-08-22 10:41:01 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปล.
มีวิธีทำแบบนี้ไม๊ครับ
เราส่งค่าให้กับ อีก คลาส ทำงาน
ในขณะที่ คลาส นั้นๆ ทำงานอยู่
อยากให้ คลาสหลัก เปลี่ยนแปลงได้
เช่น
Code (C#)
public partial class Form2 : Form
{
public RichTextBox richTextBox1 = new RichTextBox();
public Form2()
{
InitializeComponent();
richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
richTextBox1.Location = new System.Drawing.Point(0, 0);
this.Controls.Add(this.richTextBox1);
}
private void Form2_Load(object sender, EventArgs e)
{
new TestControl().Test_("Set Text 555");
}
}
Code (C#)
public class TestControl : Form2
{
public void Test_(string s)
{
for (int i = 0; i < 10;i++ )
// ขณะที่ TestControl ทำงานอยู่ richTextBox1.Text ก็จะเปลี่ยนค่าไป
// richTextBox1.Text += "\n " + s + " " + i;
}
}
|
 |
 |
 |
 |
Date :
2017-08-22 10:45:11 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วส่ง Parameter แบบข้างบนเข้ามา ใช้ไม่ได้เหรอคับ
|
 |
 |
 |
 |
Date :
2017-08-22 11:04:20 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แบบคีย์ข้อมูลลงไปได้ โดยไม่ค้างใช่ไหมครับ
|
 |
 |
 |
 |
Date :
2017-08-22 11:12:23 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ง้นลองคำถามนี้ครับ
เราจะประมวลผล หรือ display ค่า เพื่อดูความคืบหน้าของ method ใน อีก class ได้ยังไงครับ
Code (C#)
public partial class Form2 : Form
{
public RichTextBox richTextBox1 = new RichTextBox();
public Form2()
{
InitializeComponent();
richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
richTextBox1.Location = new System.Drawing.Point(0, 0);
this.Controls.Add(this.richTextBox1);
}
private void Form2_Load(object sender, EventArgs e)
{
new TestControl().Test_("Set Text 555");
new TestControl().Test_("Set Text 666");
new TestControl().Test_("Set Text 777");
new TestControl().Test_("Set Text 888");
}
}
Code (C#)
public class TestControl
{
public void Test_(string s)
{
for (int i = 0; i < 100000;i++ )
//ส่งค่าการประมวลผล
}
}
จากโค้ดด้านบน Form2 ส่งค่า s ให้ TestControl ประมวลผลซึ่งก็เริ่มตั้งแต่ 0 ถึง 100000 นานมากกกกก
นอกจากนั้น Form2 ยังส่งไปถึง 4 ค่า ทำให้การประมวลผลนานเข้าไปอีก
ตรงส่วนนี้แหละครับที่ผมคิดว่าจะทำยังไงให้ Form2 รับรู้ความคืบหน้าของ work ที่ TestControl ทำอยู่
และอาจจะใช้ร่วมกับ processbar เพื่อให้ user ได้รู้ความคืบหน้า
หรือใช้ richTextBox1 เพื่อดูขั้นตอนได้ดวย
|
ประวัติการแก้ไข 2017-08-22 11:20:10
 |
 |
 |
 |
Date :
2017-08-22 11:18:27 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประมาณนี้ปะ ใช้ Thread เข้ามาช่วย
public static RichTextBox richTextBox1 = new RichTextBox();
public SelectForm2()
{
InitializeComponent();
richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
richTextBox1.Location = new System.Drawing.Point(20, 20);
this.Controls.Add(richTextBox1);
}
public class TestControl
{
private string s;
public TestControl(string s)
{
this.s = s;
}
public void Test_()
{
for (int i = 0; i < 1000000; i++)
{
Action action = () => richTextBox1.Text += "\n " + s + " " + i;
richTextBox1.Invoke(action); // Or use BeginInvoke
}
}
}
private void SelectForm2_Load(object sender, EventArgs e)
{
Thread tid2 = new Thread(new ThreadStart(new TestControl("Set Text 555").Test_));
tid2.Start();
}
|
 |
 |
 |
 |
Date :
2017-08-22 11:32:56 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|