在c#的消息表单中显示指向路径的链接
本文关键字:路径 显示 链接 消息 表单 | 更新日期: 2023-09-27 18:19:13
我有一个c#桌面应用程序。在这个程序中,我有一个显示不同消息的表单。我有一条消息说:"输出文件是在:C:'Work'result.txt中生成的"。我如何将此路径显示为文件的链接,以及何时显示带有此消息的表单以将路径显示为链接,以及用户单击链接以打开指定的路径/文件?
I tried:
The output file was generated in : <a href=/" C:'Work'result.txt/">C:'Work'result.txt</a>
但是不工作
谢谢!
你可以有一个on-onclick事件,然后你可以使用下面的代码打开文件。
System.Diagnostics.Process.Start(@"C:'Work'result.txt"); //or like
System.Diagnostics.Process.Start(@"C:'Work'result.docx");
这里,文件的默认程序必须存在。然后只有shell会运行相关程序从注册表中读取它,就像通常在资源管理器中双击一样。
MessageBox.Show()方法获取对话框的标题、文本、图标、按钮和默认按钮。然而,在。net框架文档中没有提到任何关于向MessageBox
添加链接的内容。然而,你可以通过创建一个继承自System.Windows.Forms.Form的新类来达到你想要的效果,并添加一个按钮(或者更多,如果你喜欢),一个图标,一个标签和一个LinkButton。然后使用Form类的ShowDialog()方法以模态形式显示消息框。你也可以创建一个名为MyErrorBox的类(c# 2中的静态类或c# 1中的封闭类),它只包含一个名为Show()的静态方法,该方法创建表单,添加所需的控件并以modal模式显示表单。最后一种方法的演示如下所示。然后你就可以随时随地使用这个课程了!
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);
}
}
}
没有标准的MessageBox功能可以通过链接标签来实现这一点。我的建议是使用是/否消息框按钮,然后从选择的选项中应用事件
像这样:
if (MessageBox.Show(
"The file is saved at the following link: link here", "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk
) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(@"C:'TestLocation'SavedFiles");
}