【问题标题】:Customizing Dialogresult MessageBox with user defined MessageBoxButton [duplicate]使用用户定义的 MessageBoxButton 自定义 Dialogresult MessageBox [重复]
【发布时间】:2013-12-23 11:19:14
【问题描述】:

我想在按钮上创建一个带有我自己指定标签的 DialogResult MessageBox。我知道使用 YesNo 选项按钮对 DR MessageBox 进行编码。

【问题讨论】:

    标签: c# dialogresult


    【解决方案1】:

    创建您自己的对话框并放置按钮。您可以为按钮分配对话框结果值。

    this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
    this.button2.DialogResult = System.Windows.Forms.DialogResult.No;
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用自己的自定义按钮和图标以及要显示的标签制作自定义消息框。 如下创建构造函数。 添加属性 DisplayData 来存储要显示的 messageBox 数据。

          public void CustomMessage(string title, string dataTodisplay, string leftButton, string rightButton, MessageBoxIcon iconSet)
          {
              // Set up some properties.
              this.Font = SystemFonts.MessageBoxFont;
              this.ForeColor = SystemColors.WindowText;
              InitializeComponent();
              DisplayData = dataTodisplay;
              // Do some measurements with Graphics.
              SetFormData(dataTodisplay);
      
              // Set the title, and some Text properties.
              if (string.IsNullOrEmpty(title) == false)
              {
                  this.Text = title;
              }
              // Set the left button, which is optional.
              if (string.IsNullOrEmpty(leftButton) == false)
              {
                  this.ButtonOK.Text = leftButton;                 
              }
              Else
              {
                this.AcceptButton = ButtonCancel
                this.ButtonCancel.Visible = False
              }
              // Set the PictureBox and the icon.
              if ((iconSet != null))
              {
                  ShowMessageBoxIcon(iconSet);
              }
      

      在此处将图标分配给图片框

       private void ShowMessageBoxIcon(MessageBoxIcon iconSet)
          {
              switch (iconSet)
              {
                  case MessageBoxIcon.Asterisk:
                      PictureBoxIconImage.Image =  Bitmap.FromHicon(SystemIcons.Asterisk.Handle);
                      break;
                  case MessageBoxIcon.Error:
                      PictureBoxIconImage.Image = Bitmap.FromHicon(SystemIcons.Error.Handle);
                  /*
                   * Add remaining icons here
                   * 
                   */
              }
             }
              this.ButtonCancel.Text = rightButton
            }
      

      【讨论】:

      • 我需要一个 c# 的代码,而不是 VB.NET
      • @ShaivyaSharma 找到编辑。我已经编辑了答案。
      猜你喜欢
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 2012-06-10
      • 2021-11-10
      相关资源
      最近更新 更多