【问题标题】:How to fill multiple text boxes from the same button in c#如何从C#中的同一个按钮填充多个文本框
【发布时间】:2016-03-15 16:17:42
【问题描述】:

如果上面的文本框已经被填充,我想要一个按钮来填充多个文本框。 IE。 IF textbox1 = file.shp THEN textbox2 = openFileDialog

我尝试使用长度函数做一些事情,但是当我选择一个文件时,textbox1 和 2 都被填充了。

    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;
                }


        }

    }

我希望每次单击“添加 Shapefile”按钮时都能填充一个新的文本框。如果 textbox1 已被占用,我希望 textbox2 填充。

【问题讨论】:

  • 您能否更具体地说明您要做什么以及到底出了什么问题?

标签: c# .net arcobjects


【解决方案1】:

您需要首先检查文本框的长度,如下所示:

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...
         }
    }
}

【讨论】:

    【解决方案2】:
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = openFileDialog1.FileName;
    

    有了这个,你总是填充第一个文本框。之后

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

    这会填充第二个文本框。

    解决方案:检查第一个文本框是否在之前填充:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多