Using user controls in a. NET application

Source: Internet
Author: User
Tags define bool
Programs | controls using user controls in. NET Applications

Zhengzo 2004-11-30

People who have done asp.net know that it is convenient to use user controls when developing, providing considerable flexibility for functional modularity. Happily, you can also use user controls to develop Windows Forms. Here we take a look at adding properties and events to the user control and implementing sending the message to the parent container. This article is mainly to provide some references for friends who have not used the user control.

The implementation of the user control is relatively simple, directly from the System.Windows.Forms.UserControl.

public class UserControl1:System.Windows.Forms.UserControl



To facilitate the test I added a TextBox above and registered the TextChanged event for the textbox,

This.textBox1.TextChanged + = new System.EventHandler (this.textbox1_textchanged);

Event handler function,

private void textBox1_TextChanged (object sender, System.EventArgs e)

{

MessageBox.Show (This.textBox1.Text);

}

This shows that if the content of the text box in the control changes, the current text box content is displayed with MessageBox.

The control appears as follows:



Add the above user control to the form, and when we change the text of the textbox, we can see that it's easy to jump out of a dialog box.

Here's a look at adding properties to the control.

This defines a private variable.

private string Customvalue;

Add access to his properties

public string Customvalue

{

Get{return Customvalue;}

Set{customvalue =value;}

}

When used in a form, it is accessed like a normal control.

Usercontrol11.customvalue = "User control custom Data";

Events allow you to pass messages to a form, before you define a simple parameter class.

public class Textchangeeventargs:eventargs

{

private string message;

Public Textchangeeventargs (String message)

{

this.message = message;

}

public string Message

{

Get{return message;}

}

}

Define delegate as,

public delegate void Textboxchangedhandle (Object Sender,textchangeeventargs e);

Next, add an event to the user control.

Defining events

public event Textboxchangedhandle Usercontrolvaluechanged;

To trigger a new event for the user control, modify the code,

private void textBox1_TextChanged (object sender, System.EventArgs e)

{

if (usercontrolvaluechanged!= null)

Usercontrolvaluechanged (this,new Textchangeeventargs (This.textBox1.Text));



}

Well, to make it easier to answer questions on the CSDN, post the complete code:

Using System;

Using System.Collections;

Using System.ComponentModel;

Using System.Drawing;

Using System.Data;

Using System.Windows.Forms;



Namespace ZZ. WindowsApplication1

{

public class UserControl1:System.Windows.Forms.UserControl

{

Private System.Windows.Forms.TextBox TextBox1;

private string Customvalue;



Private System.ComponentModel.Container components = null;



public string Customvalue

{

Get{return Customvalue;}

Set{customvalue =value;}

}



Defining events

public event Textboxchangedhandle Usercontrolvaluechanged;



Public UserControl1 ()

{

InitializeComponent ();

}



protected override void Dispose (bool disposing)

{

if (disposing)

{

if (Components!= null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing);

}



Code generated #region Component Designer

private void InitializeComponent ()

{

This.textbox1 = new System.Windows.Forms.TextBox ();

This. SuspendLayout ();

This.textBox1.Location = new System.Drawing.Point (12, 36);

This.textBox1.Name = "TextBox1";

This.textBox1.TabIndex = 0;

This.textBox1.Text = "TextBox1";

This.textBox1.TextChanged + = new System.EventHandler (this.textbox1_textchanged);

This. Controls.Add (This.textbox1);

This. Name = "UserControl1";

This. Size = new System.Drawing.Size (150, 92);

This. ResumeLayout (FALSE);



}

#endregion



private void textBox1_TextChanged (object sender, System.EventArgs e)

{

if (usercontrolvaluechanged!= null)

Usercontrolvaluechanged (this,new Textchangeeventargs (This.textBox1.Text));



}

}

Defining delegates

public delegate void Textboxchangedhandle (Object Sender,textchangeeventargs e);



public class Textchangeeventargs:eventargs

{

private string message;

Public Textchangeeventargs (String message)

{

this.message = message;

}

public string Message

{

Get{return message;}

}

}

}

When you use to register the above event in the form, it is easier to attach the source code,

Using System;

Using System.Drawing;

Using System.Collections;

Using System.ComponentModel;

Using System.Windows.Forms;

Using System.Data;



Namespace ZZ. WindowsApplication1

{

public class Form1:System.Windows.Forms.Form

{

Private Windowsapplication1.usercontrol1 UserControl11;

Private System.ComponentModel.Container components = null;



Public Form1 ()

{

InitializeComponent ();

Usercontrol11.customvalue = "User control custom Data";

Usercontrol11.usercontrolvaluechanged + = new Textboxchangedhandle (usercontrol11_usercontrolvaluechanged);

}



protected override void Dispose (bool disposing)

{

if (disposing)

{

if (Components!= null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing);

}



Code generated #region the Windows forms Designer

private void InitializeComponent ()

{

This.usercontrol11 = new Windowsapplication1.usercontrol1 ();

This. SuspendLayout ();

This.userControl11.Location = new System.Drawing.Point (8, 8);

This.userControl11.Name = "UserControl11";

This.userControl11.Size = new System.Drawing.Size (150, 84);

This.userControl11.TabIndex = 0;

This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);

This. ClientSize = new System.Drawing.Size (292, 193);

This. Controls.Add (THIS.USERCONTROL11);

This. Name = "Form1";

This. Text = "Form1";

This. ResumeLayout (FALSE);



}

#endregion



[STAThread]

static void Main ()

{

Application.Run (New Form1 ());

}



private void Usercontrol11_usercontrolvaluechanged (object sender, Textchangeeventargs e)

{

MessageBox.Show ("The value of the current control is:" + e.message);

}

}

}

In addition to dynamic loading, the control is added to the container's Controls collection, and the following is the addition of controls to the constructor.

Public Form1 ()

{

InitializeComponent ();

UserControl1 UC = new UserControl1 ();

Uc. Customvalue = "Dynamically loaded user control";

Uc. Usercontrolvaluechanged + = new Textboxchangedhandle (usercontrol11_usercontrolvaluechanged);

This. Controls.Add (UC);

}

In addition, drag the user control from the Toolbox in vs.net to the form, and if it is the first time you need to compile the project.




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.