Winform development: several common development experiences and knowledge accumulation (1). winform Development

Source: Internet
Author: User

Winform development: several common development experiences and knowledge accumulation (1). winform Development

I have been developing Winform for many years. I have worked tirelessly and made little achievements. I collect or develop some common things myself, and I have accumulated some development experience and knowledge that can be used in various projects, I will introduce some of them gradually to help readers make progress together.

1. The close button of window [×] is minimized, and a message is displayed on the tray.

In general, some management systems, in order to prevent the customer from shutting down the program at Will or for other reasons, usually the window [×] Close button is minimized, such as familiar fetion, MSN, etc, however, some customers who are not very familiar with it do not know where the program is when the pallets are minimized. Therefore, when the pallets are minimized, it is necessary to prompt information with a bubble, as shown below.

First, add a policyicon control in the design interface of the main form, and then implement the relevant code.

Some key code is listed below. You should know how to implement it after reading it.

Private void policymenu_show_click (object sender, EventArgs e) {if (this. windowState = FormWindowState. minimized) {this. windowState = FormWindowState. maximized; this. show (); this. bringToFront (); this. activate (); this. focus ();} else {this. windowState = FormWindowState. minimized; this. hide () ;}} private void policymenu_exit_click (object sender, EventArgs e) {try {this. showInTaskbar = false; P Ortal. gc. quit ();} catch {// Nothing to do .}} private void policyicon1_mousedoubleclick (object sender, MouseEventArgs e) {policymenu_show_click (sender, e);} private void MainForm_MaximizedBoundsChanged (object sender, EventArgs e) {this. hide () ;}/// <summary> /// zoom out to the tray without exiting /// </summary> private void MainForm_FormClosing (object sender, FormClosingEventArgs e) {// if we operate the [×] button, the program will be scaled down to the tray instead of being closed And prompt the user. if (this. WindowState! = FormWindowState. minimized) {e. cancel = true; // do not close the program // The icon prompt is displayed when the program is minimized to the tray, prompting the user not to close the program this. windowState = FormWindowState. minimized; policyicon1.showballoontip (3000, "program minimization prompt", "the icon has been reduced to the tray. To open the window, double-click the icon. ", ToolTipIcon. info) ;}} private void MainForm_Move (object sender, EventArgs e) {if (this = null) {return ;} // if (this. windowState = FormWindowState. minimized) {this. hide (); notifyIcon1.ShowBalloonTip (3000, "program minimization prompt", "the icon has been reduced to the tray. To open the window, double-click the icon. ", ToolTipIcon. Info );}}

2. Only one program instance is allowed. Even if it is connected through a virtual desktop, only one person is allowed to run.

This code has been encapsulated. You only need to call the function in the Main function. When multiple instances are allowed, the following dialog box prompts that many instances are not allowed to run, as shown below:

The Code is as follows.

/// <Summary> /// main entry point of the application. /// </Summary> [STAThread] private static void Main () {GlobalMutex (); Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); // ******* start code ***********} private static Mutex mutex = null; private static void GlobalMutex () {// whether to create mutex bool newMutexCreated = false; string mutexName = "Global \" + "WareHouseMis" for the first time; // The system name, which is Global, indicates that even though the connection is through a virtual desktop, only one try {mu operation is allowed. Tex = new Mutex (false, mutexName, out newMutexCreated);} catch (Exception ex) {Console. write (ex. message); System. threading. thread. sleep (1000); Environment. exit (1);} // create mutex if (newMutexCreated) {Console. writeLine ("program started"); // todo: the task to be executed} else {MessageUtil. showTips ("another window is already running and cannot be run repeatedly. "); System. Threading. Thread. Sleep (1000); Environment. Exit (1); // Exit the program }}

3. Use NotifyWindow to prompt users

You can use the yywindow class (which is included in the final attachment) to make some information prompts so that you can understand some important information. The interface is more friendly, as shown below:

The code for prompting information is as follows:

/// <Summary> /// pop-up prompt message window /// </summary> public void handle Y (string caption, string content) {comment y (caption, content, 400,200,500 0);} // <summary> // The message window is displayed. // </summary> public void Policy (string caption, string content, int width, int height, int waitTime) {policywindow = new policywindow (caption, content); policywindow. titleClicked + = new System. eventHandler (policyuncwc Lick); yywindow. textClicked + = new EventHandler (notifyWindowClick); yywindow. setDimensions (width, height); policywindow. waitTime = waitTime; policywindow. notify ();} private void policywindowclick (object sender, EventArgs e) {// SystemMessageInfo info = BLLFactory <SystemMessage>. instance. findLast (); // if (info! = Null) // {// FrmEditMessage dlg = new FrmEditMessage (); // dlg. ID = info. ID; // dlg. showDialog ();//}}

4. Use the SearchCondion control to simplify the conversion of query Conditions

Whether in Winform or WebForm, the query construction conditions are complex. Using this control can effectively simplify the code and improve the accuracy and convenience of operations, this control has been completed for several years and has been accompanied by various query operations.

private string GetConditionSql()        {            SearchCondition condition = new SearchCondition();            condition.AddCondition("ItemName", this.txtName.Text, SqlOperator.Like)                .AddCondition("ItemBigType", this.txtBigType.Text, SqlOperator.Like)                .AddCondition("ItemType", this.txtItemType.Text, SqlOperator.Like)                .AddCondition("Specification", this.cmbSpecNumber.Text, SqlOperator.Like)                .AddCondition("MapNo", this.txtMapNo.Text, SqlOperator.Like)                .AddCondition("Material", this.txtMaterial.Text, SqlOperator.Like)                .AddCondition("Source", this.txtSource.Text, SqlOperator.Like)                .AddCondition("Note", this.txtNote.Text, SqlOperator.Like)                .AddCondition("Manufacture", this.txtManufacture.Text, SqlOperator.Like)                .AddCondition("ItemNo", this.txtItemNo.Text, SqlOperator.LikeStartAt);            string where = condition.BuildConditionSql().Replace("Where", "");            return where;        }

You can construct conditions and pass in the query function to query data.

String where = GetConditionSql (); List <ItemDetailInfo> list = BLLFactory <ItemDetail>. instance. find (where, this. winGridViewPager1.PagerInfo); this. winGridViewPager1.DataSource = new WHC. pager. winControl. sortableBindingList <ItemDetailInfo> (list); this. winGridViewPager1.PrintTitle = Portal. gc. gAppUnit + "--" + "spare parts information report ";
Finally, some class libraries and controls used by the Code: http://files.cnblogs.com/wuhuacong/WinformTips.rar

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.