如何在C#中创建自定义消息?
我正在设计一个自定义消息框。此消息框将显示字符串列表。但这不是工作。你能帮我修一下吗。 你可以在图片中看到,我有一个包含7项的列表。当我点击“等级项目”时,消息框不显示任何项目。 这就是我需要的结果如何在C#中创建自定义消息?,c#,C#,我正在设计一个自定义消息框。此消息框将显示字符串列表。但这不是工作。你能帮我修一下吗。 你可以在图片中看到,我有一个包含7项的列表。当我点击“等级项目”时,消息框不显示任何项目。 这就是我需要的结果 //主窗体 私有void btnGrade_单击(对象发送者,事件参数e) { 列表结果=新列表(); 结果。添加(“真”); 结果。添加(“假”); 结果。添加(“真”); 结果。添加(“假”); 结果。添加(“真”); 结果。添加(“假”); 结果。添加(“假”); 显示(结果,“项目1”,M
//主窗体
私有void btnGrade_单击(对象发送者,事件参数e)
{
列表结果=新列表();
结果。添加(“真”);
结果。添加(“假”);
结果。添加(“真”);
结果。添加(“假”);
结果。添加(“真”);
结果。添加(“假”);
结果。添加(“假”);
显示(结果,“项目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;
}
//MsgBox表单
公共部分类MsgBox:表单
{
专用静态MsgBox_MsgBox;
//页眉、页脚
专用面板_plHeader=新面板();
自有品牌_lbltile;
专用面板_plFooter=新面板();
专用面板_plIcon=新面板();
//面板
私有FlowLayoutPanel_flpButtons=新的FlowLayoutPanel();
//钮扣
私有列表_buttonCollection=新列表();
//Kế曲靖ả
私有静态对话框结果_按钮结果;
//信息
私人列表信息;
私有MsgBox()
{
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;
this.BackColor=Color.FromArgb(45,45,48);
this.StartPosition=FormStartPosition.CenterScreen;
this.Padding=newsystem.Windows.Forms.Padding(3);
这个。宽度=800;
//标题
_lblTitle=新标签();
_lblTitle.ForeColor=颜色。白色;
_lblTitle.Font=新系统.Drawing.Font(“Segoe UI”,18);
_lblTitle.Dock=DockStyle.Top;
_lblTitle.高度=60;
//信息
_lblMessage=新列表();
对于(int i=0;i<\u lblMessage.Count;i++)
{
TextBox txt=new TextBox();//创建TextBox cóname txt
txt.Text=_lblMessage[i];
txt.ForeColor=Color.Red;
txt.Font=新系统.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=新的填充(20);
//_plHeader.Controls.Add(_lblMessage);
_plHeader.Controls.Add(_lbltite);
_plFooter.Dock=DockStyle.Bottom;
_plFooter.Padding=新的填充(20);
_plFooter.BackColor=Color.FromArgb(37,37,38);
_plFooter.Height=80;
_plFooter.Controls.Add(_flpButtons);
//添加控件váo表单
this.Controls.Add(_plHeader);
//this.Controls.Add(_plIcon);
this.Controls.Add(_plFooter);
}
公共静态对话框结果显示(列表消息、字符串标题、按钮)
{
_msgBox=新的msgBox();
_msgBox.lblMessage=消息;
_msgBox.\u lblTitle.Text=标题;
_msgBox._plIcon.Hide();
MsgBox.InitButtons(按钮);
_msgBox.ShowDialog();
返回按钮返回;
}
感谢您的收看。您的代码有问题:
private MsgBox()
{
...
// Message
_lblMessage = new List<string>();
...
}
private MsgBox()
{
...
//信息
_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;
....
}
专用MsgBox(列出消息)
{
...
//信息
_lblMessage=消息;
...
}
公共静态对话框结果显示(列表消息、字符串标题)
{
_msgBox=新的msgBox(消息);
//_msgBox.lblMessage=消息;
....
}
另外,你最好设置文本框的位置,否则所有的文本框都会相互重叠。
“Result”+string.Join(“,Result)
你的想法很聪明,我会试试看。
private MsgBox(List<String> messages)
{
...
// Message
_lblMessage = messages;
...
}
public static DialogResult Show(List<String> message, string title)
{
_msgBox = new MsgBox(message);
//_msgBox._lblMessage = message;
....
}