c# - 如何在 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 都会相互重叠。
推荐阅读
- xml - XML-TEI 如何调用两个属性
- javascript - 如何捕获 Google recaptchaV3 Promise Timeout?
- html - CSS网格中的Safari图像高度
- r - 在 R 中随机抽取地理空间数据的最佳方法
- python - 将networkx转换为igraph python但保留networkx图的节点ID
- r - 在 R 中将调用的 OSM 数据从 SF 转换为 Shapefile 的最佳方法
- python - Python Unicode 问题 - 如何将文本文件用作电子邮件正文?
- c# - 使用 XmlReader.Create(....) 时如何修复文件名或路径的外部控制
- python - 给定一个整数的单链表,一次反转链表“k”的节点并返回其修改后的列表
- boost-accumulators - 重置 Boost 参数化累加器