Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
               }
            }
        }




Thanks!
-pao :3
Posted
Updated 16-May-11 0:07am
v2
Comments
Sergey Alexandrovich Kryukov 16-May-11 3:40am    
What are the types of all those BrowseButton1, textBox1, etc.?
And I cannot see a problem. After all, run your code under debugger to see what's going on.
--SA
jcosep 16-May-11 3:44am    
What do you mean types sir?
Exactly sir, after I ran the debugger I notice that the file/data save on textBox1 will be overwrited by the file/data on textBox2.
Sergey Alexandrovich Kryukov 16-May-11 4:40am    
All variables have types. How come you cannot understand what am I asking about?
"textBox" only suggests its type is "TextBox", but it can be anything... and from what library? there are different types under this name, for your information. Just tell us the types. And make some code sample (simplified as much as possible) to show the problem.

Under debugger... if you used it properly you would see what line of code change what... The problem looks very simple, you just need to present it clearly.
--SA
jcosep 16-May-11 4:48am    
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);
}
}
}
Sergey Alexandrovich Kryukov 16-May-11 4:59am    
Excuse me, how can you ask question about BrowseButton1, BrowseButton2 if these identifiers are not even shown in your code. What's the point to show it?
--SA

1 solution

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:

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

replace with (i.e. no foreach loop)
C#
runProcess(textbox1.Text);
runProcess(textbox2.Text);


Hope it will help.

Thanks,
Hemant
 
Share this answer
 
Comments
Nithin Sundar 16-May-11 7:35am    
My 5! Good Answer. Can't believe that I didn't notice that one. The Filenames property contains only the currently selected files. Which probably means that whatever is in TextBox1 is overwritten.
Hemant__Sharma 16-May-11 7:52am    
Thanks @Nithin.
charles henington 16-May-11 16:53pm    
thats a 5 here commented above on the foreach loop but didn't scroll down to read other post good eye
Hemant__Sharma 17-May-11 1:56am    
Thanks a lot @charles.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900