我正在用C#創建一個應該從不同程序調用的公共方法的dll。當程序調用這個方法時:'ShowMsg',它應該創建並顯示一個小表單(基本上是一個警告彈出框),並且根據消息的類型以特定的顏色顯示特定的消息。 表單應該保持可見,直到調用程序再次用不同的參數調用ShowMsg爲止,同樣的表單應該更新它的消息和顏色。C#class libary在單獨的線程上顯示和更新表單
我現在在調用程序是這樣的:
//Create the form here but do not show it yet
AlertMsg alertMsg = new AlertMsg();
//Show the form with this message
alertMsg.ShowMsg("message 1", "message 1 description", AlertMsg.MsgTypes.Warning);
//do lengthy stuff, simulated with a sleep here
System.Threading.Thread.Sleep(4000);
//Update the form showing this message
alertMsg.ShowMsg("message 2", "message 2 description", AlertMsg.MsgTypes.Critical);
//do lengthy stuff, simulated with a sleep here
System.Threading.Thread.Sleep(4000);
我已經創造了從「ALERTMSG」,這看起來應該的方式。當我第一次打電話給.ShowMsg時,它也會顯示出來。只有表格上的標籤保持白色。另外,當我下次調用.ShowMsg時不會更新表單。
我認爲要解決這個問題,我應該創建一個單獨的線程上的窗體,但我不知道如何做到這一點。有人能指出我正確的方向嗎?
非常感謝。
我試過.Refresh提示,我得到了,現在我有這個。以上仍然是調用程序,並保持不變。現在
,在我的課ALERTMSG我(出留下了一些代碼爲簡潔起見)有這樣的:
public class AlertMsg
{
frmAlertMsg _frmAlertMsg;
public AlertMsg()
{
_frmAlertMsg = new frmAlertMsg();
}
public void Show(string header, string text, MsgTypes msgType)
{
_frmAlertMsg.showMsg(header, text, msgType);
}
}
並在窗體類我有這樣的:
private void frmAlertMsg_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - 420, Screen.PrimaryScreen.WorkingArea.Bottom - 120);
}
internal void showMsg(string header, string text, AlertMsg.MsgTypes typeM)
{
//left out setting the colors part here
lblHeader.Text = header;
lblDescription.Text = text;
this.Show();
this.Refresh();
}
這現在完美地顯示了標籤設置爲我給它的文本的形式。當我再次調用showMsg時,它也會更新。
所以再次感謝你。
聽起來好像你是對的,你需要一個單獨的線程。首先用谷歌搜索「c#線程示例」,並嘗試一些在那裏的東西。如果遇到特定問題並無法解決問題,請回到SE併發布問題和問題代碼。 – 2014-09-01 18:49:47
出於好奇,您是否在不顯示的控件上嘗試過一個簡單的「.Refresh()」?它在幾乎類似的情況下爲我工作。 – Larry 2014-09-01 19:19:42
謝謝你所有的答案。其實,首先我只是嘗試.Refresh技巧,它的工作原理。實際上就像一個魅力! – Mytzenka 2014-09-02 16:32:12