我有 SQLite 資料庫,其中有一個包含一定數量行的表。我的任務是在考慮到這些控制元件的坐標的情況下以幾種形式顯示相同數量的文本框。這些文本框的名稱應取自該表的其中一列。標簽也必須存在。他們的名字應該相似。考慮到該表這一列的值是西里爾字母,我決定將其翻譯成 Decryptor 類中的拉丁符號。
我找到了以一種形式顯示必要文本框的解決方案,并且我能夠將此代碼復制到其他形式中。但這似乎不是一個好主意,盡管它在這種情況下會起作用。
我創建了新類 - CommunicationCanalsDrawing。該類的思想是減少代碼數量,增加該代碼的使用便利性。我的特定類代碼:
class CommunicationCanalsDrawing
{
public int Tx { get; set; }
public int Ty { get; set; }
public int Lx { get; set; }
public int Ly { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string[] Labels;
private string[] Textboxes;
public TextBox txt = new TextBox();
public Label l = new Label();
public int i { get; set; }
public void DrawThat()
{
Ly = 32;
Ty = 32;
Decryptor decr = new Decryptor();
decr.Word = Labels[i];
Textboxes[i] = decr.Decrypt();
txt = new TextBox();
txt.Name = Textboxes[i];
txt.Text = "";
txt.Width = Width;
txt.Height = Height;
txt.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)(204)));
txt.Location = new System.Drawing.Point(Tx, Ty);
l = new Label();
l.Name = "l_" Textboxes[i];
l.Text = Labels[i];
l.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)(204)));
l.Location = new System.Drawing.Point(Lx, Ly);
}
public void ListOfColumns()
{
SQLiteConnection m_dbConn = new SQLiteConnection();
SQLiteCommand m_sqlCmd = new SQLiteCommand();
int AmountTextbox = 0;
try
{
InceptDb cl = new InceptDb();
m_dbConn = new SQLiteConnection("Data Source="
cl.dbFileName ";Version=3;");
m_dbConn.Open();
m_sqlCmd.Connection = m_dbConn;
m_sqlCmd.CommandText = "SELECT COUNT(*) FROM communication_remedies";
AmountTextbox = Convert.ToInt32(m_sqlCmd.ExecuteScalar().ToString());
}
catch (SQLiteException ex)
{
MessageBox.Show("Error count*: " ex.Message);
}
m_dbConn.Close();
if (AmountTextbox == 0)
{
MessageBox.Show("Дов?дник канал?в зв'язку пустий.");
Organizations o = new Organizations();
o.Close();
}
m_dbConn = new SQLiteConnection();
m_sqlCmd = new SQLiteCommand();
Labels = new string[AmountTextbox];
Textboxes = new string[AmountTextbox];
try
{
InceptDb cl = new InceptDb();
m_dbConn = new SQLiteConnection("Data Source="
cl.dbFileName ";Version=3;");
m_dbConn.Open();
m_sqlCmd.Connection = m_dbConn;
m_sqlCmd.CommandText = "SELECT name FROM communication_remedies";
DataTable dt = new DataTable();
SQLiteDataAdapter adapter = new SQLiteDataAdapter(m_sqlCmd.CommandText,
m_dbConn);
adapter.Fill(dt);
Labels =
dt.AsEnumerable().Select(r => r.Field<string>("name")).ToArray();
}
catch (SQLiteException ex)
{
MessageBox.Show("Error count*: " ex.Message);
}
m_dbConn.Close();
}
}
我的某種形式的代碼:
private void Organizations_Load(object sender, EventArgs e)
{
CommunicationCanalsDrawing comm = new CommunicationCanalsDrawing();
comm.ListOfColumns();
comm.Lx = 12;
comm.Tx = 243;
comm.Ty = 173;
comm.Ly = 207;
comm.Height = 26;
comm.Width = 228;
for (int i = 0; i < comm.Labels.Length; i )
{
comm.i = i;
comm.DrawThat();
this.Controls.Add(comm.txt);
this.Controls.Add(comm.l);
}
}
我知道控制元件的坐標可能不正確,但現在沒關系。很容易糾正。我只看到最后一個文本框和最后一個標簽。其他不顯示。
uj5u.com熱心網友回復:
您的問題是您只創建一次 CommunicationCanalsDrawing 實體,然后對其屬性執行 for 回圈。這樣你就只剩下最后一個了。您需要通過 for 回圈在每個回圈上創建一個新的 CommunicationCanalsDrawing 實體:
private void Organizations_Load(object sender, EventArgs e)
{
for (int i = 0; i < comm.Labels.Length; i )
{
CommunicationCanalsDrawing comm = new CommunicationCanalsDrawing();
comm.ListOfColumns();
comm.Lx = 12;
comm.Tx = 243;
comm.Ty = 173;
comm.Ly = 207;
comm.Height = 26;
comm.Width = 228;
comm.i = i;
comm.DrawThat();
this.Controls.Add(comm.txt);
this.Controls.Add(comm.l);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418000.html
標籤:
上一篇:在時間塊內獲取不同的值
