2017-09-15 21 views
0

我有一個應用程序,用戶可以根據他/她需要添加任何數量的標籤頁。這些標籤具有相同的控件(文本框和標籤)。 每個tabpages上的控件都是這樣命名的:(在tabpage1上,控件名爲:txtServer1,txtPort1,txtUser1,txtDbName1。如果用戶點擊'添加另一個連接按鈕',應用程序創建第二個tabpage並且控件將被命名:txtServer2,txtPort2,txtUser2,txtDbName2)等等...閱讀運行時添加文本框的文本

例如,如果用戶有多個的TabPages來設置: connection1

用戶點擊「添加另一個連接」和另一已創建具有相同控件的tabpage,並且用戶用適當的數據填充它:

connection2

同樣的事情會發生在這裏:

connection3

這裏與控件動態添加標籤頁中的代碼:

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

當用戶點擊保存按鈕,我想申請讀文本框中的每個文本(該url文本除外),以便我可以將其保存到配置文件中。

所有我能想到的是這個

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

,我知道這是不正確的。

任何解決方案?謝謝:)

+0

@Pio鏈接被破壞。 – breekoy

回答

0

txtServer [i] .Text它是真的,但你還沒有定義一個數組。

你可以像下面這樣做

//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) 
     . 
     . 
     . 

    } 

最後

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

你可以只在控制迴路並找到匹配的名稱,像這樣:

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

'return ctrl'引發錯誤:「不能隱式地將類型'System.Windows.Forms.Control'轉換爲'System.Windows.Forms.TextBox'。顯式轉換存在」 – breekoy

+0

嘗試轉換它,就像我在編輯示例。 – devzero

+0

現在'page.controls'有錯誤。 'object'不包含'controls'的定義,並且沒有擴展方法'controls'接受'object'類型的第一個參數。 – breekoy

1

這是最簡單的方法,你可以試試這個只是使用下面的函數來獲取文本從控制頁面

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

,並在您保存代碼中使用它就像下面

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

'getValue'總是返回null – breekoy

+0

它的原因是它無法找到您的標籤頁中的控件,這意味着您提供了錯誤的控件名稱,因此您可以顯示錯誤跟蹤。 –

0

您可以使用ArrayList來存儲和訪問所有動態添加控件。爲每個動態添加的控件設置一個名稱。它可以基於ArrayList的大小。

我不會顯示c#的確切systax。

第一

聲明一個ArrayList或者List(類型安全)

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

我們剛剛創造了我們的按鈕,將在運行時添加存儲。

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

有幾種方法可以獲得List<T>中的物品。一個是通過使用索引。

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

我可以要求樣品嗎?謝謝 :) – breekoy