I have SQLite database wherein there is a table with some quantity of rows. I have a task to display the same quantity of textboxes in few forms with taking into account the coordinates of these controls. The names of these textboxes should be taken from one of the columns of this table. Also the labels must be present. Their names should be similar. Taking into account the fact the values of this column of this table is cyrillic I have decided to translate it into latinic symbols in the class Decryptor.
I've found the solution to perform the focus of displaying necessary textboxes in one form and I am able to copy this code into other forms. But it isn't good idea it seems despite the fact it would work in this case.
I've created new class - CommunicationCanalsDrawing. The idea of this class is reduction of code quantity and incresing of the usage convinience of this code. My code of particular class:
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();
}
}
My code of some form:
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);
}
}
I know the coordinates of the controls can be incorrect, but it does not matter now. It's easy to rectify. I see only the last textbox and the last label. Others aren't displayed.
CodePudding user response:
Your problem is you are creating only once instance of CommunicationCanalsDrawing and then doing your for loop on its properties. That way you end up with only the last. You need to create a new instance of CommunicationCanalsDrawing on each cycle through the for loop:
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);
}
}
