C # controls, inter-thread operations (obtain data of the main form Control)

Source: Internet
Author: User

2. The sub-thread obtains the data of the main form control. The TextBox is used as an example.

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Threading;

Namespace WindowsApplication1
{
Public partial class Form1: Form
{
Private System. Windows. Forms. TextBox textBox1;
Private System. Windows. Forms. Button button1;

Public Form1 ()
{
InitializeComponent ();
}
Private void InitializeComponent ()
{
This. textBox1 = new System. Windows. Forms. TextBox ();
This. button1 = new System. Windows. Forms. Button ();
This. SuspendLayout ();
//
// TextBox1
//
This. textBox1.Location = new System. Drawing. Point (111, 44 );
This. textBox1.Name = "textBox1 ";
This. textBox1.Text = "aaaaaaaaaa ";
This. textBox1.Size = new System. Drawing. Size (194, 21 );
This. textBox1.TabIndex = 0;
//
// Button1
//
This. button1.Location = new System. Drawing. Point (325, 34 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (87, 30 );
This. button1.TabIndex = 1;
This. button1.Text = "button1 ";
This. button1.UseVisualStyleBackColor = true;
This. button1.Click + = new System. EventHandler (this. button#click );
//
// Form1
//
This. AutoScaleDimensions = new System. Drawing. SizeF (6F, 12F );
This. AutoScaleMode = System. Windows. Forms. AutoScaleMode. Font;
This. ClientSize = new System. Drawing. Size (471,353 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textBox1 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. ResumeLayout (false );
This. initialize mlayout ();

}
Private void button#click (object sender, EventArgs e)
{
Thread t = new Thread (ThreadStart );
T. Start ();
}

Private void ThreadStart ()
{
MessageBox. Show (GetTextBoxText ());
}

Private delegate string GetTextBoxTextDelegate ();

Private string GetTextBoxText ()
{
If (this. textBox1.InvokeRequired)
{
Return this. Invoke (new GetTextBoxTextDelegate (GetTextBoxText). ToString ();
}
Else
{
Return this. textBox1.Text;
}
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.