MessageBox Class

Examples

http://msdn.microsoft.com/en-us/library/aa969773(v=vs.110).aspx

 

Displays a message box that can contain text, buttons, and symbols that inform and instruct the user.

MessageBoxButtons.YesNo

 

const string message = "您想删除当前记录吗?";
                const string caption = "删除当前记录";
                var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    //delete the current record



                }
View Code

 

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    const string message =
        "Are you sure that you would like to close the form?";
    const string caption = "Form Closing";
    var result = MessageBox.Show(message, caption,
                                 MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question);

    // If the no button was pressed ... 
    if (result == DialogResult.No)
    {
        // cancel the closure of the form.
        e.Cancel = true;
    }
}

 

 

 

private void validateUserEntry()
	{

		// Checks the value of the text. 

		if(serverName.Text.Length == 0)
		{

			// Initializes the variables to pass to the MessageBox.Show method. 

			string message = "You did not enter a server name. Cancel this operation?";
                        string caption = "Error Detected in Input";
			MessageBoxButtons buttons = MessageBoxButtons.YesNo;
			DialogResult result;

			// Displays the MessageBox.

			result = MessageBox.Show(message, caption, buttons);

			if (result == System.Windows.Forms.DialogResult.Yes)
			{

				// Closes the parent form. 

				this.Close();

			}

		}

	}

 

 

// Handles the ComboBox1 DropDown event. If the user expands the   
	// drop-down box, a message box will appear, recommending the 
	// typical installation. 
	private void ComboBox1_DropDown(object sender, System.EventArgs e)
	{
		MessageBox.Show("Typical installation is strongly recommended.", 
		"Install information", MessageBoxButtons.OK, 
			MessageBoxIcon.Information);
	}

 

MessageBoxButtons Enumeration

 

 

 Member nameDescription
 AbortRetryIgnoreThe message box contains Abort, Retry, and Ignore buttons.
 OKThe message box contains an OK button.
 OKCancelThe message box contains OK and Cancel buttons.
 RetryCancelThe message box contains Retry and Cancel buttons.
 YesNoThe message box contains Yes and No buttons.
 YesNoCancelThe message box contains Yes, No, and Cancel buttons.

 MessageBoxIcon Enumeration

Member nameDescription
 AsteriskThe message box contains a symbol consisting of a lowercase letter i in a circle.
 ErrorThe message box contains a symbol consisting of white X in a circle with a red background.
 ExclamationThe message box contains a symbol consisting of an exclamation point in a triangle with a yellow background.
 HandThe message box contains a symbol consisting of a white X in a circle with a red background.
 InformationThe message box contains a symbol consisting of a lowercase letter i in a circle.
 NoneThe message box contain no symbols.
 QuestionThe message box contains a symbol consisting of a question mark in a circle. The question-mark message icon is no longer recommended because it does not clearly represent a specific type of message and because the phrasing of a message as a question could apply to any message type. In addition, users can confuse the message symbol question mark with Help information. Therefore, do not use this question mark message symbol in your message boxes. The system continues to support its inclusion only for backward compatibility.
 StopThe message box contains a symbol consisting of white X in a circle with a red background.
 WarningThe message box contains a symbol consisting of an exclamation point in a triangle with a yellow background.

 DialogResult

Member nameDescription
 AbortThe dialog box return value is Abort (usually sent from a button labeled Abort).
 CancelThe dialog box return value is Cancel (usually sent from a button labeled Cancel).
 IgnoreThe dialog box return value is Ignore (usually sent from a button labeled Ignore).
 NoThe dialog box return value is No (usually sent from a button labeled No).
 NoneNothing is returned from the dialog box. This means that the modal dialog continues running.
 OKThe dialog box return value is OK (usually sent from a button labeled OK).
 RetryThe dialog box return value is Retry (usually sent from a button labeled Retry).
 YesThe dialog box return value is Yes (usually sent from a button labeled Yes).

 

if(dr == DialogResult.Cancel)
{
    e.Cancel = true;
}
else
{
    if(dr == DialogResult.Yes)
    {
         //Save the data
    }
}

 

DialogResult dr = MessageBox.Show("Do You Want to Save Data?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

                if (dr == DialogResult.Yes)
                {
                    //e.Cancel = false ; 

                }
                else if (dr == DialogResult.Cancel)
                {
                    //e.cancel = true ; 
                }
                else
                {

                }
View Code

 

 

How to: Retrieve Data from a Dialog Box

http://msdn.microsoft.com/en-us/library/bb383855(v=vs.90).aspx

Key =>

Form2 subForm = new Form2(this);
subForm.Show();

 

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void addItems_Click(object sender, EventArgs e)
        {
            Form2 subForm = new Form2(this);
            subForm.Show();
        }
    }




public partial class Form2 : Form
    {
        Form1 mainForm;

        public Form2(Form1 mainForm)
        {
            this.mainForm = mainForm;

            InitializeComponent();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text != string.Empty)
            {

                mainForm.listBox1.Items.Clear();

                string[] stringsEntered = textBox1.Lines;

                for (int count = 0; count < stringsEntered.Length; count++)
                {

                    mainForm.listBox1.Items.Add(stringsEntered[count]);

                }

            }
            this.Close();

        }
    }
View Code

 

 

 

 

 

END

转载于:https://www.cnblogs.com/kevinygq/p/3887381.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个集成了注册逻辑的 Python 代码示例: ```python from tkinter import * import tkinter.messagebox as messagebox class LoginFrame(Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.master.title('登录') self.master.geometry('300x200') self.create_widgets() def create_widgets(self): self.label_user = Label(self.master, text='用户名:') self.label_user.pack() self.entry_user = Entry(self.master) self.entry_user.pack() self.label_pwd = Label(self.master, text='密码:') self.label_pwd.pack() self.entry_pwd = Entry(self.master, show='*') self.entry_pwd.pack() self.btn_login = Button(self.master, text='登录', command=self.login) self.btn_login.pack() self.btn_register = Button(self.master, text='注册', command=self.register) self.btn_register.pack() def login(self): user = self.entry_user.get() pwd = self.entry_pwd.get() # 在这里可以添加验证逻辑 messagebox.showinfo('登录成功', '欢迎回来,%s' % user) def register(self): user = self.entry_user.get() pwd = self.entry_pwd.get() # 在这里添加注册逻辑 if user.strip() == '' or pwd.strip() == '': messagebox.showerror('注册失败', '用户名和密码不能为空!') elif user in users: messagebox.showerror('注册失败', '该用户名已存在!') else: users[user] = pwd messagebox.showinfo('注册成功', '注册成功,请登录!') root = Tk() users = {} # 存储用户信息的字典,键为用户名,值为密码 login_frame = LoginFrame(root) login_frame.pack() root.mainloop() ``` 以上代码中,我们在 `register` 函数中添加了注册逻辑,包括判断用户名和密码是否为空、判断用户名是否已存在、在 `users` 字典中添加新用户等步骤。同时,我们也对注册成功和失败进行了相应的提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值