C#Inputbox取消按钮不工作
我的浏览按钮上有以下代码。 如何为inputbox取消按钮编写代码C#Inputbox取消按钮不工作,c#,c#-4.0,inputbox,C#,C# 4.0,Inputbox,我的浏览按钮上有以下代码。 如何为inputbox取消按钮编写代码 private void btnbrowse_Click(object sender, EventArgs e) { String sf_no = Microsoft.VisualBasic.Interaction.InputBox( "You are uploading File For SF NO. ", "Information", def, -1, -1); ofd.ShowDialog
private void btnbrowse_Click(object sender, EventArgs e)
{
String sf_no = Microsoft.VisualBasic.Interaction.InputBox(
"You are uploading File For SF NO. ", "Information", def, -1, -1);
ofd.ShowDialog();
ofd.Multiselect = true;
string[] result = ofd.FileNames;
foreach (string y in result)
{
String path = y.Substring(0, y.LastIndexOf("\\"));
String filename = y.Substring(y.LastIndexOf("\\"));
string[] row = new string[] { sf_no,path, filename };
dataGridView2.Rows.Add(row);
}
}
在取消InputBox时,返回值是一个空字符串,因此您的代码将是
if (sf_no!="")
{
//ok stuff here, including the showdialog logic as shown below
}
{
//cancel stuff here
}
由于ofd.ShowDialog
也可以取消,因此您的代码应为:
if (ofd.ShowDialog()==DialogResult.OK)
{
//do stuff on OK button
}
else
{
//do stuff on Cancel button
}
调用ofd.Multiselect=true在调用ShowDialog()之前进行编码>或在属性框中进行设置(如果您始终使用Multiselect)
因此,您的新代码现在是:
private void btnbrowse_Click(object sender, EventArgs e)
{
String sf_no = Microsoft.VisualBasic.Interaction.InputBox("You are uploading File For SF NO. ", "Information", def, -1, -1);
if (sf_no!="") //we got the sf_no
{
ofd.Multiselect = true;
if (ofd.ShowDialog()==DialogResult.OK)//user select file(s)
{
string[] result = ofd.FileNames;
foreach (string y in result)
{
String path = System.IO.Path.GetDirectoryName(y);
String filename = System.IO.Path.GetFileName(y);
string[] row = new string[] { sf_no,path, filename };
dataGridView2.Rows.Add(row);
}
}
else
{
//handle what happen if user click cancel while selecting file
}
}
else
{
//handle what happen if user click cancel while entering SF NO
}
}
在取消InputBox时,返回值是一个空字符串,因此您的代码将是
if (sf_no!="")
{
//ok stuff here, including the showdialog logic as shown below
}
{
//cancel stuff here
}
由于ofd.ShowDialog
也可以取消,因此您的代码应为:
if (ofd.ShowDialog()==DialogResult.OK)
{
//do stuff on OK button
}
else
{
//do stuff on Cancel button
}
调用ofd.Multiselect=true在调用ShowDialog()之前进行编码>或在属性框中进行设置(如果您始终使用Multiselect)
因此,您的新代码现在是:
private void btnbrowse_Click(object sender, EventArgs e)
{
String sf_no = Microsoft.VisualBasic.Interaction.InputBox("You are uploading File For SF NO. ", "Information", def, -1, -1);
if (sf_no!="") //we got the sf_no
{
ofd.Multiselect = true;
if (ofd.ShowDialog()==DialogResult.OK)//user select file(s)
{
string[] result = ofd.FileNames;
foreach (string y in result)
{
String path = System.IO.Path.GetDirectoryName(y);
String filename = System.IO.Path.GetFileName(y);
string[] row = new string[] { sf_no,path, filename };
dataGridView2.Rows.Add(row);
}
}
else
{
//handle what happen if user click cancel while selecting file
}
}
else
{
//handle what happen if user click cancel while entering SF NO
}
}
您应该使用
System.IO.Path.GetDirectoryName()
和System.IO.Path.GetFileName()
方法,而不是y.SubString()
调用,以使其更安全。我已经编写了,但它给出了errorPath=System.IO.Path.GetDirectoryName();filename=System.IO.Path.GetFileName();string[]行=新字符串[]{sf_no,path,filename};dataGridView2.Rows.Add(row);'@Shweta当然不行,使用GetDirectoryName(y)和GetFileName(y)。在GetDirectoryName和GetFileName上按F1键,它将向您解释一切。事实上,如果你按下输入框上的F1键,你的整个问题都会得到回答。感谢您的回答您应该使用System.IO.Path.GetDirectoryName()
和System.IO.Path.GetFileName()
方法,而不是y.SubString()
调用,以使其更安全。我已经编写了,但它给出了errorPath=System.IO.Path.GetDirectoryName();filename=System.IO.Path.GetFileName();string[]行=新字符串[]{sf_no,path,filename};dataGridView2.Rows.Add(row);'@Shweta当然不行,使用GetDirectoryName(y)和GetFileName(y)。在GetDirectoryName和GetFileName上按F1键,它将向您解释一切。事实上,如果你按下输入框上的F1键,你的整个问题都会得到回答。谢谢你的回答。你能告诉我需要添加哪些代码(来自我的问题)吗?如果其他条件编辑了我的答案以显示你的新代码应该是什么吗?你能告诉我需要添加哪些代码(来自我的问题)如果其他条件编辑了我的答案以显示你的新代码应该是什么吗