【问题标题】:How can i use informations after create textBox in C#在 C# 中创建文本框后如何使用信息
【发布时间】:2014-01-15 05:37:07
【问题描述】:

我创建了手动添加文本框和按钮的函数。

这是我的实际脚本:

    private void OpenFileDialogButton_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string souborFilename = openFileDialog1.FileName;
            filePathText.Text = souborFilename;
        }
    }

    private void nextDialog_Click(object sender, EventArgs e)
    {
        if (calculate <= 7)
        {
            TextBox text = new TextBox();
            text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
            text.Size = new Size(194, 20);
            text.ReadOnly = true;
            text.Name = "filePathText" + "{calculate}";
            //MessageBox.Show(text.Name);
            this.Controls.Add(text);

            Button button = new Button();
            button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
            button.Size = new Size(33, 24);
            button.Text = "...";
            button.Click += new EventHandler(OpenFileDialogButton_Click);
            this.Controls.Add(button);

            this.nextDialog.Location = new Point(22, 49 + y);
        }
        else
        {
            this.nextDialog.Controls.Remove(nextDialog);
            this.nextDialog.Dispose();
            MessageBox.Show("Maximální možnost počtů přidaných souborů byla dosažena!");
        }

        y = y + 28;
        calculate++;
    }

当用户单击 nextDialog 按钮时,它会创建正确的下一个按钮和文本框,但所有按钮都具有相同的功能。每个按钮都有自己的文本框。

问题是每个按钮在使用 openFileDialog 后仍然会更改相同的文本框。我需要每个按钮只改变他自己的文本框。

所以我需要功能 “OpenFileDialogBu​​tton_Click” 方面的帮助。

正是这部分:

filePathText.Text = 这是我的默认文本框名称,而不是我开始使用手动添加文本框和按钮的功能。有必要让它动态化。

filePathText.Text = souborFilename;

这是我的问题的图片:

http://i.imgur.com/z698zAz.jpg http://i.imgur.com/PGJgvjl.jpg

【问题讨论】:

    标签: c# winforms button textbox openfiledialog


    【解决方案1】:

    您的按钮有一个Tag 属性,可以接受一个对象,尝试将您关联的TextBox 的名称放入其中,然后使用Controls.Find 方法找到具有该名称的TextBox。像这样的。

    您修改后的 NextDialog 方法:

    private void nextDialog_Click(object sender, EventArgs e)
    {
        if (calculate <= 7)
        {
            TextBox text = new TextBox();
            text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
            text.Size = new Size(194, 20);
            text.ReadOnly = true;
            text.Name = "filePathText" + "{calculate}";
            //MessageBox.Show(text.Name);
            this.Controls.Add(text);
    
            Button button = new Button();
            button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
            button.Size = new Size(33, 24);
            button.Text = "...";
            button.Tag = text.Name;  //Name of associated TextBox added to Tag Property
            button.Click += new EventHandler(OpenFileDialogButton_Click);
            this.Controls.Add(button);
    
            this.nextDialog.Location = new Point(22, 49 + y);
        }
        else
        {
            this.nextDialog.Controls.Remove(nextDialog);
            this.nextDialog.Dispose();
            MessageBox.Show("Maximální možnost počtů přidaných souborů byla dosažena!");
        }
    
        y = y + 28;
        calculate++;
    }
    

    您的事件处理程序:

    private void OpenFileDialogButton_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Button btn = sender as Button; //Get the Button that was clicked
            string souborFilename = openFileDialog1.FileName;
            this.Controls.Find((string)btn.Tag), true )[0].Text = souborFilename; //Find textbox that matches stored name
                                                                                 //since method returns an array you will
                                                                                 //have to access it threw an index.
        }
    }
    

    【讨论】:

    • 你的想法还是给我写这个错误:对象引用未设置为对象的实例。
    • 您是否像在 NextDialogClick eventHandler 中那样将文本框的名称分配给您的 Buttons.Tag 属性?
    【解决方案2】:

    最简单的方法是使用 TextBox 和 Button 创建自定义控件,然后在 nextDialog Click 上添加该控件。

    【讨论】:

      【解决方案3】:

      我建议您为正在创建的新按钮命名,并根据“OpenFileDialogBu​​tton_Click”中的发件人名称分配文本框。

      button.Name = "btn";
      
       private void OpenFileDialogButton_Click(object sender, EventArgs e)
          {
              OpenFileDialog openFileDialog1 = new OpenFileDialog();
      
              if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
              {
                  string souborFilename = openFileDialog1.FileName;
      
                  string s = ((Button)sender).Name;
      
                  if (s == "btn")
                  {
                      Newtextbox.Text = "";
                  }
                  else
                      filePathText.Text = souborFilename;
              }
          }
      

      【讨论】:

        【解决方案4】:

        尝试像这样更改动态 OpenFileDialogBu​​tton 按钮的事件处理程序:

        private void nextDialog_Click(object sender, EventArgs e)
        {
          ...
          TextBox text = new TextBox();
          text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
          text.Size = new Size(194, 20);
          text.ReadOnly = true;
          text.Name = "filePathText" + "{calculate}";
          //MessageBox.Show(text.Name);
          this.Controls.Add(text);
        
          Button button = new Button();
          button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
          button.Size = new Size(33, 24);
          button.Text = "...";
        
          // -- start changes
          button.Click += (o, args) => {
            using (var opf = new OpenFileDialog()) {
              if (opf.ShowDialog() == DialogResult.OK) {
                var souborFilename = opf.FileName;
                text.Text = souborFilename;
              }
            }
          };
          // -- end changes
        
          this.Controls.Add(button);
        
          this.nextDialog.Location = new Point(22, 49 + y);
          ...
        }
        

        【讨论】:

          【解决方案5】:
          private void OpenFileDialogButton_Click(object sender, EventArgs e)
              {
                  OpenFileDialog openFileDialog1 = new OpenFileDialog();
          
                  if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                  {
                      string souborFilename = openFileDialog1.FileName;
          
                      foreach (var control in this.Controls)
                      {
                          if (control is TextBox)
                          {
                              TextBox tb = control as TextBox;
                              Button b = sender as Button;
                              if(b != null && tb.Name.Equals("filePathText" + b.Name.Substring(b.Name.Count()-1,1)))
                              {
                                  tb.Text = souborFilename;
                              }
                          }
                      }
                      filePathText.Text = souborFilename;
                  }
              }
          
              private void nextDialog_Click(object sender, EventArgs e)
              {
                  if (calculate <= 7)
                  {
                      TextBox text = new TextBox();
                      text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
                      text.Size = new Size(194, 20);
                      text.ReadOnly = true;
                      text.Name = "filePathText" + calculate.ToString();
                      //MessageBox.Show(text.Name);
                      this.Controls.Add(text);
          
                      Button button = new Button();
                      button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
                      button.Size = new Size(33, 24);
                      button.Text = "...";
                      button.Click += new EventHandler(OpenFileDialogButton_Click);
                      button.Name = "filePathButton" + calculate.ToString();
                      this.Controls.Add(button);
          
                      this.nextDialog.Location = new Point(22, 49 + y);
                  }
                  else
                  {
                      this.nextDialog.Controls.Remove(nextDialog);
                      this.nextDialog.Dispose();
                      MessageBox.Show("Maximální možnost počtů přidaných souborů byla dosažena!");
                  }
          
                  y = y + 28;
                  calculate++;
              }
          

          【讨论】:

            【解决方案6】:

            你可以有你的函数绑定像 -

            button.Click += OpenFileDialogButton_Click(text);
            

            你的功能可以像 -

            private EventHandler OpenFileDialogButton_Click(TextBox txt){ //Your code  }
            

            方便你每次传递新创建的文本框对象。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-11-13
              • 1970-01-01
              • 2021-08-09
              • 1970-01-01
              • 1970-01-01
              • 2011-03-31
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多