2011-11-01 11 views
5

मैं 3 कॉलम और 1 पंक्ति के साथ एक TableLayoutPanel है के बीच में पंक्तियों को जोड़ने कैसे करें: (बटन, उपयोगकर्ता नियंत्रण निकालें, जोड़ें बटन)एक TableLayoutPanel

मैं जोड़ें बटन के लिए इसी तरह एक नई पंक्ति जोड़ना चाहते हैं नीचे से ऊपर क्लिक किए गए बटन: जैसे: से पहले:

  1. (बटन 1, उपयोगकर्ता नियंत्रण 2 निकालें, बटन 1 जोड़े)
  2. (बटन 2, उपयोगकर्ता नियंत्रण 2 निकालें, बटन 2 जोड़े)

"बटन 1 जोड़ें" पर क्लिक करने के बाद:

  1. (बटन 1, उपयोगकर्ता नियंत्रण 2 निकालें, जोड़ें बटन 1)
  2. (बटन 3, उपयोगकर्ता नियंत्रण 3 निकालें, जोड़ें बटन 3)
  3. (निकालें बटन 2, उपयोगकर्ता नियंत्रण 2, बटन जोड़ें 2)

मैं टेबललेउपनेल के अंत में पंक्ति जोड़ने में कामयाब रहा लेकिन मध्य तक नहीं: यह लेआउट को खराब कर रहा है।

void MySecondControl::buttonAdd_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    int rowIndex = 1 + this->tableLayoutPanel->GetRow((Control^)sender); 

    /* Remove button */ 
    Button^ buttonRemove = gcnew Button(); 
    buttonRemove->Text = "Remove"; 
    buttonRemove->Click += gcnew System::EventHandler(this, &MySecondControl::buttonRemove_Click); 

    /* Add button */ 
    Button^ buttonAdd = gcnew Button(); 
    buttonAdd->Text = "Add"; 
    buttonAdd->Click += gcnew System::EventHandler(this, &MySecondControl::buttonAdd_Click); 

    /*Custom user control */ 
    MyControl^ myControl = gcnew MyControl(); 

    /* Add the controls to the Panel. */ 
    this->tableLayoutPanel->RowCount += 1; 
    this->tableLayoutPanel->Controls->Add(buttonRemove, 0, rowIndex); 
    this->tableLayoutPanel->Controls->Add(myControl, 1, rowIndex); 
    this->tableLayoutPanel->Controls->Add(buttonAdd, 2, rowIndex); 
} 

यह ठीक से काम नहीं करता है: यहाँ ईवेंट हैंडलर का एक टुकड़ा है।

क्या मैं कुछ गलत कर रहा हूं? कोई सुझाव?

उत्तर

6

अंत में एक समाधान पाया: इसके बजाय सीधा स्थान उनका करने के लिए नियंत्रण जोड़ने का है, मैं उन्हें समाप्त करने के लिए जोड़ रहा और उसके बाद इच्छित स्थान पर नियंत्रण स्थानांतरित करने के लिए SetChildIndex() समारोह का उपयोग करें:

void MySecondControl::buttonAdd_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    int childIndex = 1 + this->tableLayoutPanel->Controls->GetChildIndex((Control^)sender); 

    /* Remove button */ 
    Button^ buttonRemove = gcnew Button(); 
    buttonRemove->Text = "Remove"; 
    buttonRemove->Click += gcnew System::EventHandler(this, &MySecondControl::buttonRemove_Click); 

    /* Add button */ 
    Button^ buttonAdd = gcnew Button(); 
    buttonAdd->Text = "Add"; 
    buttonAdd->Click += gcnew System::EventHandler(this, &MySecondControl::buttonAdd_Click); 

    /*Custom user control */ 
    MyControl^ myControl = gcnew MyControl(); 

    /* Add the controls to the Panel. */ 
    this->tableLayoutPanel->Controls->Add(buttonRemove); 
    this->tableLayoutPanel->Controls->Add(myControl); 
    this->tableLayoutPanel->Controls->Add(buttonAdd); 

    /* Move the controls to the desired location */ 
    this->tableLayoutPanel->Controls->SetChildIndex(buttonRemove, childIndex); 
    this->tableLayoutPanel->Controls->SetChildIndex(myControl, childIndex + 1); 
    this->tableLayoutPanel->Controls->SetChildIndex(buttonAdd, childIndex + 2); 
} 
+0

+1 साझा करने के लिए बहुत बहुत धन्यवाद, एल्डड! मेरे साथ भी ठीक यही समस्या आई। यह अभी कोई समझ में नहीं आया कि शुरुआत में तत्वों को जोड़ने और संग्रह के अंत में काम क्यों शुरू हुआ लेकिन शुरुआत और अंत के बीच कहीं भी असफल रहा ... SetChildIndex न केवल बहुत अधिक कोड ले गया बल्कि एक आकर्षण की तरह काम करता है :) फिर से! – libjup

+0

खुशी मैं मदद कर सकता है :) – Eldad

संबंधित मुद्दे