none
Form with many buttons, Rendering Issue RRS feed

  • Question

  • I have a form which contains 100 buttons. Each button need to change its color based on the database data. I've tried update them using loop, also put the method into a thread. But it takes too long to update the form. Any help?
    Friday, January 5, 2018 10:09 PM

Answers

  • Try some possible optimisations:

    DateTime time = DateTime.ParseExact(sellingDateComboBox.SelectedValue.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture).Date;

    for (int i = 0; i < numberBtns.Count; i++)

    {

       NumberSellings result = db.NumberSellings.FirstOrDefault(e => e.LoteryId == loteryType && e.Date == time && e.Number == i);

       if (result != null && result.Monto != 0)

       {

          numberBtns[i].BackColor = Color.LightGreen;

       }

       else

       {

          numberBtns[i].BackColor = Color.White;

       }

    }

    (Use a collection that allows ‘[i]’).

    Fore more benefits, consider executing a single optimised query that returns all of required data (avoiding 100 queries).

    • Edited by Viorel_MVP Saturday, January 6, 2018 5:55 PM
    • Marked as answer by Alex Feng Li Saturday, January 6, 2018 10:58 PM
    Saturday, January 6, 2018 5:53 PM

All replies

  • Hi,

    I'm afraid you need to provide your code.

    Regards,

    Ashidacchi

    Saturday, January 6, 2018 4:26 AM
  • As stated already, you need to supply code used. In the mean time, try the following not using a thread. SuspendLayout (use it by itself does the entire form), do the work, ResumeLayOut. SuspendLayOut stops the form updates while ResumeLayout allows form updates to be seen again.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 6, 2018 10:31 AM
    Moderator
  • Here is my code  private void changeBtnsColor()
            {
                using (var db = new DataBaseConnection())
                {
                    if (numberBtns == null)
                    {
                        initBtns();

                    }

                    for (int i = 0; i < numberBtns.Count; i++)
                    {
                        DateTime time = DateTime.ParseExact(sellingDateComboBox.SelectedValue.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture).Date;
                        List<NumberSellings> result = db.NumberSellings.Where(e => e.LoteryId == loteryType && e.Date == time && e.Number == i).ToList();
                        if (result.Count != 0)
                        {
                            if (result.First().Monto != 0)
                            {
                                numberBtns.ElementAt(i).BackColor = Color.LightGreen;

                            }

                            else

                            {
                                numberBtns.ElementAt(i).BackColor = Color.White;
                            }
                        }
                        else
                        {
                            numberBtns.ElementAt(i).BackColor = Color.White;
                        }
                    }
                }
                    
            }
    Saturday, January 6, 2018 3:52 PM
  • Try some possible optimisations:

    DateTime time = DateTime.ParseExact(sellingDateComboBox.SelectedValue.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture).Date;

    for (int i = 0; i < numberBtns.Count; i++)

    {

       NumberSellings result = db.NumberSellings.FirstOrDefault(e => e.LoteryId == loteryType && e.Date == time && e.Number == i);

       if (result != null && result.Monto != 0)

       {

          numberBtns[i].BackColor = Color.LightGreen;

       }

       else

       {

          numberBtns[i].BackColor = Color.White;

       }

    }

    (Use a collection that allows ‘[i]’).

    Fore more benefits, consider executing a single optimised query that returns all of required data (avoiding 100 queries).

    • Edited by Viorel_MVP Saturday, January 6, 2018 5:55 PM
    • Marked as answer by Alex Feng Li Saturday, January 6, 2018 10:58 PM
    Saturday, January 6, 2018 5:53 PM
  • thanks for the comment
    Saturday, January 6, 2018 10:55 PM