Make a combination control in C #
Lishui Auto Transport Group Co., Ltd. Information Center
We usually get the user's data input via controls such as text boxes, combination boxes, to tell the user that the content you need to enter is usually added to add a tip that prompts, such as: Name
Zhang San
If the content to be entered to be entered, you should put a lot of labels at the same time, not only the workload is large, it is not easy to typeset, this article describes how to make a combined control to simplify the operation, that is, the above label and text box are made into one. Overall, the text box moves the label follows the move, and you can set the location, content, font, color, etc. of the prompt tab.
Open the VS2003, create a new Windows control library (not a Windows project), you may wish to name Multicontrols.
Will the following code:
Public Class UserControl1: System.Windows.Forms.userControl
{
Private system.componentmodel.Container Components = NULL;
Public userControl1 ()
{
change into:
Public class textboxext: system.windows.Forms.TextBox
{
Private system.componentmodel.Container Components = NULL;
Public textBoxext ()
{
That is, modify the default control name userControl1 to TextBoxext, and modify the base class to TextBox, indicating that the control inherits from TextBox.
Click Menu "Project" → "Properties", select "All Configuration" in the "Configuration", click "Generate" under "Configuring Properties", specify a directory for the output path, that is, debug version Or the release version, you are output to the same directory, which is convenient for later references, as shown below:
Compile it to ensure that there is no error. The Windows control cannot be directly commissioned. For the convenience of debugging, we have to create a new Windows project.
Click Menu File → Add Item → "New Project", select "Windows Applications", you may wish to name TestTextBox and set the item to start the project. Click Menu "Project" → "Project Dependency", set the project TestTextBox relies on Multicontrols, as shown below:
Switch to the "General" tab of the toolbox panel, right-click, select Add / Transfer ", turn on the Custom Toolbox dialog, switch to" .NET Framework Components ", click" Browse "button, find Just compiled the generated multicontrols.dll, select the "OK" button after selecting, at this time, there is a custom control "TextBoxext", switch to the Form1 window of the TestTextBox project, just drag a few more places on the window, you can see At present, there is no difference between the current TextBoxext with the standard TextBox control, because we have not started writing code.
Switch into the code window of the TextBoxext (the following points refer to the code in this window, except for special instructions).
Create a tab (Label) that is prompted, and we will first create a procedure in the process of requiring some initial information.
Private system.windows.Forms.Label M_LBLPROMPT = NULL;
Private system.windows.Forms.Label myprompt {
get
{
// If prompted the label, it is not created one
IF (M_LBLPROMPT == Null && this.parent! = null)
{
// The creation process is behind it later
}
Return M_LBLPROMPT;
}
}
Add a variable to represent the positional relationship between the text box and Label.
Private system.drawing.contentAlignment M_LBLPROMPTALIGNMENT = system.drawing.contentAlignment.topleft;
Public system.drawing.contentAlignment LabElalAlignment
{
get
{
Return M_LBLPROMPTALIGNMENT;
}
set
{
M_LBLPROMPTALIGNMENT = VALUE;
THIS.LAYOUTCTRLS ();
}
}
The above code uses a function this.LayoutCtrls (), indicating how to specifically implement the location of the text box and the label, the code is as follows:
Private void LayoutCtrls ()
{
// Set the position of the message according to the position of the prompt text
Label LBL = MyPrompt;
IF (lbl! = null)
{
Switch (M_LBLPROMPTALIGNMENT)
{
Case system.drawing.contentAlignment.bottomcenter:
lbl.top = this.bottom;
LBL.LEFT = this.Left (this.width-lbl.width) / 2;
Break;
Case system.drawing.contentAlignment.bottomleft:
lbl.top = this.bottom;
lbl.left = this.left;
Break;
Case system.drawing.contentAlignment.bottomright:
lbl.top = this.bottom;
lbl.Left = this.right-lbl.width;
Break;
Case system.drawing.contentAlignment.middleCenter:
Case system.drawing.contentAlignment.topcenter:
LBL.TOP = this.top-lbl.height;
LBL.LEFT = this.Left (this.width-lbl.width) / 2;
Break;
Case system.drawing.contentAlignment.topleft:
LBL.TOP = this.top-lbl.height;
lbl.left = this.left;
Break;
Case system.drawing.contentAlignment.topright:
LBL.TOP = this.top-lbl.height;
lbl.Left = this.right-lbl.width;
Break;
Case system.drawing.contentAlignment.middleleft:
LBL.LEFT = this.left-lbl.width;
LBL.TOP = this.top (this.height-lbl.height) / 2;
Break;
Case system.drawing.contentAlignment.middleright:
lbl.Left = this.right;
LBL.TOP = this.top (this.height-lbl.height) / 2; BREAK;
}
}
}
Set the tag content:
Private string m_lbltext = "";
Public String LabelText
{
get
{
Return this.m_lbltext;
}
set
{
M_LblText = Value;
Label LBL = MyPrompt;
IF (lbl! = null)
{
LBL.TEXT = M_LBLText;
LayoutCtrls ();
}
}
}
With the above preparation, let's complete the specific process of the previous air created Label:
Private system.windows.Forms.Label myprompt
{
get
{
// If you prompt the label, create one
IF (M_LBLPROMPT == Null && this.parent! = null)
{
M_LBLPROMPT = New label ();
M_LBLPROMPT.Visible = True;
M_LBLPROMPT.AUTOSIZE = True;
M_LBLPROMPT.BACKCOLOR = system.drawing.color.transparent;
M_LBLPROMPT.TEXT = this.labeltext;
this.parent.controls.add (m_lblprompt);
This.Parent.ResumeLayout (FALSE);
}
Return M_LBLPROMPT;
}
}
You can set the foreground, background, font, etc. in the method of labeltext.
Re-compiling the solution, switch on the Form1 window of the TestTextBox project, select a control that just added, there should be a labeltext property in the property window. If you do not find it, turn it back, turn it again, modify it in the properties window The content of the LabelText property, look at the text box on the window, is there a prompt?
Similarly, you can also set the LabElalIgment, adjust the arrangement between tags and text boxes, if the text box is moved, the discovery tab will not move, let's implement this feature.
Switch to the TextBoxext design window, switch to the "Event" option in the Properties window, double-click the locationschanged event, will automatically add a response code in the code window, to ensure that the text box is changed, does not affect the arrangement of the label and text box, In the property window, find the resize event, do not double click, but select the LocationChanged event you just added from the drop-down box on the right, so that the two events correspond to the same code, the code written is as follows:
Private void textboxext_locationchanged (Object Sender, System.EventArgs E)
{
THIS.LAYOUTCTRLS ();
}
The VisableChanged event added to the text box is as follows:
Private void textboxext_visiblechanged (Object Sender, System.Eventargs E)
{
Label LBL = MyPrompt;
IF (lbl! = null)
{
lbl.visible = this.visible;
THIS.LAYOUTCTRLS ();
}
}
When designing, if a text box is excess, you need to delete, at this time, you should delete the corresponding tag, so modify the Dispose function as follows: protected override void dispose (bool disposing)
{
IF (Disposing)
{
// Release the prompt tag
Label LBL = MyPrompt;
IF (lbl! = null)
lbl.dispose ();
IF (Components! = NULL)
Components.dispose ();
}
Base.dispose (Disposing);
}
So far, a combined control is complete, you can also make ComboBoxext, etc., you can also combine more controls into one.
The code in this article can be downloaded from the "安 廷" folder to the "安 廷" folder, and the file name is "combined control .rar".