【发布时间】:2010-06-17 06:53:38
【问题描述】:
如何创建无模式消息框?我是否必须创建自己的 Windows 窗体类并使用它?如果是这样,是否有一种简单的方法可以添加警告图标(而不是插入我自己的图像)并根据文本量调整大小?
【问题讨论】:
标签: c# .net messagebox modeless
如何创建无模式消息框?我是否必须创建自己的 Windows 窗体类并使用它?如果是这样,是否有一种简单的方法可以添加警告图标(而不是插入我自己的图像)并根据文本量调整大小?
【问题讨论】:
标签: c# .net messagebox modeless
如果您需要一个在代码继续在后台运行时仅显示自身的消息框(该框仍然是模态的,并且会阻止用户在单击“确定”之前使用其他窗口),您始终可以在它自己的线程并继续执行您在原始线程中所做的一切:
// Do stuff before.
// Start the message box -thread:
new Thread(new ThreadStart(delegate
{
MessageBox.Show
(
"Hey user, stuff runs in the background!",
"Message",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
})).Start();
// Continue doing stuff while the message box is visible to the user.
// The message box thread will end itself when the user clicks OK.
【讨论】:
MessageBox.Show,但不能构造和显示整个Form?是否有任何.NET 方法可以使用表单模拟MessageBox? (也许我需要一个比标准消息框有更多选择的消息框)
new Thread(() => { MessageBox.Show("Hi"); }).Start();
您必须创建一个表单并使用Show() 将其显示为无模式。 MessageBox.Show(...) 在 ghiboz 的示例中表现出模态; “消息描述”会一直显示,直到用户按下按钮。
使用MessageBox.Show(...),您会在消息框关闭后立即获得结果;使用无模式的消息框,您的代码必须具有一种机制,例如当用户最终在您的消息框上选择某些内容时做出反应的事件。
【讨论】:
不写代码,你可以创建一个小表单,在构造函数中执行以下操作
SystemIcons.ApplicationSystemIcons.AsterixSystemIcons.ErrorSystemIcons.ExclamationSystemIcons.HandSystemIcons.InformationSystemIcons.QuestionSystemIcons.ShieldSystemIcons.WarningSystemIcons.WinLogo如果你真的想要,你可以监听按下 OK 按钮时触发的事件。
【讨论】:
您可以使用SystemIcons 使用标准系统警告图标
【讨论】:
您必须使用表单并调用 showDialog()
对于图标使用
MessageBoxIcon.Warning
【讨论】:
ShowDialog 将使其成为模态。 Show 代表无模式,我相信。
注意:这将创建一个模态对话框,这不是问题要问的内容
这是一个示例代码
if (MessageBox.Show("Description of the message", "Caption text", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
// Do some stuff if yes pressed
}
else
{
// no pressed
}
【讨论】:
//没有通讯
object sync = new object();
ManualResetEvent Wait = new ManualResetEvent();
//you should create a place holder named MessageData for Message Data.
List<MessageData> Messages = new List<MessageData>();
internal void ShowMessage(string Test, string Title, ....)
{
MessageData MSG = new MessageData(Test, Title);
Wait.Set();
lock(sync) Messages.Add(MSG);
}
// another thread should run here.
void Private_Show()
{
while(true)
{
while(Messsages.Count != 0)
{
MessageData md;
lock(sync)
{
md = List[0];
List.RemoveAt(0);
}
MessageBox.Show(md.Text, md.Title, md....);
}
Wait.WaitOne();
}
}
需要更多线程和更多代码(我没有足够的时间来编写)并发消息框。
【讨论】: