C# 根据数据库信息动态更改按钮图像

C# 根据数据库信息动态更改按钮图像,c#,winforms,button,sql-server-ce,C#,Winforms,Button,Sql Server Ce,我正试图根据数据库中的状态更改几个按钮图像。 如果我得到任何状态“0”,我应该将按钮图像更改为“忙”图像,因为默认图像是“空闲”图像 所以我的问题是:如何通过变量名更改按钮? 我现在有这个代码(我知道它错了): 简单到: while (readSuite.Read()) { suiteIndex = Convert.ToInt32(readSuite["SuiteNum"]); switch(suiteIndex) { case 0: {

我正试图根据数据库中的状态更改几个按钮图像。 如果我得到任何状态“0”,我应该将按钮图像更改为“忙”图像,因为默认图像是“空闲”图像

所以我的问题是:如何通过变量名更改按钮?

我现在有这个代码(我知道它错了):

简单到:

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;
       }
    }
}
编辑:

我使用开关的原因是为了防止将来出现其他状态。您有“忙”和“闲”,但也可能有“保留”,您可能需要进一步的条件,这些条件只会在一个简单的
if-else-if
序列中变得模糊。

简单如下:

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;
       }
    }
}
编辑:


我使用开关的原因是为了防止将来出现其他状态。您有“忙”和“闲”,但也可能有“保留”,您可能需要进一步的条件,这些条件只会在简单的
if-else-if
序列中变得模糊。

我相信您需要使用
this.Controls.Find(suitePath,true)
将字符串转换为控件。我假定
“suite”+suiteIndex
是您每个按钮的
名称

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


或者,为了更快地访问,您可能希望保留一个带有每个按钮的
字典。

我认为您需要使用
this.Controls.Find(suitePath,true)
将字符串转换为控件。我假定
“suite”+suiteIndex
是您每个按钮的
名称

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


或者,为了更快地访问,您可能希望保留一本带有每个按钮的
词典。

我做到了!把字典当作胸腺嘧啶说:

                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;
                }
            }

            }


    }
public void checkSuites()
{
字典btnList=新字典();
btnList.Add(1,suite1);
btnList.Add(2,suite2);
btnList.Add(3,suite3);
btnList.Add(4,套件4);
btnList.Add(5,suite5);
SqlCeCommand checkSuite=新SqlCeCommand(“从RentSessionLog中选择*,其中State='0',mainConnection);
SqlCeDataReader readSuite=checkSuite.ExecuteReader();
while(readSuite.Read())
{
int suiteIndex=Convert.ToInt32(readSuite[“SuiteNum”]);
字符串suitePath=“suite”+suiteIndex;
foreach(btnList.Values中的按钮键)
{
if(key.Name==suitePath)
{
key.Image=NewSoftware.Properties.Resources.busySuiteImg;
}
}
}
}

谢谢所有帮助过我的人:我做到了!把字典当作胸腺嘧啶说:

                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;
                }
            }

            }


    }
public void checkSuites()
{
字典btnList=新字典();
btnList.Add(1,suite1);
btnList.Add(2,suite2);
btnList.Add(3,suite3);
btnList.Add(4,套件4);
btnList.Add(5,suite5);
SqlCeCommand checkSuite=新SqlCeCommand(“从RentSessionLog中选择*,其中State='0',mainConnection);
SqlCeDataReader readSuite=checkSuite.ExecuteReader();
while(readSuite.Read())
{
int suiteIndex=Convert.ToInt32(readSuite[“SuiteNum”]);
字符串suitePath=“suite”+suiteIndex;
foreach(btnList.Values中的按钮键)
{
if(key.Name==suitePath)
{
key.Image=NewSoftware.Properties.Resources.busySuiteImg;
}
}
}
}

感谢所有帮助您的人:D

您的代码中到底有什么“错误”?您遇到了什么错误?我收到了这个错误:'string'不包含'Image'的定义,并且找不到接受'string'类型的第一个参数的扩展方法'Image'(您是否缺少using指令或程序集引用?)。代码中的“错误”到底是什么?您遇到了什么错误?我收到了此错误:'string'不包含'Image'的定义,并且找不到接受'string'类型的第一个参数的扩展方法'Image'(是否缺少using指令或程序集引用?)仍然存在相同的问题:“string”不包含“Image”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“Image”(您是否缺少using指令或程序集引用?)啊,我看到了您的问题。您需要在按钮上设置
图像
属性,而不是字符串本身。是的,使用“开关”是一个非常好的建议但它不接受变量作为按钮的名称。按钮定义在哪里?仍然存在相同的问题:“string”不包含“Image”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“Image”(是否缺少using指令或程序集引用?)啊,我看到了您的问题。您需要在按钮上设置
Image
属性,而不是字符串本身。是的,使用“switch”是一个很好的建议,但它不接受变量作为按钮的名称。按钮定义在哪里?我得到这个错误:方法“Find”没有重载需要1个参数hoops,您需要
Find(suitePath,true)
布尔值告诉它是否应该搜索子项。现在我收到了这个错误:无法将类型“System.Windows.Forms.Control[]”隐式转换为“System.Windows.Forms.Button”:(啊,这很好,应该可以提供更好的性能(即使它可能永远不会被注意到)。对于
Find
,它首先返回一个数组,因此您需要选择第一个项目(您可能希望确保它返回项目),然后将其强制转换到
按钮,或者
(按钮)这个。Find(…)
还有更多(不太可能)与此相反的错误案例,与字典方法相反……我得到这个错误:没有过载