0

I'm trying to change several button images according to what state I get in the database. If I get any state "0" I should change the button image to a "busy" image, since the default image is a "free" one.

So my question is: How can i change a button through a variable name?

I have this code for now(i know its wrong):

private void checkSuites()
{
    SqlCeCommand checkSuite = new SqlCeCommand("SELECT * FROM RentSessionLog WHERE State='0'", mainConnection);

    SqlCeDataReader readSuite = checkSuite.ExecuteReader();
    int suiteIndex;
    string suitePath = "suite" + suiteIndex;

    while (readSuite.Read())
    {
        suiteIndex = Convert.ToInt32(readSuite["SuiteNum"]);
        suitePath.Image = NewSoftware.Properties.Resources.busySuiteImg;
    }
}
CAbbott
  • 8,078
  • 4
  • 31
  • 38
Renato Parreira
  • 838
  • 1
  • 7
  • 23
  • what exactly is "wrong" in your code? What error are you getting? – trebuchet Dec 05 '12 at 20:55
  • I receive this error:'string' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) – Renato Parreira Dec 05 '12 at 21:05

3 Answers3

2

It's as simple as:

while (readSuite.Read())
{
    suiteIndex = Convert.ToInt32(readSuite["SuiteNum"]);
    switch(suiteIndex)
    {
    case 0:
       {
           suitePath.Image = NewSoftware.Properties.Resources.busySuiteImg;
           break;
       }
    default:
       {
           suitePath.Image = NewSoftware.Properties.Resources.freeSuiteImg;
       }
    }
}

Edit:

The reason I used a switch is in case you have other-states appearing in the future. You have "Busy" and "Free", but there could be "Reserved" as well and you may want to have further conditions that would just get obfuscated in a simple if else if sequence.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • Still having the same problem: 'string' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) – Renato Parreira Dec 05 '12 at 21:09
  • Ah, I see your issue. You need to be setting the `Image` property on your button, not the string itself. – Moo-Juice Dec 05 '12 at 21:12
  • Yes, it's a very good suggestion using "switch" but it doesn't accept the variable as the name of the button. – Renato Parreira Dec 05 '12 at 21:12
  • Where are the buttons defined? – Moo-Juice Dec 05 '12 at 21:13
1

I believe you need to use this.Controls.Find(suitePath, true) to turn your string into a control. I am presuming that "suite" + suiteIndex is the .Name of each of your buttons.

string suitePath = "suite" + suiteIndex;
Button suiteButton = this.Controls.Find(suitePath, true);
suiteButton.Image = ...

See more details about Controls.Find

Alternatively for quicker access, you may want to keep a Dictionary<int, Control> with each of your buttons in it.

Community
  • 1
  • 1
Thymine
  • 8,775
  • 2
  • 35
  • 47
  • i get this error: No overload for method 'Find' takes 1 arguments – Renato Parreira Dec 05 '12 at 23:15
  • Whoops, you need `Find(suitePath, true)` The boolean tells it if it should search children or not. – Thymine Dec 05 '12 at 23:17
  • Now I'm receiving this error: Cannot implicitly convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.Button' :( – Renato Parreira Dec 05 '12 at 23:31
  • Ah, good that worked, that should offer better performance (even tho it would probably never be noticeable). For `Find`, first it returns an array, so you would need to select the first item (you may want to make sure it returns items) then cast it to `Button`, either `(Button)this.Find(...)` There are far more (unlikely) error cases to consider with this, as opposed to the dictionary method... – Thymine Dec 06 '12 at 04:57
0

I DID IT! Using a dictionary as Thymine said:

                public void checkSuites()
    {
        Dictionary<int, Control> btnList = new Dictionary<int, Control>();
        btnList.Add(1, suite1);
        btnList.Add(2, suite2);
        btnList.Add(3, suite3);
        btnList.Add(4, suite4);
        btnList.Add(5, suite5);
        SqlCeCommand checkSuite = new SqlCeCommand("SELECT * FROM RentSessionLog WHERE State='0'", mainConnection);

        SqlCeDataReader readSuite = checkSuite.ExecuteReader();
        while (readSuite.Read())
        {
            int suiteIndex = Convert.ToInt32(readSuite["SuiteNum"]);
            string suitePath = "suite" + suiteIndex;
            foreach (Button key in btnList.Values)
            {
                if (key.Name == suitePath)
                {
                 key.Image = NewSoftware.Properties.Resources.busySuiteImg;
                }
            }

            }


    }

Thank you all who helped :D

Renato Parreira
  • 838
  • 1
  • 7
  • 23