MessageBox.Show()方法需要有.NET框架文件中所提沒什麼可說的有關添加鏈接到一個消息框
但是任何東西dialog.However的標題,文字,圖標,按鈕和默認按鈕,你可以通過創建一個從System.Windows.Forms.Form繼承的新類並添加一個按鈕(或者更多如果你喜歡的),一個圖標,一個標籤和一個LinkButton來達到你想要的效果。然後使用Form類的ShowDialog()方法以模態形式顯示消息框。你也可以創建一個名爲MyErrorBox的類(C#2中的靜態類,或者僅用C#1封裝),它只包含一個名爲Show()的靜態方法,它創建一個表單,添加所需的控件並以模態模式顯示錶單。下面顯示了最後一種方法的演示。那麼你可以隨時隨地使用這門課並隨時隨地使用!
using System;
using System.Windows.Forms;
using System.Drawing;
namespace MessageBoxes{
public sealed class MyErrorBox{
private MyErrorBox(){}
private static Form frm;
private static string detailsStore;
private static TextBox txt;
public static DialogResult Show(string caption, string text, string details, Icon icon){
frm = new Form(); frm.Size = new Size(510, 195);
frm.Text = caption; frm.ShowInTaskbar = false; frm.ControlBox = false;
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
PictureBox icon1 = new PictureBox(); icon1.Location = new Point(8,16);
icon1.Size = new Size(icon.Width, icon.Height);
icon1.Image = icon.ToBitmap();
frm.Controls.Add(icon1);
Label lbl = new Label(); lbl.Text = text; lbl.Location = new Point(88,8);
lbl.Size = new Size(400,88); frm.Controls.Add(lbl);
LinkLabel btn1 = new LinkLabel(); btn1.Text = "View Details";
btn1.Size = new Size(72,23); btn1.Location = new Point(416,96);
btn1.Click += new EventHandler(btn1_Click); frm.Controls.Add(btn1);
//Ofcourse you can add more buttons than just the ok with more DialogResults
Button btn2 = new Button(); btn2.Text = "&Ok";
btn2.Size = new Size(72,23); btn2.Location = new Point(224,130);
btn2.Anchor = AnchorStyles.Bottom; frm.Controls.Add(btn2);
frm.AcceptButton = btn2; btn2.Click += new EventHandler(btn2_Click);
btn2.DialogResult = DialogResult.OK; detailsStore = details;
return frm.ShowDialog();
}
private static void btn1_Click(object sender, EventArgs e) {
frm.Size = new Size(510,320);
txt = new TextBox(); txt.Multiline = true;
txt.ScrollBars = ScrollBars.Both; txt.Text = detailsStore;
txt.Size = new Size(488,128); txt.Location = new Point(8,120);
txt.ReadOnly = true; frm.Controls.Add(txt);
LinkLabel lnk = (LinkLabel)(sender); lnk.Text = "Hide Details";
lnk.Click -= new EventHandler(btn1_Click);
lnk.Click += new EventHandler(btn1_ReClick);
}
private static void btn2_Click(object sender, EventArgs e) {
frm.Close();
}
private static void btn1_ReClick(object sender, EventArgs e) {
frm.Controls.Remove(txt); frm.Size = new Size(510, 195);
LinkLabel lnk = (LinkLabel)(sender); lnk.Text = "View Details";
lnk.Click -= new EventHandler(btn1_ReClick);
lnk.Click += new EventHandler(btn1_Click);
}
}
}
月LinkLabel的幫助 – wonko79
@當然wonko79,但'LinkLabel'應該被加入到標準的形式,而不是在MessageBox窗口。 –
我想你不能在.NET中擁有這樣一個'MessageBox',解決方案是自定義你自己的MessageBox或者使用第三方的MessageBox。 –