简体   繁体   中英

How can i use informations after create textBox in C#

I created function for manually add textBox and Button.

Here is my actually scripts:

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

When user click on nextDialog button so its created correct next button and textboxt but all buttons have same functions. Each button have own textBox.

Problem is that every button still change same textBox after used openFileDialog. And i need that each button change only his own textBox.

So i need help with function "OpenFileDialogButton_Click" .

Exactly this part:

filePathText.Text = This is my default TextBox name than i started use function for manually add textbox and button. It is necessary to make it dynamic.

filePathText.Text = souborFilename;

Here is picture with my problems:

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

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

Your button has a Tag Property that can take an Object, try putting your associated TextBox's name in it, then use the Controls.Find Method to locate the TextBox that has that name. Something like this.

Your Modified NextDialog Method:

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

Your Event Handler:

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

I would suggest you to give name to the new button you are creating and based on the sender name in "OpenFileDialogButton_Click" assign the text box.

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

You can have your function binding like -

button.Click += OpenFileDialogButton_Click(text);

Your function can be like -

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

It will facilitate you to pass newly created textbox object every time.

Try changing the event handler for the dynamic OpenFileDialogButton button like this:

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);
  ...
}
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++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM