如何在不同的事件中使用字符串变量?
How to use string variable in different event?
我想知道如何 call/use 来自另一种方法的字符串。
public partial class PPAP_Edit : Form
{
string Main_dir { get; set; }
string Sub_dir { get; set; }
string targetPath { get; set; }
...等我不想全部复制
private void button_browse_Click(object sender, EventArgs e)
{
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
Main_dir = @"C:\Users\h109536\Documents\PPAP\";
Sub_dir = text_PSW_ID.Text + "_" + text_Partnumber.Text + "_" + text_Partrev.Text + @"\";
targetPath = System.IO.Path.Combine(Main_dir, Sub_dir);
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
MessageBox.Show("Folder has been created!");
}
foreach (string fileName in od.FileNames)
System.IO.File.Copy(fileName, targetPath + System.IO.Path.GetFileName(fileName), true);
}
catch (Exception ex)
{
MessageBox.Show("An error has occurred: " + ex.Message);
}
}
private void button_open_Click(object sender, EventArgs e)
{
if (!Directory.Exists(targetPath))
{
MessageBox.Show("Folder is not added to the database!");
System.Diagnostics.Process.Start("explorer.exe", Main_dir);
}
else
{
System.Diagnostics.Process.Start("explorer.exe", Main_dir + Sub_dir);
}
}
我指的是 Main_dir, Sub_dir 和 targetPath 字符串,但打开按钮方法在我单击浏览按钮之前不起作用。
提前感谢您的帮助!
从表单的构造函数中设置主目录的默认值。然后它将可用于表单中的任何方法。子路径和目标路径只是函数,可以放在属性 getter 方法中。
public partial class PPAP_Edit : Form
{
// set this from constructor
public string MainDir { get; set; }
// can't set this in constructor as it requires access to form controls, but can just use the getter
public string SubDir
{
get
{
return text_PSW_ID.Text + "_" + text_Partnumber.Text + "_" + text_Partrev.Text + @"\";
}
}
// again just use the getter
public string TargetPath
{
get
{
return Path.Combine(MainDir, SubDir);
}
}
// set defaults in constructor
public PPAP_Edit()
{
MainDir = @"C:\Users\h109536\Documents\PPAP\";
}
}
我想知道如何 call/use 来自另一种方法的字符串。
public partial class PPAP_Edit : Form
{
string Main_dir { get; set; }
string Sub_dir { get; set; }
string targetPath { get; set; }
...等我不想全部复制
private void button_browse_Click(object sender, EventArgs e)
{
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
Main_dir = @"C:\Users\h109536\Documents\PPAP\";
Sub_dir = text_PSW_ID.Text + "_" + text_Partnumber.Text + "_" + text_Partrev.Text + @"\";
targetPath = System.IO.Path.Combine(Main_dir, Sub_dir);
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
MessageBox.Show("Folder has been created!");
}
foreach (string fileName in od.FileNames)
System.IO.File.Copy(fileName, targetPath + System.IO.Path.GetFileName(fileName), true);
}
catch (Exception ex)
{
MessageBox.Show("An error has occurred: " + ex.Message);
}
}
private void button_open_Click(object sender, EventArgs e)
{
if (!Directory.Exists(targetPath))
{
MessageBox.Show("Folder is not added to the database!");
System.Diagnostics.Process.Start("explorer.exe", Main_dir);
}
else
{
System.Diagnostics.Process.Start("explorer.exe", Main_dir + Sub_dir);
}
}
我指的是 Main_dir, Sub_dir 和 targetPath 字符串,但打开按钮方法在我单击浏览按钮之前不起作用。
提前感谢您的帮助!
从表单的构造函数中设置主目录的默认值。然后它将可用于表单中的任何方法。子路径和目标路径只是函数,可以放在属性 getter 方法中。
public partial class PPAP_Edit : Form
{
// set this from constructor
public string MainDir { get; set; }
// can't set this in constructor as it requires access to form controls, but can just use the getter
public string SubDir
{
get
{
return text_PSW_ID.Text + "_" + text_Partnumber.Text + "_" + text_Partrev.Text + @"\";
}
}
// again just use the getter
public string TargetPath
{
get
{
return Path.Combine(MainDir, SubDir);
}
}
// set defaults in constructor
public PPAP_Edit()
{
MainDir = @"C:\Users\h109536\Documents\PPAP\";
}
}