breekoy
breekoy

Reputation: 128

read the texts of run-time added textboxes

I have an application where the user can add any number of tabpages depending on what he/she needs. These tabpages have identical controls (textboxes and labels). The controls on each tabpages are named like this: (on tabpage1 the controls are named: txtServer1, txtPort1, txtUser1, txtDbName1. if the user clicks the 'add another connection button' the application creates a second tabpage and the controls will be named: txtServer2, txtPort2, txtUser2, txtDbName2) and so on...

for example if the user have multiple tabpages to set-up: connection1

the user clicks the 'add another connection' and the another tabpage with identical controls has been created and the user fills it up with appropriate data:

connection2

same thing goes here:

connection3

here's the code for dynamically added tabpage with controls:

//when 'add another connection' button is clicked
    private void btnAddConnection_Click(object sender, EventArgs e)
    {
        string tabTitle = "Connection " + (tabControl1.TabCount + 1).ToString();
        TabPage tabPage = new TabPage(tabTitle);
        tabControl1.TabPages.Add(tabPage);
    }

//when another tabpage has been added to tabcontrol
   private void tabControl1_ControlAdded(object sender, ControlEventArgs e)
    {
        //control instances
        TextBox txtServer = new TextBox();
        TextBox txtPort = new TextBox();
        TextBox txtUser = new TextBox();
        TextBox txtDbName = new TextBox();

        Label lblServer = new Label();
        Label lblPort = new Label();
        Label lblUser = new Label();
        Label lblDbName = new Label();

        tabControl1.SelectedTab = tabControl1.TabPages[tabControl1.TabCount - 1]; //select the newly addded tabpage
        tabControl1.SelectedTab.BackColor = tabPage1.BackColor; //tabpage background color

        //lblServer Properties
        lblServer.Location = lblServer1.Location;
        lblServer.Text = lblServer1.Text;
        lblServer.ForeColor = lblServer1.ForeColor;
        lblServer.Name = "lblServer" + tabControl1.TabCount.ToString();

        //lblPort Properties
        lblPort.Location = lblPort1.Location;
        lblPort.Text = lblPort1.Text;
        lblPort.ForeColor = lblPort1.ForeColor;
        lblPort.Name = "lblPort" + tabControl1.TabCount.ToString();

        //lblUser Properties
        lblUser.Location = lblUser1.Location;
        lblUser.Text = lblUser1.Text;
        lblUser.ForeColor = lblUser1.ForeColor;
        lblUser.Name = "lblUser" + tabControl1.TabCount.ToString();

        //lblDbName Properties
        lblDbName.Location = lblDbName1.Location;
        lblDbName.Text = lblDbName1.Text;
        lblDbName.ForeColor = lblDbName1.ForeColor;
        lblDbName.Name = "lblDbName" + tabControl1.TabCount.ToString();

        //txtserver properties
        txtServer.Location = txtServer1.Location;
        txtServer.Width = txtServer1.Width;
        txtServer.Height = txtServer1.Height;
        txtServer.Font = txtServer1.Font;
        txtServer.Name = "txtServer" + tabControl1.TabCount.ToString();

        //txtport properties
        txtPort.Location = txtPort1.Location;
        txtPort.Width = txtPort1.Width;
        txtPort.Height = txtPort1.Height;
        txtPort.Font = txtPort1.Font;
        txtPort.Name = "txtPort" + tabControl1.TabCount.ToString();

        //txtuser properties
        txtUser.Location = txtUser1.Location;
        txtUser.Width = txtUser1.Width;
        txtUser.Height = txtUser1.Height;
        txtUser.Font = txtUser1.Font;
        txtUser.Name = "txtUser" + tabControl1.TabCount.ToString();

        //txtdbname properties
        txtDbName.Location = txtDbName1.Location;
        txtDbName.Width = txtDbName1.Width;
        txtDbName.Height = txtDbName1.Height;
        txtDbName.Font = txtDbName1.Font;
        txtDbName.Name = "txtUser" + tabControl1.TabCount.ToString();

        //add controls to tabpage
        tabControl1.SelectedTab.Controls.Add(lblServer);
        tabControl1.SelectedTab.Controls.Add(lblPort);
        tabControl1.SelectedTab.Controls.Add(lblUser);
        tabControl1.SelectedTab.Controls.Add(lblDbName);
        tabControl1.SelectedTab.Controls.Add(txtServer);
        tabControl1.SelectedTab.Controls.Add(txtPort);
        tabControl1.SelectedTab.Controls.Add(txtUser);
        tabControl1.SelectedTab.Controls.Add(txtDbName);
    }

When the user clicks the save button, I want the application to read each text in the textboxes (except for that url text) so that I can save it to a configuration file.

all I can think of is this

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
   string connection;
   for (int i = 1; i <= tabControl1.TabCount; i++ )
   {
      connection = "server=" + txtServer[i].Text + ";port=" txtPort[i].Text + ";user=" + txtUser[i].Text + ";dbname=" + txtDbName[i];

      //save to config file code goes here...
   }
}

and I know this is not correct.

any solution for this? thanks :)

Upvotes: 0

Views: 97

Answers (4)

Mr. Leeds
Mr. Leeds

Reputation: 164

You can use an ArrayList to store and access all dynamically added controls. Set a name for each dynamically added controls. It can be based on the size of ArrayList.

I will not show the exact systax of c#.

first

declare an ArrayList or List(Type Safe)

List<Button> buttons = new List<Button>();

We just created a storage for our buttons that will be added at runtime.

void your_event (some parameters) {
     Button temp = new Button("Hello");
     temp.Name="button_name";
     YourMainPanel.Controls.add(temp);
     //after creating your button add it to the parent container. of any control where you want this button to be added.
     //you can also set the coordinates of the button if you like
     //after creating the button we need to add it to our List so that we can  
     //access it later, since the value of the 'temp' will change after this  
     //event was invoked in the future.
     buttons.add(temp);
}

There are several ways to get the items in List<T>. One is by using index.

void someMethod(some parameters) {
    Button button = buttons.ElementAt(index);
}

Upvotes: 0

devzero
devzero

Reputation: 2680

You could just loop over the controls and find the matching name, something like this:

foreach(var page in tabControl1.TabPages){
    var server = ProcessControls(page.Controls, "txtServer");
    //... continue with the others
}

private TextBox ProcessControls(Control ctrlContainer, string name) 
{ 
    foreach (Control ctrl in ctrlContainer.Controls) 
    { 
        if (ctrl.GetType() == typeof(TextBox)) 
        { 
            if(ctrl.Name.StartsWith(name))
                return (TextBox)ctrl;
        }
    } 
}

Upvotes: 0

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2710

This is the simplest way you can try this just use below function to get text from control in tabpage

public string getValue(string controlName, TabPage selectedTab)
{
  if (selectedTab.Controls.ContainsKey(controlName)){
    TextBox selectedtb = (TextBox)selectedTab.Controls[controlName];
    return selectedtb.Text;
    }
   else
     return null;
}

and in your save code use it like below

string connection;
int i = 1;
TabControl.TabPageCollection pages = tabControl1.TabPages;
      foreach (TabPage page in pages)
      {
         connection = "server=" + getValue("txtServer"+i,page) + ";port=" +getValue("txtPort"+i,page) + ";user=" + getValue("txtUser"+i,page) + ";dbname=" + getValue("txtDbName"+i,page);
         i++;
         //save to config file code goes here...
      }

Upvotes: 1

UMKalaycı
UMKalaycı

Reputation: 83

txtServer[i].Text it's true but You have not defined an array.

You can do it like this

    //global
        List<TextBox> txtServerList= new List<TextBox>();

private void tabControl1_ControlAdded(object sender, ControlEventArgs e)
    {
        txtServer.Location = txtServer1.Location;
        txtServer.Width = txtServer1.Width;
        txtServer.Height = txtServer1.Height;
        txtServer.Font = txtServer.Font;
        txtServer.Name = "txtServer" + tabControl1.TabCount.ToString();

        txtServerList.Add(txtServer)
        .
        .
        .

    }

Finally

for (int i = 1; i <= tabControl1.TabCount; i++ )
{
connection=txtServerList.get(i).Text + ......
}

Upvotes: 0

Related Questions