-2

I want to change with a timer helps the color of buttons contains in a flowLayoutPanel1. With my query code the color change right, but when the condition of query turn to false, my button don't change his color (when I create it I set the color to green).

This is my code:

private void timer1_Tick(object sender, EventArgs e)
{
    int t = 0;
    int st = 0;
    try
    {
        using (SqlConnection cn =new SqlConnection(VisualizzaOrdini.Form1.cnstr))
        {
            string strSql = "SELECT tavolo,stampa FROM Ordini";
            SqlCommand cmd = new SqlCommand(strSql, cn);
            cn.Open();
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    t = (int)dr["tavolo"];
                    st = (int)dr["stampa"];
                    foreach (Control c in flowLayoutPanel1.Controls)
                    {
                        if (st == 0)
                        {
                            if (c.Name == Convert.ToString(t))
                            {
                                c.BackColor = Color.Red;
                            }
                        }
                        else if (c.Name == Convert.ToString(t))
                        {
                            c.BackColor = Color.Green;
                        }
                    }
                }
            }
            cn.Close();
        }
    }
}
catch (Exception)
{
}

I need to implement this in a restaurant program. I want to color table red if it's busy and green if it's free.

This is an image:

https://docs.google.com/file/d/0B-nMmszaCz8EVE9JdjlISHg1aDA/edit?usp=sharing

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Mikykly
  • 15
  • 2
  • 4
  • 2
    And what is the question? – MoonKnight Apr 24 '13 at 09:14
  • I've multiple buttons with backcolor green, with the query I want to turn them red(that's work!), but my query don't work well because if the numeber of table change from busy to free, the button don't change its color from red to green! – Mikykly Apr 24 '13 at 09:17
  • You know when the table state is changing as you will have to update the database yourself. So, when you make an update you update the colour displayed. So I don't see the problem. How is the database state getting updated? – MoonKnight Apr 24 '13 at 09:17
  • In what scenario is the backcolour changed to Yellow? – Lojko Apr 24 '13 at 09:19
  • There is a problem in the foreach I think... – Mikykly Apr 24 '13 at 09:19
  • Sorry, I edit text...not yellow but Green! – Mikykly Apr 24 '13 at 09:20
  • Forget the colour, tell us some useful information. You want the colour to change when the database is updated right? Then when do you update the table state in the database? Also, what is the definition of your table `Ordini`; can you show it? – MoonKnight Apr 24 '13 at 09:22
  • your if conditions are not right what i think...try to debug the program see what is the value in st... – Shafqat Masood Apr 24 '13 at 09:27
  • With foreach I want to iterate through buttuns contains in a flowLayoutPanel1, with Sql query I want to say if number of table is in my flowLayoutPanel1 with st(stampa-->print)==0 so it's busy(red) else it's free(green) – Mikykly Apr 24 '13 at 09:30

2 Answers2

0
foreach (Button button in flowLayoutPanel1.Controls)
{    
    if (button.Name == Convert.ToString(t))
    {
        if (st == 0)
        {
           button.BackColor = Color.Red;
        }                    
        else
        {
           button.BackColor = Color.Green;
        }
    }
}

Additionally, alter the foreach loop to look for the specific button control (or whatever custom control you may be using) as it currently will look through all the controls regardless whether they are a button or not

Lojko
  • 173
  • 1
  • 13
  • No, I've the same problem! – Mikykly Apr 24 '13 at 09:53
  • If you are not updating the database elsewhere (which I assumed you were) st will always be coming back as 0. So your "stampa" column in the database must be updated when you feel it becomes available again. – Lojko Apr 24 '13 at 09:57
0

if your buttons in Panel inside flowLayoutPanel1 code will not work

foreach (Control c in flowLayoutPanel1.Controls)
                    {
                        if (c is Panel)
                        {
                           //iterate through panel
                        }
                    }
user1659922
  • 346
  • 4
  • 15