如何从应用程序配置中读取文本框的值

How to read the value to text box from app config

我在 vb.net

的 appconfig 中添加了键值
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key0" value="0" />
<add key="Key1" value="1" />
<add key="Key2" value="2" />
</appSettings>
</configuration>

我想遍历应用程序配置中的所有键值并将值读取到 textbox1、textbox2 和 textbox3。

我已经做了一些工作但未能实现下面是我尝试过的。

If form1.combobox1.selecteditem = 0 then
Dim appsettings = configurationmanager.appsettings
dim result as string
for each result in appsetting
if result = appsettings("Key1") then
textbox1.text = result
else
if result = appsettings("key2") then
textbox2.text = result
end if
next

以上在if条件下抛出错误,能否请你帮我找到解决方案,从VB.net平台的Appconfig文件中读取值到文本框。

我不确定你说的 If form1.combobox1.selecteditem = 0 then 是什么意思 如果框中有 0,则 If ComboBox1.SelectedText = "0" Then 应该有效。组合框中的项目是对象。 如果您的意思是没有选择的项目,那么 If ComboBox1.SelectedIndex = -1 你错过了 End If so 我把它添加到我认为它应该属于的地方

If ComboBox1.SelectedText = "0" Then
            Dim appsettings = ConfigurationManager.AppSettings
            Dim result As String
            For Each result In appsettings
                If result = appsettings("Key1") Then
                    TextBox1.Text = result
                ElseIf result = appsettings("key2") Then
                    TextBox2.Text = result
                End If
            Next
End If

如果你能下载Visual Studio Community 2017(免费)并开启Option Strict,对你写代码会有很大的帮助。 代码未针对 app.config.

进行测试