我有一个SQLite数据库,其中有一个带有一定行数的表。我的任务是在考虑这些控件的坐标的情况下,以几种形式显示相同数量的文本框。这些文本框的名称应该取自该表的某一列。此外,标签必须存在。他们的名字应该相似。考虑到这个表的这一列的值是cyrillic的,我决定在类Decryptor中将其转换为拉丁符号。
我已经找到了在一个表单中集中显示必要文本框的解决方案,并且我能够将此代码复制到其他表单中。但这似乎不是一个好主意,尽管事实上它在这种情况下会起作用。
我创建了一个新的类——沟通渠道制图。该类的思想是减少代码量,提高代码的使用方便性。我的特定类别代码:
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);
}
}
我知道控件的坐标可能不正确,但现在无关紧要了。这很容易纠正。我只看到最后一个文本框和最后一个标签。其他不显示。
您的问题是只创建一个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);
}
}