简体   繁体   English

如何在C#中创建textBox后使用信息

[英]How can i use informations after create textBox in C#

I created function for manually add textBox and Button. 我创建了手动添加textBox和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. 当用户单击nextDialog按钮以使其创建正确的下一个按钮和textboxt但所有按钮具有相同的功能。 Each button have own textBox. 每个按钮都有自己的textBox。

Problem is that every button still change same textBox after used openFileDialog. 问题是在使用openFileDialog之后,每个按钮仍然会改变相同的textBox。 And i need that each button change only his own textBox. 我需要每个按钮只更改自己的textBox。

So i need help with function "OpenFileDialogButton_Click" . 所以我需要帮助功能“OpenFileDialogBu​​tton_Click”

Exactly this part: 正是这部分:

filePathText.Text = This is my default TextBox name than i started use function for manually add textbox and button. filePathText.Text =这是我默认的TextBox名称,比我开始使用函数手动添加文本框和按钮。 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 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. 您的按钮具有可以获取Object的Tag属性,尝试将相关的TextBox名称放入其中,然后使用Controls.Find方法找到具有该名称的TextBox。 Something like this. 像这样的东西。

Your Modified NextDialog Method: 您修改的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++;
}

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. 我建议你给你正在创建的新按钮命名,并根据“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;
        }
    }

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: 尝试更改动态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);
  ...
}
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++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中获取主板和CPU电压信息? - How can I get motherboard and CPU Voltage informations in C#? 如何在C#的富文本框中创建换行符? - How can I create a newline in a rich textbox for C#? 将数据输入文本框后如何自动显示数据? C# - how can I show the data automatically after I input the data into a textbox? C# 在textBox中输入索引后如何选择listBox的项目c# - how i can selected item of listBox after enter the index in textBox c# 在C#单击按钮后,如何从DataGridView等于textbox.Text中选择一行? - How can I select a row from the DataGridView equals textbox.Text after clicking a button by C# ? 如何使用DirectX在C#中创建视频编辑器? - How can I use DirectX to create a video editor in C#? 如何在JavaScript函数中使用/引用所选值(TextBox)到后面的C​​#代码中? - How can I use/reference the selected value (TextBox) from a JavaScript Function into C# Code behind? 如何使用文本框编写用户密钥输入? (WPF,C#) - How can I use textbox to write the user key input? (WPF, C#) 如何使用ASP.NET TreeView显示信息 - How can I use ASP.NET TreeView to display informations 在WinForms C#2010中将文本框作为参数传递时如何在不使用文本框名称的情况下使用文本框 - How to use Textbox without using textbox name when I pass the textbox as parameter in WinForms C# 2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM