Write to a label when it loads with the form

Asked

Viewed 758 times

2

Well, I have a program that needs me to write using Abels, however these only work, and actually write when they are clicked, I would like someone to explain to me how I should proceed to be able to write as soon as the form is loaded:

EDIT: Basically as I said I’m trying to write data on a label that I took from a BD however this is only written when I click on the Abels, I would like it to be written as soon as the form and about is loaded, it is the code

SqlConnection conn = new SqlConnection(@"Data Source=.\wintouch;Initial        Catalog=bbl;User ID=sa;Password=Pa$$w0rd");
    conn.Open();
    string info = ""; // open connection and creat the string that contains info



        //-----------------------------------------------------------//

        Timer timer1 = new Timer();

        timer1.Tick += new EventHandler(label4_load);
        timer1.Interval = 55;
        timer1.Enabled = true;


        string varsql = "SELECT sum(merc1)/2 as total FROM wgcdoccab where tipodoc ='FSS' and serie='1' and contribuinte='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())),120)"; //division query 


        SqlCommand cmd = new SqlCommand(varsql, conn);

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {

            info = dr["total"].ToString().TrimEnd() + "," + "\r"; // search the database?

        }

        label4.Text = info; //display information on hte label
        label4.Text = string.Format("new_text {0}", DateTime.Now.ToLongTimeString()); 
        conn.Close();

        timer1.Stop();
    }`
  • 1

    Try to give more details about your question, post a code and/or image that can help in solving the problem.

1 answer

1


When you create a Form, the system creates a Form Builder:

.....
public Form1()
{
   InitializeComponent();
   label1.Text = "Escreva o que tu quiseres aqui";// Faça isso em baixo do InitializeComponent();
}

Just that and nothing else.

  • Yes, I later noticed that it was a silly thing, I was messing with the code so long in a row that I didn’t notice that I was writing in Labelx_load instead of Fromx_load, my mistake but thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.