我想創建一個圖形SQL編輯器的種類 - 但我不喜歡一個表的視覺效果,並試圖添加更多交互性(拖放等)。C#Windows窗體:循環遍歷動態創建的文本框,並檢查,看看文本是否已更改
我已經完成並創建了基於每條記錄的面板,並根據我的表中的每條記錄向每個面板添加了文本框。我現在卡住的是通過動態創建的控件循環並檢查它們的狀態或與它們交互的概念。
請讓我知道,如果你看到我如何構建這個問題。
我的代碼如下:
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
}
}
}
}
}
例如:產生小組
代碼,如果我想檢查文本是否改變了我放在窗體上的文本框我會這樣做:
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
所以我想我不能包裹我的頭,因爲我在運行時創建文本框,所以我會在運行時檢查它。
感謝您的幫助!
在您的'comboBox1_SelectedIndexChanged_1'方法中創建文本框時,爲該文本框的TextChanged參數分配一個事件處理函數。 I.E:'textbox.TextChanged + = myEventHandler;' –