0

Hopefully, someone can help with this problem. I have a login form and I want to the let the user know that the application is processing his/her request once the submit button clicked. The problem is that I am setting the labe1.TEXT = ""; and the pictureBox2.Visible = false; when the form starts but I cannot get the program to set it back to label.Text = "processing"; and pictureBox2.Visible = true; when the user clicks the submit button.

private void btnLogin_Click_1(object sender, EventArgs e)
{
    AppDomain.CurrentDomain.SetData("DataDirectory", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\store_close_localdb.mdf;Integrated Security=True;Connect Timeout=30");
    SqlDataAdapter sda = new SqlDataAdapter("Select Role from tblLogin where Username='" + tbUsername.Text + "' and Password='" + tbPassword.Text + "' ", con);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    if (dt.Rows.Count == 1)
    {
        label2.Text ="Processing your login request, please wait...";
        pictureBox2.Visible = true;
        Application.DoEvents();
        pleasewait pw = new pleasewait();
        pw.Show();
    }
    else
    {
        label1.Text = "INCORRECT USERNAME OR PASSWORD :(";
    }
}
Matze
  • 5,100
  • 6
  • 46
  • 69
Tony Batista
  • 177
  • 2
  • 13
  • what is the user supposed to wait for? – TaW May 14 '17 at 12:00
  • Because your are hiding window before it can update GUI. Comment `this.Hide();' and see what will happen. – apocalypse May 14 '17 at 12:02
  • @TaW The user should wait for the "pw.Show();" to show up. – Tony Batista May 14 '17 at 12:03
  • @apocalypse I commented the line as you indicated but the result is the same. The picturebox and the label are showed after the second form is loaded. – Tony Batista May 14 '17 at 12:07
  • and then..?? (Hint: If you want to access the main fomr from the pw you can pass it out: pleasewait pw = new pleasewait(this); - Then it can grab it and upon clsing show it again.. Also note that Show doesn't wait for anthing. ShowDialog does, though. – TaW May 14 '17 at 12:08
  • @TaW I want the label to loaded before the pw.show(); is completed. That way the user will know that the first form is doing something. – Tony Batista May 14 '17 at 12:11
  • Why do you even use label2.Invoke? All forms run on the UI thread anyway.. the realtion of froms and other thing is not explained well imo. – TaW May 14 '17 at 12:20
  • Well, with or without the invoke I still get the same result; I was trying to find a solution to my problem. – Tony Batista May 14 '17 at 12:27
  • You should read about async/await features in WinForms. But for now just write `Application.DoEvents()` after `pictureBox2.Visible = true;` and it should work. Also read this: http://stackoverflow.com/questions/5181777/use-of-application-doevents – apocalypse May 14 '17 at 12:52
  • @apocalypse I used that before but the problem is that the pictureBox gif image will not animate; any idea on how to resolve this? – Tony Batista May 14 '17 at 13:06
  • Yes, use a background worker...and avoid DoEvents() like a disease... – Adriano Repetti May 14 '17 at 13:11
  • @AdrianoRepetti DoEvents(); "like a disease," why is that? – Tony Batista May 14 '17 at 13:21
  • Because winforms code is (usually) not re-entrant. It's an infinite source of bugs (user may do anything and just actions will be performed in "parallel"). A background worker is easier to use and keep UI responsive (or as alternative an asynchronous action) – Adriano Repetti May 14 '17 at 13:31
  • Okay, I understand. I never used the background worker before can you give and example? – Tony Batista May 14 '17 at 13:46
  • http://stackoverflow.com/questions/6481304/how-to-use-a-backgroundworker – apocalypse May 14 '17 at 19:11

0 Answers0