学无先后,达者为师

网站首页 编程语言 正文

C#线程中弹窗的制作方法_C#教程

作者:机器侠客   更新时间: 2022-04-18 编程语言

本文实例为大家分享了C#线程中弹窗的制作代码,供大家参考,具体内容如下

首先建立一个ShowFrom窗体,窗体中放入两个按钮分别为确定和取消
分别在按钮中添加如下事件

private void btn_ok_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_cancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

在主窗体中建立如下函数可以实现调用显示弹窗

public static bool MsgShow(string msg = "未定义操作提示", bool bcancel = false)
        {
            //设备暂停,蜂鸣开始
            //   VAR.gsys_set.beep_en = true;
            Task<bool> mtask = new Task<bool>
                (
                () =>
                {
                    lock (WarnObj)
                    {
                        warning frWarning = new warning();//错误窗体
                        frWarning.TopMost = true;
                        frWarning.BackColor = Color.Yellow;
                        frWarning.lb_msg.Text = msg;
                        if (bcancel)
                        {
                            frWarning.btn_cancle.Visible = true;
                            frWarning.btn_cancle.Enabled = true;
                        }
                      
                        frWarning.ShowDialog();
                       
                        VAR.msg.AddMsg(Msg.EM_MSGTYPE.SAVE_WAR, string.Format("{0}", msg));
                        if (frWarning.DialogResult == DialogResult.OK)
                        {
                            frWarning.Dispose();
                            return true;
                        }
                        else {
                            frWarning.Dispose();
                            return false;
                        }
                    }
                }  );
            mtask.Start();
            mtask.Wait();          
            return mtask.Result;
}
private void button1_Click_1(object sender, EventArgs e)
        {

            var ret = actiom.MsgShow("ceshi", true);
            if (ret)
                MessageBox.Show("ok");
            else
                MessageBox.Show("err");
        }

结果:

原文链接:https://blog.csdn.net/gy0124/article/details/102572656

栏目分类
最近更新