SoFunction
Updated on 2025-03-06

C# pop-up dialog box to determine or cancel the instance code for performing the corresponding operation

1. Selection dialog box based on WINFORM
Under WINFORM, we can use the system dialog box (MessageBox) to implement it. The specific idea is to read the return value (YES or NO) of the MessageBox to achieve control of the operation. Here is a demo program code code such as:

private void button1_Click(object sender,  e)
{
  ="";
  DialogResult MsgBoxResult;//Set the return value of the dialog box  MsgBoxResult = ("Please select the button you want to press",//Display content of the dialog box  "hint",//Title of the dialog box  ,//Define the buttons for the dialog box, here the YSE and NO buttons are defined.  ,//Define the chart style in the dialog box, here is a yellow triangle with an exclamation mark.  MessageBoxDefaultButton.Button2);//Define the button style of the dialog box  if (MsgBoxResult == )//If the return value of the dialog box is YES (press the "Y" button)  {
   this. = ;//Font color settings   ="You selected to press the "Yes" button!";
  }
  if(MsgBoxResult == )//If the return value of the dialog box is NO (press the "N" button)  {
   this. = ;//Font color settings    ="You selected to press the "No" button!";
   }
}

When the "Yes" button in the dialog box is pressed, the words "You selected the button to press "YES" are displayed. If you press the "No" button in the dialog box, the words "You selected the button to press "NO" are displayed.

2. Selection dialog box based on WEBFORM

As we all know, under WEBFORM, it is divided into front-end programs and back-end programs. In the WEBFORM demonstration program, the front-end program mainly places a Label control and a Button control. The foreground code is as follows:

<%@ Page language="c#" Codebehind="" 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  method="post" runat="server">
 <asp:Button  style="Z-INDEX: 101; LEFT: 328px; POSITION: absolute; TOP: 200px" runat="server" Text="Change the display content"></asp:Button>
 <asp:Label  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</asp:Label>
 </form>
 </body>
</HTML>

The background processing code is as follows:

private void Page_Load(object sender,  e)
{
if(!IsPostBack)//If the page is not loading for the first time  {
  ("onclick", "return confirm('Do you want to do this?  ');");
  // When the Button1 button is pressed, the dialog box will first pop up. Onclick means that the button triggers the event, and return confirm() is the content displayed in the dialog box.  If you choose to press the OK button in the dialog box, execute the Button1_Click method, otherwise it will not be executed.  }
}
private void Button1_Click(object sender,  e)
{
="You have selected to press the OK button!";
}

The idea of ​​this program is: when the "Change Display Content" button is pressed, a button event will be triggered. Before executing this event code, we first use a pop-up dialog box to confirm whether to continue execution. If the "OK" button of the dialog box is pressed, the event code will be executed. It will be displayed on the screen: You have selected to press the "OK" button, and if the "Cancel" button of the dialog box is pressed, the execution will be aborted.

Implementation of pop-up dialog using JavaScript

JavaScript dialog box

("js", "<script>alert('The screen input is incomplete, please check and re-enter')</script>");
   //("<script language='javascript' type='text/javascript'>");
   //(" alert('AAA') ");
   //("</script>");

Implementation using Messagebox:

Unlike in vb, you can directly use msgbox to get the return value of the message box, you need to use a variable of the dialogresult type in C# to accept the return value of the message dialog from the() method. As for whether the return value of () is yes, no, ok or cancel, you need to set the selection button that it can display in the show() method. The following example code can be found:

However, you need to introduce this quote under .net first, and then use

// initializes the variables to pass to the  method.

string message = "you did not enter a server name. cancel this operation?"; 
string caption = "no server name specified"; 
messageboxbuttons buttons = ; 
dialogresult result;

// displays the messagebox.

result = (this, message, caption, buttons, 
, messageboxdefaultbutton.button1, 
);

if(result == ) 
{ 
//do your action here. 
}

This is the article about the example code for confirming or canceling the corresponding operation of the C# pop-up dialog box. For more related contents for confirming the C# pop-up dialog box, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!