0
votes

I want a single button to fill in multiple text boxes if the text box above it is already filled. I.E. IF textbox1 = file.shp THEN textbox2 = openFileDialog

I tried doing something with the length function but when I selected a file both textbox1 and 2 were filled.

    private void button1_Click(object sender, EventArgs e)
    {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = @"C:\WCGIS\GEOG489\Final\SHP";
            openFileDialog1.Title = "Browse Text Files";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            openFileDialog1.DefaultExt = "txt";
            openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;

                if (textBox1.Text.Length > 1)
                {
                    textBox2.Text = openFileDialog1.FileName;
                }


        }

    }

enter image description here

I want to be able to have a new textbox populate everytime I click the "add Shapefile" button. If textbox1 is already occupied I want textbox2 to populate.

2
can you be more specific about what you are trying to do and what exactly is going wrong?user1666620

2 Answers

0
votes

You need to first check the length of your textBox like this:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
     if (textBox1.Text.Length <= 1)
     {
         textBox1.Text = openFileDialog1.FileName;
     }
     else
     {
         if (textBox2.Text.Length <= 1)
         {
             textBox2.Text = openFileDialog1.FileName;
         } 
         else
         {                   
             //and so on...
         }
    }
}
0
votes
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    textBox1.Text = openFileDialog1.FileName;

With this you always fill the first textbox. And afterwards

    if (textBox1.Text.Length > 1)
    {
        textBox2.Text = openFileDialog1.FileName;

This fills the second textbox.

Solution: Check if the first textbox is filled before you fill it:

if (textBox1.Text.Length > 0) // if textBox1 was already filled
    textBox2.Text = openFileDialog1.FileName; // fill textBox2
else // if textBox1 was still empty
    textBox1.Text = openFileDialog1.FileName; // fill textBox1 first