我是C#的新手,但我正在做一些应用程序。
您可以添加应用程序(使用OpenFileDialog)。 它将添加的应用程序链接到按钮(提取文件图标和exe名称)。 然后,当您单击该按钮时,它将启动您添加的应用程序。
您最多可以添加5个应用程序(5个按钮),这一切都有效。但是如果你添加更多的应用程序,它只会打开最后添加的应用程序。
因此,如果我添加让我们说Word / Excel / Outlook,它将只在所有按钮上打开Outlook。
我的问题是:
如何制作它以便记住单击按钮时要打开的应用程序。
应用截图,以便您可以拥有视觉效果
http://s24.postimg.org/467l561dh/Untitled.png
代码
public partial class QuickStarter : Form
{
public QuickStarter()
{
InitializeComponent();
}
Icon ico = null;
OpenFileDialog ofd = new OpenFileDialog();
private void application1ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button1.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button1.Image = ico.ToBitmap();
button1.Enabled = true;
}
}
private void application2ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button2.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button2.Image = ico.ToBitmap();
button2.Enabled = true;
}
}
private void application3ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button3.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button3.Image = ico.ToBitmap();
button3.Enabled = true;
}
}
private void application4ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button4.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button4.Image = ico.ToBitmap();
button4.Enabled = true;
}
}
private void application5ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button5.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button5.Image = ico.ToBitmap();
button5.Enabled = true;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button2_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button3_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button4_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void button5_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @ofd.FileName;
Process.Start(start);
}
private void resetApplicationsToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.Text = "Application";
button1.Enabled = false;
button1.Image = null;
button2.Text = "Application";
button2.Enabled = false;
button2.Image = null;
button3.Text = "Application";
button3.Enabled = false;
button3.Image = null;
button4.Text = "Application";
button4.Enabled = false;
button4.Image = null;
button5.Text = "Application";
button5.Enabled = false;
button5.Image = null;
}
}
答案 0 :(得分:1)
您需要将文件名数据存储在某处以执行您想要执行的操作。目前,您正在为所选的每个应用程序重用OpenFileDialog
实例,因此仅保存所选最后一个文件的信息。
您可以添加私人字段来存储此信息。如果您只有5个按钮,则可以使用简单的数组。
public partial class QuickStarter : Form
{
public QuickStarter()
{
InitializeComponent();
}
Icon ico = null;
OpenFileDialog ofd = new OpenFileDialog();
// Added
string[] fileNames = new string[5];
现在在你的处理程序中添加一个按钮,你会添加一行
private void application1ToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(ofd.FileName);
button1.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button1.Image = ico.ToBitmap();
button1.Enabled = true;
// Added Line
fileNames[0] = ofd.FileName;
}
}
现在在你的on click按钮处理程序中,而不是从OpenFileDialog
中提取名称,而不是从数组中提取它。
private void button1_Click(object sender, System.EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = fileNames[0];
Process.Start(start);
}
现在您将打开正确的应用程序。请务必将每个处理程序的数组索引更改为正确的数字。
关于整体设计,你有很多重复的代码。您可以将所有这些代码减少到两个事件处理程序,根据object sender
是谁来存储数据。这也允许您稍后添加或删除按钮,而无需添加或删除其关联的事件处理程序。这还会为存储信息添加一些额外的逻辑。此时,List<string>
或Dictionary<string, string>
比基本数组更有用。
答案 1 :(得分:0)
您当前正在使用OpenFileDialogPath来运行该程序。这意味着您的按钮仅适用于最后添加的程序
你想要做的是,记住每个按钮的.exe-Path,如下所示:
Dictionary<Button,string> _exePaths = new Dictionary<Button,string>();
private void application1ToolStripMenuItem_Click(object sender, EventArgs e)
{
_userSelectExe(button1);
}
private void application2ToolStripMenuItem_Click(object sender, EventArgs e)
{
_userSelectExe(button2);
}
and so on
_userSelectExe方法:
private void _userSelectExe(Button button)
{
using(OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "EXE|*.exe";
ofd.Title = "Add application";
if (ofd.ShowDialog() == DialogResult.OK)
{
exePaths[button] = ofd.FileName;
button.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
button.Image = Icon.ExtractAssociatedIcon(ofd.FileName).ToBitmap();
button.Enabled = true;
}
}
}
然后像这样开始每一个:
// Assign this eventhandler to every button
private void _allButton_Click(object sender, EventArgs e)
{
_startButton((Button) sender);
}
private void _startButton(Button button)
{
string path;
if(_exePaths.TryGetValue(button, out path))
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = path;
Process.Start(start);
} else MessageBox.Show("No Exe for this button defined!");
}