Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Inputbox取消按钮不工作_C#_C# 4.0_Inputbox - Fatal编程技术网

C#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

我的浏览按钮上有以下代码。 如何为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();
    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或在属性框中进行设置(如果您始终使用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或在属性框中进行设置(如果您始终使用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键,你的整个问题都会得到回答。谢谢你的回答。你能告诉我需要添加哪些代码(来自我的问题)吗?如果其他条件编辑了我的答案以显示你的新代码应该是什么吗?你能告诉我需要添加哪些代码(来自我的问题)如果其他条件编辑了我的答案以显示你的新代码应该是什么吗