本文实例为大家分享了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"); ? ? ? ? }
结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。