Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to create application for runtime for create controls and functionality, controls created successfully but value not storing in text box according folder dialog.

Please help me.
How can be resolve that?

Thanks
Ankit Agarwal
Software Engineer

What I have tried:

C#
private void btnBrowse_Click(object sender, EventArgs e)
        {
            //LoadControls();
            ChooseFolder();
        }

        public void ChooseFolder()
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {

                TextBox textbox = new TextBox();
                textbox.Text = folderBrowserDialog1.SelectedPath;
                MessageBox.Show(textbox.Text);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int count = panel1.Controls.OfType<Label>().ToList().Count;
            TextBox textbox = new TextBox();
            count = panel1.Controls.OfType<TextBox>().ToList().Count;
            textbox.Location = new Point(3, 25 * count);
            textbox.Size = new Size(188, 20);
            textbox.Name = "textbox_" + (count + 1);
            textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
            panel1.Controls.Add(textbox);

            Button button = new Button();
            //count = panel1.Controls.OfType<Button>().ToList().Count;
            button.Location = new Point(207, 25 * count);
            button.Size = new Size(75, 23);
            button.Name = "button_" + (count + 1);
            button.Text = "Browse " + (count + 1);
            button.Click += new System.EventHandler(this.btnBrowse_Click);
            panel1.Controls.Add(button);
        }
Posted
Updated 22-Dec-16 19:31pm

1 solution

Hi Ankit,

The textbox in the btnAdd_Click() event is not the same as the textbox in ChooseFolder() method. That is, the textbox in ChooseFolder() method is being instantiated and storing the value, but as soon as it gets out of the method, it perishes. So it doesn't store the value in the btnAdd_Click() event as that textbox is totally unaware of the other textbox.

I guess your business logic is:

1) Store the value of the path on 'Browse' button click.
2) Create array of controls and add the path in the textbox on 'Add' button click.

If this is the case, then the following changes will do.
C#
string TextBoxValue = "";
public void ChooseFolder()
{
    FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        TextBoxValue = folderBrowserDialog1.SelectedPath;   // Storing the value in a temporary string.
}
Then add the line as indicated in the code below.
C#
private void btnAdd_Click(object sender, EventArgs e)
{
    int count = panel1.Controls.OfType<Label>().ToList().Count;
    TextBox textbox = new TextBox();
    count = panel1.Controls.OfType<TextBox>().ToList().Count;
    textbox.Location = new Point(3, 25 * count);
    textbox.Size = new Size(188, 20);
    textbox.Name = "textbox_" + (count + 1);
    textbox.Text = TextBoxValue;    // This line was added.
    textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
    ...............................
    ...............................
I hope that will solve the problem.
 
Share this answer
 
v2
Comments
Agarwal1984 23-Dec-16 2:11am    
How to add ChooseFolder(ref ?) textbox?.
Agarwal1984 23-Dec-16 2:13am    
When i TextBox tb=new TextBox();
and pass ChooseFolder(ref tb);
first browse button or folderBrowserDialog1 value store in second textbox.
how can we resolve?
Agarwal1984 23-Dec-16 2:15am    
Hello
Agarwal1984 23-Dec-16 2:17am    
hi
Agarwal1984 23-Dec-16 2:49am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900