浏览按钮保存在文本框中 [英] Browse Button save in textBox

查看:108
本文介绍了浏览按钮保存在文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我会给你一个场景,你能告诉我是否可能是问题所在.

场景:
我有一个工具,它有两个(2)浏览按钮和两个(2)文本框,分别命名为BrowseButton1,textBox1,BrowseButton2和textBox2.现在,如果单击"BrowseButton1",则会出现对话框,然后选择一个文件,然后将其保存在textBox1上,然后单击"BrowseButton2",然后选择一个文件,它将被保存在textBox2上,但问题是单击"BrowseButton2"后它将覆盖在BrowseButton1上的数据.

能告诉我这是什么问题吗?

OP提供的代码:

Hi all,
I will give you a scenario could you tell me if what would likely be the problem.

Scenario:
I have a tool and it has two(2) Browse Button and two(2) textBox named as BrowseButton1, textBox1, BrowseButton2 and textBox2. Now, If I click BrowseButton1 the dialog box appears and then I will chose a file then it will save on textBox1 and then when I click BrowseButton2 and then choose a file it will be save on textBox2 but the problem is after clicking BrowseButton2 it will overwrite the data on BrowseButton1.

Could tell me what''s the problem with this?

Code provided by OP:

private void browseTranslationRequest_Click(object sender, EventArgs e)
        {

            openFileDialog.Filter = "Excel Files | *.xls;";
            openFileDialog.ShowDialog();

            textBox1.Text = openFileDialog.FileName;
        }

        private void browseTranslationDictionary_Click(object sender, EventArgs e)
        {

            openFileDialog.Filter = "Excel Files | *.xls;";
            openFileDialog.ShowDialog();

            textBox2.Text = openFileDialog.FileName;
        }

        private void runButton_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "")
            {
                MessageBox.Show("Please Enter a Translation Request File.");
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("Please Enter a Translation Dictionary.");
            }
            else
            {
                if (ExcelObject != null)
                {
                    ExcelObject.CloseXlsInstance();
                    p.Close();
                }


                foreach (String TheFileNamePlusPath in openFileDialog.FileNames)
               {
                   runProcess(TheFileNamePlusPath);
               }
            }
        }





谢谢!
-pao:3





Thanks!
-pao :3

推荐答案

你好@jcosep,

嗯,这就是我所了解的:

1-您有两个按钮可以浏览,有2个文本框显示使用相应按钮选择的文件名.

2-然后,您将具有某种运行进程"类型的按钮,并且单击此按钮后,您将要处理在上述文本框中选择的所有文件.

好吧,问题出在您的 foreach 循环中,您引用的是 openFileDialog 实例,我认为这是全局的,因此您最后单击的任何按钮都将覆盖其 FileNames 属性.相反,您应该使用文本框中的值:

Hello @jcosep,

Well this is what i''ve understood:

1- you have two buttons to browse and 2 textboxes to show the file name you have selected with respective button.

2- And then you have some "Run Process" type of button and on click of this button you want to process all the files selected in the above textboxes.

Well the problem is in your foreach loop you are referring openFileDialog instance, which i believe, is global, hence whatever button you will click in last will overwrite its FileNames property. Instead you should use the values in the textboxes:

foreach (String TheFileNamePlusPath in openFileDialog.FileNames)
{
    runProcess(TheFileNamePlusPath);
}


替换为(即没有foreach循环)


replace with (i.e. no foreach loop)

runProcess(textbox1.Text);
runProcess(textbox2.Text);



希望对您有所帮助.

谢谢,
Hemant



Hope it will help.

Thanks,
Hemant


这篇关于浏览按钮保存在文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆