开发者

Why is this C# load form not working? [closed]

This question is unlikely to help any future vis开发者_运维百科itors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I am trying to get a value from a config file when the form loads it puts it in a Textbox. I am using Program.cs to get the value from the config file. Everything looks setup correctly but when I run the form no value is there.

public form()
{   
    InitializeComponent();

    string NewValue = Program.Value;
    Textbox.Text = NewValue;
}

Program.cs:

public static string Value = "";         

switch (element.ChildNodes[i].Name)
{
    case "FileInfo":
    {
        for (int j = 0; j < childNodeList.Count; j++)
        {
            switch (childNodeList[j].Name)
            {
                case "Value":
                {
                    Program.Value = childNodeList[j].InnerText;
                    break;
                }
            }
        }

         .........

    }

     ---------

}  

Config:

<config>
  <FileInfo>
  <Value>1234</Value>
  </FileInfo>
</config>


I'm assuming that you are using at least .net 2.0

  • Add reference to System.Configuration to your project
  • Make sure that your app.config looks properly:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="myKey" value="myValue"/>
        </appSettings>
    </configuration>    
    
  • Change you code to use ConfigurationManager to retrieve the value:

    using System.Configuration;
    using System.Windows.Forms;
    
    namespace SO6065319
    {
            public partial class Form1 : Form
            {
                    public Form1()
                    {
                            InitializeComponent();
                            textBox1.Text = ConfigurationManager.AppSettings["myKey"];
                    }
            }
    }
    

Here textBox1 is the name of the text box on the form. You can see now "myValue" in your textbox.


Read about ConfigurationManager.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜