C # pop-up selection Dialog Box program

Source: Internet
Author: User

1. Selection Dialog Box Based on WINFORM

In WINFORM, we can use the system's dialog box (MessageBox). The specific idea is to read the return value (YES or NO) of MessageBox to control the operation. The following is a Demo code example:

Private void button#click (object sender, System. EventArgs e)

{

Label1.Text = "";

DialogResult MsgBoxResult; // sets the return value of the dialog box.

MsgBoxResult = MessageBox. Show ("select the button you want to press", // display content of the dialog box

"Prompt", // the title of the dialog box

MessageBoxButtons. YesNo, // defines the buttons in the dialog box. The YSE and NO buttons are defined here.

MessageBoxIcon. Exclamation, // define the chart style in the dialog box. Here is a yellow triangle with an Exclamation point

MessageBoxDefaultButton. Button2); // defines the button style of the dialog box.

If (MsgBoxResult = DialogResult. Yes) // if the returned value of the dialog box is YES (press the "Y" button)

{

This. label1.ForeColor = System. Drawing. Color. Red; // set the font Color.

Label1.Text = "you have selected to press the" Yes "button! ";

}

If (MsgBoxResult = DialogResult. No) // if the returned value of the dialog box is NO (press the "N" button)

{

This. label1.ForeColor = System. Drawing. Color. Blue; // set the font Color.

Label1.Text = "you have selected to press the" No "button! ";

}

}

Result 1 after the code is run:

Figure 1

When the "YES" button in the pressing dialog box is displayed, the red font "you selected to press the" YES "button" is displayed, if you press the "NO" button in the dialog box, the "you have selected to press" NO "button in the blue font is displayed.

2. Selection Dialog Box Based on WEBFORM

As you know, in WEBFORM, the front-end program and the backend program are divided. In the demonstration program of WEBFORM, the front-end program mainly places a Label control and a Button control. The front-end code is as follows:

<% @ Page language = "c #" Codebehind = "WebForm1.aspx. cs" AutoEventWireup = "false" Inherits = "dhk. WebForm1" %>

<HTML>

<HEAD>

<Title> WebForm1 </title>

<Meta name = "GENERATOR" Content = "Microsoft Visual Studio. NET 7.1">

<Meta name = "CODE_LANGUAGE" Content = "C #">

<Meta name = "vs_defaultClientScript" content = "JavaScript">

</HEAD>

<Body MS_POSITIONING = "GridLayout">

<Form id = "Form1" method = "post" runat = "server">

<Asp: Button id = "Button1" style = "Z-INDEX: 101; LEFT: 328px; POSITION: absolute; TOP: 200px "runat =" server "Text =" Change display content "> </asp: Button>

<Asp: Label id = "Label1" style = "Z-INDEX: 102; LEFT: 288px; POSITION: absolute; TOP: 152px "runat =" server "Width =" 256px "Height =" 32px "Font-Size =" Larger "ForeColor =" Red "> This is the initial display content </asp: label>

</Form>

</Body>

</HTML>

The background processing code is as follows:

Private void Page_Load (object sender, System. EventArgs e)

{

If (! IsPostBack) // If the page is not loaded for the first time

{

Button1.Attributes. Add ("onclick", "return confirm ('do you want to perform this operation? ');");

// When you press the Button1 button, a dialog box is displayed. onclick indicates the button trigger event, and return confirm () indicates the content displayed in the dialog box. If you choose to press the "OK" button in the dialog box, execute the button#click method. Otherwise, do not execute.

}

}

Private void button#click (object sender, System. EventArgs e)

{

Label1.Text = "you have selected to press the" OK "button! ";

}

The idea of this program is: when you press the "Change display content" button, a button event will be triggered. Before executing the Event code, we will first use a pop-up dialog box, confirm whether or not to continue the execution. If you press the "OK" button in the dialog box, continue to execute the Event code. The screen displays: You have selected to press the "OK" button, if you press the "cancel" button in the dialog box, stop the operation. The running effect of this program 2

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.