Shanzhai edition windows. Forms User Control

Source: Internet
Author: User

This article from http://www.cnblogs.com/chf/archive/2009/07/07/1518742.html

 

 

The prawns in the garden always discuss system. web controls are mostly web-based user-defined controls, but few people write some information about windows. forms user control or custom control, probably because Web is developed, compared to Windows. it is difficult to draw the interface of forms custom controls. Ah, remember that last year, in order to implement a winformProgramYou can't find any information about the user control in the yard...
First, declare that I encapsulate windows. Forms user controls, not custom controls. As for why write a user control instead of a custom control, I believe everyone knows that the user control only needs to inherit usercontrol. Drag several built-in win controls to write a few words. The purpose of Writing user controls is to improve the development efficiency and unify the appearance, so the interface effect is naturally beautiful without the control vendors.
Of course, there areSource codeDownload the images.

Figure 1 title text box

Figure 2 pop-up window

Figure 3 paging table

 

Figure 4 drop-down tree

 

It is very easy to develop user controls. I will not introduce them here.ArticleI want to introduce my thoughts to you.
When we write an application, the Information Input Interface appears, like "the title followed by a text box" (see figure 1, it must be to drag two controls in, one is label, the other is Textbox, and then layout. In fact, most of the time we only use a simple text box to read the content and display a text title, so in many cases we can combine the two into a title text box user control, this will save more development time. Of course, there are other benefits that will not be introduced here. Please referCodeSelf-experience.

The above is just a very simple occasion. Let's introduce a complicated occasion. Developers who write applications will certainly encounter the need to select a record in the pop-up window and return it to the main window to continue the operation (see figure 2). If it is like a normal design, we need to design the same job for each of these scenarios, so in order to save the workload, I encapsulated this. In order to allow the pop-up window to communicate with the main window, I defined the interface and defined the parameters. Of course this is a little restrictive, but it can solve a lot of problems. You may ask, it's not that simple. It's actually not simple, but it's easy to develop according to the Conventions.
Interface to be inherited in the pop-up window (used to trigger the selection action and use the transfer parameter, PS: Actually changed to delegate, as if other controls do this)
Public interface iselectbuttons
{
Dialogvalue mydialogvalue {Get; set ;}
Void setselect ();
}
To bring up the main window, drag the control mytextboxbuttonselect and set relevant parameters. Pay attention to the important parameters:
String dialogassemblyname, The namespace in the pop-up window. (Prepare for reflection)
String dialogaddform: the name of the pop-up window class if it is created. (To unify the interface style of the pop-up window, the myform user control is defined, and all other content-related user controls are defined as functional user controls)
String dialogaddusercontrol. If it is created, the pop-up user control class name is displayed.
The pop-up window for selection is also called dialogselectform and dialogselectusercontrol.
Some source code of the control is pasted here. The action of clicking the new button is as follows:
Private void buttonadd_click (Object sender, eventargs E)
{
Base. onclick (E );
Try
{
If (string. isnullorempty (dialogaddform) & string. isnullorempty (dialogaddusercontrol ))
Return;
Form F = getform (dialogaddform );
Usercontrol u = getusercontrol (dialogaddusercontrol );
Mydialogvalue = new dialogvalue ();
(Iselectbuttons) U). mydialogvalue = mydialogvalue;
(Iselectbuttons) U). mydialogvalue. fromvalue = getmytextbox ();
(Iselectbuttons) U). mydialogvalue. returnvalue = myvalue;
F. autosize = true;
F. Controls. Add (U );
If (U. Tag! = NULL)
F. Text = "" + U. Tag. tostring ();
F. showdialog ();
If (! String. isnullorempty (mydialogvalue. returntext )&&! String. isnullorempty (mydialogvalue. returnvalue ))
Setselectvalue ();
}
Catch (exception ex)
{
Ex. Source + = string. Format ("dialogaddusercontrol: {0}", dialogaddusercontrol );
Exceptionhelper. Exception (Ex );
}
}

Don't think that my shanzhai version of user control is gone here. Otherwise, there are many other things that I want to share with you. I hope you can understand them, and you will not be able to understand them, there are too few people who can understand me. I used to say that I was a lone wolf for a long time and recently changed my real name. I will not talk about it much. Here we will introduce the commonly used scenario of displaying the list of dview (see figure 3). I have encapsulated a control that can be used to export Excel by page, in addition, the functions are irrelevant to the business. Of course, you must develop the functions according to the Conventions. That is, the container (window) of the functions must implement the ipagination interface.
The interface code is simple:
Public interface ipagination
{
Void binddata ();
// Void selectrow (); // (PS: Why do you note this? I don't remember it. When I wrote it last year, I wanted to implement the select action to trigger events. I will share it with you this year .)
}
Mydview dview has several parameters that need attention:
Int pagesize
Int currentpageindex
Int rowstotalcount
Object datasource
Paste some codes of the mydatagridview user control:
Public int getpagecount ()
{
If (rowstotalcount = 0) | (pagesize = 0 ))
Return 0;
Int I = rowstotalcount/pagesize;
If (rowstotalcount % pagesize> 0)
I ++;
Return I;
}

Private ipagination findcontrolipagination ()
{
Control C = This. parent;
While (C! = NULL)
{
If (C is ipagination)
Break;
C = C. parent;
}
If (! (C is ipagination ))
Throw new exception ("No ipagination control in this usercontrol or form .");
Return C as ipagination;
}

Public void databind ()
{< br> datagridviewdefault. datasource = datasource;
toolstriplabelrowstotalcount. TEXT = string. format ("Total: {0} {1} items/page", rowstotalcount, pagesize);
toolstriplabelcurrentpage. TEXT = string. format ("{0}/{1}", currentpageindex, getpagecount ();
setenabled ();
}

private void redatabind ()
{< br> ipagination page = findcontrolipagination ();
page. binddata ();
}

nun, I was writing this article here, But I suddenly saw a dropdowntreeview control that had been made up for a long time, this control implements the drop-down tree selection function (see figure 4). Compared with other user controls, this control is not inherited from usercontrol, but from ComboBox, however, in fact, ComboBox inherits from control indirectly, so dropdowntreeview can define its own Treeview control to display and carry data, the only thing that needs to be processed is to find the Windows handle and obtain the ComboBox action to expand the receiving tree. See some code:
private void showdropdown ()
{< br> If (dropdown! = NULL)
{< br> treeviewhost. size = new size (dropdownwidth-2, dropdownheight);
dropdown. show (this, 0, this. height);
}< BR >}

Protected override void wndproc (ref message m)
{
Try
{
If (M. MSG = wm_lbuttondblclk | M. MSG = wm_lbuttondown)
{
Showdropdown ();
Return;
}
Base. wndproc (ref m );
}
Catch (exception ex)
{
CHF. Windows. Command. exceptionhelper. Exception (Ex );
}
}

Forget it, forget it. I can't finish writing it again. I will post the number of user controls in my class library. Many of them will know what they mean when they see their names.

 

 

Finally, paste the original code: Download the windows. Form user control (CHF. Windows. formcontrol)

As for calling the demo, I will not be able to give out the entire project code, after all, it is already in the market is still alive http://soft.bxren.cn
(I gave several demo files for calling, but an error will be reported because I didn't provide the complete demo section. I believe that prawns can see the effect through the source code)

Because it was used in the early stage, it was not specially reconstructed or packaged, but it was very practical. This is a temporary deal, and I didn't make a good introduction or trim the Code. If I don't use it, I will ask if I have a thick face. If I use it, it won't hurt me if I hit my head with a brick ~ Don't believe it ....

 

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.