我有一个循环,当鼠标悬停在按钮上时,它会递增并显示递增数字。 每个按钮的值都存储在app.config
文件中。如何使用递增的值显示正确的值名称?
当前代码
public void Controls()
{
for (int i = 1; i <= Applications; i++)
{
string iconPath = ConfigurationManager.AppSettings["Application"+i+"_Icon"];
Button button = new Button();
this.Controls.Add(button);
top += button.Height+25;
button.Tag = i;
button.MouseHover += Button_MouseHover;
button.MouseLeave += Button_MouseLeave;
button.Visible = true;
button.Click += new EventHandler(button_Click);
}
app.config
:
<appSettings>
<add key="Applications" value="3" />
<add key="Catergory" value="1"/>
<add key="Application1_Name" value="word"/>
<add key="Application1_Executable_Name" value="word.exe"/>
<add key="Application1_AutoClose" value="true"/>
<add key="Application1_Icon" value="C:picturesimages.png"/>
<add key="Catergory" value="1"/>
<add key="Application2_Name" value="Paint"/>
<add key="Application2_Executable_Path" value="C:WindowsSystem32"/>
<add key="Application2_AutoClose" value="true"/>
<add key="Application2_Icon" value="C:picturesimages.png"/>
<add key="Catergory" value="1"/>
<add key="Application3_Name" value="notepad"/>
<add key="Application3_Executable_Path" value="C:Windows"/>
<add key="Application3_AutoClose" value="true"/>
<add key="Application3_Icon" value="C:picturesimages.png"/>
</appSettings>
目前,当我将鼠标悬停在一个按钮上时,它会显示值 1,2,3,但我希望它向我显示记事本、单词、油漆。
我尝试了什么
public void Controls()
{
for (int i = 1; i <= Applications; i++)
{
NameValueCollection sAll;
sAll = ConfigurationManager.AppSettings;
Button button = new Button();
this.Controls.Add(button);
top += button.Height+25;
button.Tag = i;
foreach(string s in sAll.AllKeys)
{
button.Text = s + i ;
}
}
}
它的作用是它向我显示 1,2,3 并且显示所有内容的油漆。
您的s
变量实际上是appSetting
键。您需要进一步传递此Key
才能将相应的appSetting
值获取到ConfigurationManager.AppSettings
。
NameValueCollection sAll;
sAll = ConfigurationManager.AppSettings;
foreach (string s in sAll.AllKeys)
{
var applicationNamevalue = sAll [s];//Just for demonstration, should combine these statements.
button.Text = applicationNamevalue + i; //and set it as button text.
}
是的,@Shivang的一个很好的建议,如果你真的不在乎键的值是什么,那么可以直接使用索引来获取配置值。
试试这个
string value = ConfigurationManager.AppSettings["Key"];
因此,对于您的情况,请使用 NameValueCollection 中的值,而不是键
button.Text = sAll[s] + i ;
在您的应用程序配置文件中,更好、更干净的方法是在配置部分中添加应用程序名称,如下所示
<configSections>
<section name="ApplicationList" type="System.Configuration.NameValueSectionHandler" />
<configSections>
然后添加连接到配置部分的每个应用程序名称
<ApplicationList>
<add key="1" value="Notepad"/>
<add key="2" value="Word"/>
<add key="3" value="Paint"/>
</ApplicationList>
然后可以这样使用
var applications = ConfigurationManager.GetSection("ApplicationList") as NameValueCollection;
for (int i = 0; i < applications.Count; i++)
{
var applicationName = applications[i].ToString() ;
}
还有 Controls 函数中的逻辑:如果 totalApplication 获取所有当前应用程序,那么为什么要遍历 sAll.AllKeys 中的每个名称?
循环不应该这样编码吗
public void Controls()
{
var applications = ConfigurationManager.GetSection("ApplicationList") as NameValueCollection;
for (int i = 0; i < applications.Count; i++)
{
var button = new Button();
this.Controls.Add(button);
top += button.Height+25;
button.Tag = i;
button.Text = applications[i].ToString();
}
}
由于您需要键的值,因此您应该在循环中执行类似以下内容的操作。
而且您不需要 foreach 循环。
只需按照以下方式使用即可,它将帮助您
当您使用 foreach 循环时,这根本不是必需的。
因此,请将其删除,因为它会增加时间复杂度。
如果您的 App.config 中有多个密钥,但您想获取其中的一些密钥,那么您应该为Application_Name指定格式,例如:
- application_name1
- application_name2
- application_name3
- application_name4
然后在代码中执行以下操作,然后只需将密钥添加到配置文件即可添加任意数量的按钮。
public void Controls()
{
for (int i = 1; i <= Applications; i++)
{
NameValueCollection sAll;
sAll = ConfigurationManager.AppSettings;
Button button = new Button();
this.Controls.Add(button);
top += button.Height+25;
button.Tag = i;
button.Text = sAll["application_name"+i] ;
}
}