Any other ways to do it in C# ? I've been working for it since yesterday but i cant solve this problem. . I want this code to be executed in the background using another thread so that my main thread or UI thread will be responsive, I am creating a dynamic label using loop. . Thanks.
- private void Contact_Load(object sender, EventArgs e)
- {
- Thread loadContact = new Thread(new ThreadStart(loadContacts));
- loadContact.Start();
- }
- private void loadContacts()
- {
- Label l = null; ContactPanel.Controls.Clear();
- dt.Clear();
- dataConnection.Open();
- using (var messages = new SqlCommand("SELECT * FROM BasicInfo ", dataConnection))
- {
- da.SelectCommand = messages; da.Fill(dt);
- SqlDataReader rd = messages.ExecuteReader();
- string Fname, Lname,Fullname,Id;
- if (rd.HasRows)
- {
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- Point location = Point.Empty;
- DataRow dr = dt.Rows[i];
- Id = dr["_Id"].ToString();
- Fname = dr["_Fname"].ToString();
- Lname = dr["_Lname"].ToString();
- Fullname = Fname + Lname;
- l = addLabel(i);
- ContactPanel.Controls.Add(l);
- l.Text = Fname + " " + Lname; l.Name = Lname + i;
- l.Click += new EventHandler(this.labelClick);
- }
- }
- else { }
- Thread.Sleep(5000);
- }
- dataConnection.Close();
- }
- public void labelClick(object sender, EventArgs e)
- {
- Label currentLabel = (Label)sender;
- currentLabel.BackColor = Color.Gray;
- MessageBox.Show(currentLabel.Text);
- MainForm MainForm = new MainForm();
- MainForm.Show();
- this.Close();
- MainForm.TxtReciever.Text = currentLabel.Text;
- }
- public Label addLabel(int i)
- {
- Label l = new Label();
- l.Name = "Date" + i.ToString();
- l.BackColor = Color.SteelBlue;
- l.ForeColor = Color.White;
-
- l.Font = new Font("Segoe UI", 12, FontStyle.Regular);
- l.TextAlign = ContentAlignment.MiddleCenter;
- l.Margin = new Padding(5,3,3,3);
- l.Width = 255;
- l.Height = 53;
- return l;
- }