繁体   English   中英

如何在 C# 中创建自定义消息?

[英]How to create custom message in C#?

我正在设计一个自定义消息框。 此消息框将显示字符串列表。 但它不起作用。 你能帮我修一下吗。 您可以在图片中看到,我有一个包含 7 个项目的列表。 当我单击“成绩项目”时,消息框不显示任何项目。 在此处输入图片说明 这就是我需要的结果

在此处输入图片说明

//Main form
 private void btnGrade_Click(object sender, EventArgs e)
        {
            List<string> result = new List<string>();
            result.Add("True");
            result.Add("False");
            result.Add("True");
            result.Add("False");
            result.Add("True");
            result.Add("False");
            result.Add("False");
            MsgBox.Show(result,"Project 1",MsgBox.Buttons.OK);
        }

这是 msgbox 形式的代码

//MsgBox form
    public partial class MsgBox : Form
    {
       private static MsgBox _msgBox;

        // Header, Footer 
        private Panel _plHeader = new Panel();
        private Label _lblTitle;
        private Panel _plFooter = new Panel();
        private Panel _plIcon = new Panel();
        // Panel
        private FlowLayoutPanel _flpButtons = new FlowLayoutPanel();

        // button
        private List<Button> _buttonCollection = new List<Button>();

        // Kết quả
        private static DialogResult _buttonResult;
        // Message
        private List<String> _lblMessage;

        private MsgBox()
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.BackColor = Color.FromArgb(45, 45, 48);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Padding = new System.Windows.Forms.Padding(3);
            this.Width = 800;

            // Header
            _lblTitle = new Label();
            _lblTitle.ForeColor = Color.White;
            _lblTitle.Font = new System.Drawing.Font("Segoe UI", 18);
            _lblTitle.Dock = DockStyle.Top;
            _lblTitle.Height = 60;

            // Message
            _lblMessage = new List<string>();
            for (int i = 0; i < _lblMessage.Count; i++)
            {
                TextBox txt = new TextBox(); //Create Textbox có name txt
                txt.Text = _lblMessage[i];
                txt.ForeColor = Color.Red;
                txt.Font = new System.Drawing.Font("Segoe UI", 30);
                this.Controls.Add(txt);  //add control txt
            }

            _flpButtons.FlowDirection = FlowDirection.RightToLeft;
            _flpButtons.Dock = DockStyle.Fill;

            _plHeader.Dock = DockStyle.Fill;
            _plHeader.Padding = new Padding(20);
          //  _plHeader.Controls.Add(_lblMessage);
            _plHeader.Controls.Add(_lblTitle);

            _plFooter.Dock = DockStyle.Bottom;
            _plFooter.Padding = new Padding(20);
            _plFooter.BackColor = Color.FromArgb(37, 37, 38);
            _plFooter.Height = 80;
            _plFooter.Controls.Add(_flpButtons);                       

            // Add controls vào form
            this.Controls.Add(_plHeader);
            //this.Controls.Add(_plIcon);
            this.Controls.Add(_plFooter);

        }
        public static DialogResult Show(List<String> message, string title, Buttons buttons)
        {
            _msgBox = new MsgBox();
            _msgBox._lblMessage = message;
            _msgBox._lblTitle.Text = title;
            _msgBox._plIcon.Hide();
            MsgBox.InitButtons(buttons);
            _msgBox.ShowDialog();          
            return _buttonResult;
        }

感谢您的观看。

您的代码有问题:

private MsgBox()
{
    ...
    // Message
    _lblMessage = new List<string>();
    ...
}

您的 _lblMessage 将始终是一个空列表,因此您根本看不到任何消息。

您可以像这样更改代码:

private MsgBox(List<String> messages)
{
    ...
    // Message
    _lblMessage = messages;
    ...
}

public static DialogResult Show(List<String> message, string title)
{
    _msgBox = new MsgBox(message);
    //_msgBox._lblMessage = message;
    ....
}

而且,你最好设置 TextBox 的位置,否则所有的 TextBox 都会相互重叠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM