2017-02-01 4 views
1

그래픽 종류의 SQL 편집기를 만들려고합니다. 그러나 표의 비주얼이 마음에 들지 않아 더 많이 추가하려고합니다. 상호 작용 (드래그/드롭 등).C# Windows Form : 반복적으로 생성 된 텍스트 상자를 반복하고 텍스트가 변경되었는지 확인합니다.

각 레코드를 기반으로 패널을 작성하고 테이블의 각 레코드를 기반으로 각 패널에 텍스트 상자를 추가했습니다. 지금 당장 붙어있는 것은 동적으로 생성 된 컨트롤을 반복하고 해당 상태를 확인하거나 상호 작용하는 개념입니다.

내가 구조화하는 방법에 문제가 있으면 알려주십시오.

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e) 
    { 
     groupBox1.Controls.Clear(); 
     string pDBString = null; 
     SqlConnection cnn; 
     pDBString = "Data Source=localhost\\" + Form1.host + ";Initial Catalog=" + Form1.db + ";Integrated Security=SSPI;"; 
     cnn = new SqlConnection(pDBString); 
     string sqlForProps = "select * from PROPS where user_id_txt ='" + comboBox1.SelectedItem.ToString() + "'"; 
     try 
     { 
      using (cnn) 
      { 
       cnn.Open(); 
       SqlCommand cmd = new SqlCommand(sqlForProps, cnn); 
       SqlDataReader sqlReader = cmd.ExecuteReader(); 

       int x = 0; 
       int count = 0; 
       while (sqlReader.Read()) 
       { 
        Panel panel = new System.Windows.Forms.Panel(); 
        panel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
        x += 30; 
        panel.Location = new System.Drawing.Point(3, x); 
        panel.Name = "panel" + count; 
        panel.Size = new System.Drawing.Size(519, 30); 
        panel.TabIndex = 3; 
        PropsPanels.Add(panel); 
        groupBox1.Controls.Add(panel); 

        TextBox textbox = new System.Windows.Forms.TextBox(); 
        panel.Controls.Add(textbox); 
        textbox.Location = new System.Drawing.Point(1, 3); 
        textbox.Name = "textBox" + count; 
        textbox.Size = new System.Drawing.Size(100, 20); 
        textbox.TabIndex = 4; 
        textbox.Text = sqlReader["USER_ID_TXT"].ToString(); 

        TextBox textboxAM = new System.Windows.Forms.TextBox(); 
         panel.Controls.Add(textboxAM); 
         textboxAM.Location = new System.Drawing.Point(126, 3); 
         textboxAM.Name = "textBoxAM" + count; 
         textboxAM.Size = new System.Drawing.Size(100, 20); 
         textboxAM.TabIndex = 4; 
         textboxAM.Text = sqlReader["PROP_TXT"].ToString(); 

        TextBox textboxAMSet = new System.Windows.Forms.TextBox(); 
         panel.Controls.Add(textboxAMSet); 
         textboxAMSet.Location = new System.Drawing.Point(232, 3); 
         textboxAMSet.Name = "textBoxAM" + count; 
         textboxAMSet.Size = new System.Drawing.Size(100, 20); 
         textboxAMSet.TabIndex = 4; 
         textboxAMSet.Text = sqlReader["VAL_TXT"].ToString(); 
        count++; 
       } 
       sqlReader.Close(); 
       cnn.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Can not open connection !"); 
     } 

    } 

패널을 점검 할 생각되는 코드 : : 예를 들어

 public AMMain() 
    { 

     InitializeComponent(); 
     string pDBString = null; 
     SqlConnection cnn; 
     pDBString = "Data Source=US7-AHACKETT\\SQLEXPRESS;Initial Catalog=OrchestroConfigurationDB;Integrated Security=SSPI;"; 
     MessageBox.Show(pDBString); 
     cnn = new SqlConnection(pDBString); 
     try 
     { 
      using (cnn) 
      { 
       SqlCommand sqlForUserList = new SqlCommand("select UserName from users a join Company b on a.CompanyID = b.CompanyID where CompanyCode='" + Form1.company + "'", cnn); 
       cnn.Open(); 
       MessageBox.Show("Connection Open !"); 
       SqlDataReader sqlReader = sqlForUserList.ExecuteReader(); 
       while (sqlReader.Read()) 
       { 
        comboBox1.Items.Add(sqlReader["UserName"].ToString()); 
       } 
       sqlReader.Close(); 
       cnn.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Can not open connection !"); 
     } 

     foreach (Panel p in PropsPanels) 
     { 
      foreach (Control c in p.Controls) 
      { 
       if(c is TextBox) 
       { 
        object sender = new object(); 
        EventArgs e = new EventArgs(); 
        if(c.TextChanged()??????) 
        { 
        //DOSOMETHING 
        } 
       } 
      } 
     } 
    } 

: 패널을 생성

코드를 다음과 같이

내 코드 인 경우 I 양식에 입력 한 텍스트 상자에서 텍스트가 변경되었는지 확인하고 싶습니다.

 private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

그래서 런타임에 Textbox를 생성 할 때 런타임에이를 확인하는 방법에 대해 머리를 감쌀 수 없습니다.

도움 주셔서 감사합니다. 예를 들어

+0

'comboBox1_SelectedIndexChanged_1' 메소드에서 텍스트 상자를 생성 할 때 해당 텍스트 상자의 TextChanged 매개 변수에 이벤트 핸들러를 할당하십시오. I.E :'textbox.TextChanged + = myEventHandler; ' –

답변

1

: 텍스트 내가 그렇게 할 것입니다 양식에 넣어 텍스트 상자에 대한 변경 한 경우 확인하기를 원한다면 :

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 

} 

그래서 내가 생각 나는 주위에 내 머리를 정리하고 수 없습니다 내가 런타임에 Textboxes를 생성하고 있기 때문에 나는 이것을 런타임에서 어떻게 확인할 것인가?

동일하게하십시오!

먼저 이벤트를 처리하는 방법을 만들기 : 당신이 당신의 양식을 초기화하면

private void TextBoxTextChanged(object sender, EventArgs e) { 
    // you can use the sender argument to check exactly which text box's text changed 
} 

을, 당신은이 작업을 수행 :

textbox.TextChanged += TextBoxTextChanged; 
textboxAM.TextChanged += TextBoxTextChanged; 
textboxAMSet.TextChanged += TextBoxTextChanged; 
+0

도움을 주셔서 감사합니다! "폼을 초기화 할 때 다음과 같이하십시오 :"라고 말하면됩니다. 그게 무슨 뜻인지 설명해 주시겠습니까? 실제 텍스트 상자를 만들 때 그 의미가 있습니까? 나는 그것을 선언해야합니까? – user249784

+0

기숙사 디자이너처럼 텍스트 상자의 이벤트에'TextBoxTextChanged' 이벤트 핸들러를 연결하면 호출됩니다. @ user249784 – Sweeper

+0

오, 무슨 말을하는지 알 겠어. 감사! – user249784

0

당신은이 같은 방법으로 할 수있는 당신이 떨어 것처럼 텍스트 상자를 수동으로 배치합니다. 새로운 TextBoxTextChanged 이벤트를 구독하기 만하면됩니다.

var x = new TextBox(); 

x.TextChanged += textBox1_TextChanged; 

이전 버전의 .Net에서는 대리인을 지정해야 할 수 있습니다. 자세한 내용은 MSDN on the subject을 참조하십시오.

+0

답장을 보내 주셔서 감사합니다! – user249784

0

컨트롤을 만들 때 TextChanged 대리자에 등록해야합니다.

TextBox textbox = new System.Windows.Forms.TextBox(); 
panel.Controls.Add(textbox); 
textbox.Location = new System.Drawing.Point(1, 3); 
textbox.Name = "textBox" + count; 
textbox.TextChanged += TextBox_TextChanged 

그런 다음, 이벤트 핸들러에서 이벤트를 트리거 컨트롤의 인스턴스를 캐스팅하기 위해 sender 매개 변수를 사용합니다.

private void(object sender, EventArgs e) 
    { 
     //get name of textbox 
     var tb = (TextBox) sender; 
     // do whatever with text box 
    } 
+0

감사합니다.이 말이 좋습니다. 나는 그것을 시도 할 것이다. – user249784