2017-02-17 35 views
0

我有簡單的窗體來獲取用戶詳細信息。如果用戶希望通過單擊「+」按鈕添加備用手機號碼,則應在其下方出現文本框,其他文本框應根據新添加的文本框重新定位自己的位置。 我可以在運行時動態添加文本框,但無法重新定位關於動態添加文本框的其他表單組件。以下是我的代碼和表單快照。提前致謝。動態添加和重新定位文本框

private void button1_Click(object sender, EventArgs e) 
{ 
    TextBox txtRun = new TextBox(); 
    txtRun.Name = "txtDynamic" + c++; 
    txtRun.Location = new System.Drawing.Point(90, 74 + (20 * c)); 
    txtRun.Size = new System.Drawing.Size(200, 25); 
    txtRun.Location.X = 90; 
    txtRun.Location.Y = 74; 
    this.Controls.Add(txtRun); 
} 

User details windows form

回答

0

試試這個,

int c = 0; // for uinque txtDynamic text creation 
private void button1_Click(object sender, EventArgs e) 
{ 
     TextBox txtDynamic = this.Controls.Find("txtDynamic" + c, true)[0] as TextBox; // find lastly added txtDynamic 

     TextBox txtRun = new TextBox(); 
     txtRun.Name = "txtDynamic" + ++c; 
     txtRun.Size = new System.Drawing.Size(100, 20); 
     txtRun.Location = new Point(txtDynamic.Location.X, txtDynamic.Location.Y + 35); // X axis will be same y will increase with count 35 


     foreach (Control item in this.Controls) 
     { 
      if (item.Location.Y >= txtRun.Location.Y){ // if there is an item that has greater Y location 
       item.Location = new Point(item.Location.X, txtRun.Location.Y + 35); // It should increase its value as 35 too. 
      } 
      this.Controls.Add(txtRun); 

     } 
} 

編輯1:拖放

好吧,我已經創建了控制,我不知道如果你以編程方式創建它們。 +按鈕將在美孚文本框後添加新的文本框。所以我拖動和下降的移動文本框將成爲最重要的一點。所以我給它的名字爲「txtDynamic0」,

enter image description here

戴上斷點button1_click,你c變量有不同,那麼0的出發點我估計值。

結果; enter image description here

希望幫助,

+0

我得到異常指數的陣列 –

+0

的我給移動文本框的ID爲txtDynamic0初始邊界之外。這就是爲什麼我使用++ c而不是C++。更改移動文本框的ID。這就是爲什麼你有數組異常的界限。 – Berkay

+0

ID?你的意思是文本框名稱 –

1

嘗試將所有控件(姓名,電子郵件,移動,城市等),以一個FlowLayoutPanel,並添加到您的窗口:

var panel = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown } 
panel.Controls.Add(namePanel); 
panel.Controls.Add(emailPanel); 
// etc 

當用戶點擊+按鈕,將新控件插入所需位置:

panel.Controls.Insert(3, newControlPanel); // add new control at index #3 

如果您還沒有這樣做,您可能需要將每個Label - TextBox對包裝在它自己的Panel中,以便流程佈局按預期工作。這可以通過編程來完成:

private void InitializeForm() 
{ 
    var layoutPanel = new FlowLayoutPanel(); 
    // todo: initialize flow layout panel here... 

    layoutPanel.Controls.Add(CreatePanel("Name")); 
    layoutPanel.Controls.Add(CreatePanel("Email")); 
    // etc 

    this.Controls.Add(layoutPanel); 
} 

private Panel CreatePanel(string labelText) 
{ 
    var label = new Label(labelText); 
    // todo: initialize label here... 

    var textBox = new TextBox(); 
    // todo: initialize textbox here... 

    var panel = new Panel(); 
    panel.Controls.Add(label); 
    panel.Controls.Add(textBox); 
    // todo: initialize panel here... 

    return panel; 
} 

由於每個面板以完全相同的方式添加的,這種方法也可以幫助你的形式看起來更加一致。例如,可以在一個位置更改邊距和填充。

0

每個Form對象有它的Controls哪裏列出&存儲所有的對象。您還accesing,並在此代碼加入您的TextBox

this.Controls.Add(txtRun); 

你可以決定,因爲有更多的解決方案是如何處理的情況。您可以通過所有控件,找到低於MobileTextBox的圖標,並將它們向下移動幾個像素。

或者您可以將MobileTextBox下方的控件分組到GroupBox(或其他分組控件),然後移動GroupBox

和/或是由@gt - FlowLayoutPanel提及的解決方案,它將以與WPF framework類似的方式處理佈局。

的某些代碼示例:

private void button1_Click(object sender, EventArgs e) 
{ 
    TextBox txtRun = new TextBox(); 
    txtRun.Name = "txtDynamic" + c++; 
    txtRun.Location = new System.Drawing.Point(90, 74 + (20 * c)); 
    this.Controls.Add(txtRun); 

    //removed some code for brevity 


    //1st solution 
    foreach(Control item in this.Controls) 
    { 
     //there can be some other condition 
     //based on e.g. name of TB 
     //or if the type is GroupBox with some name 

     if(item.Location.Y >= txtRun.Location.Y) 
      item.Location.Y = item.Location.Y + 25; 

    } 

}