已回答  在文本框中显示ini信息

凯谢尔

活跃成员
已加入
2020年2月28日
留言内容
43
编程经验
Beginner
所以我所拥有的是一种布局,该布局将采用添加到文本框中的信息,然后将其添加到INI文件中。那里的所有东西都可以正常工作,但是当重新打开表单时,我希望它读取INI文件中的内容并将其显示在正确的文本框中。
我环顾四周,一无所获,但这可能也是我一直在寻找的方式。尝试过textbox.items.add(ini​​f.Read("Database", "Devs")); with no luck.
我的代码如下
谢谢


set ini:
    public partial class DashBoard : Form
    {

        public DashBoard()
        {
            InitializeComponent();
        }
       
        private void DashBoard_Load(object sender, EventArgs e)
        {

        }


        private void updatebtn_Click(object sender, EventArgs e)
        {

            string siten = sitetxtbox.Text;
            sitetxtbox.Text = "siten";

            string personto = emailtotxtbox.Text;
            sitetxtbox.Text = "personto";

            string ccto = cctotxtbox.Text;
            sitetxtbox.Text = "ccto";

            string bcto = bcctextbox.Text;
            sitetxtbox.Text = "bcto";

            INIFile inif = new INIFile(@"c:\test\mailsettings.ini");

            inif.Write("Properties", "site", $"{siten}");
            inif.Write("Properties", "personto", $"{personto}");
            inif.Write("Properties", "ccto", $"{ccto}");
            inif.Write("Properties", "bcto", $"{bcto}");

            MessageBox.Show("Settings Have Been Updated");

        }


    }
}
class INIFile
{
    private string filePath;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
    string key,
    string val,
    string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
    string key,
    string def,
    StringBuilder retVal,
    int size,
    string filePath);

    public INIFile(string filePath)
    {
        this.filePath = filePath;
    }

    public void Write(string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value.ToLower(), this.filePath);
    }

    public string Read(string section, string key)
    {
        StringBuilder SB = new StringBuilder(255);
        int i = GetPrivateProfileString(section, key, "", SB, 255, this.filePath);
        return SB.ToString();
    }

    public string FilePath
    {
        get { return this.filePath; }
        set { this.filePath = value; }
    }
)
 
Last edited:

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,533
地点
弗吉尼亚州切萨皮克
编程经验
10+
A TextBox only has a Text property. It does not have an Items collection. So you just need to set the Text property.

之后编辑:根据第18-28行,您似乎已经知道这一点。
 
Last edited:

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,524
地点
悉尼,澳大利亚
编程经验
10+
As suggested, the Text property is a single string that represents all the text in the control, so you can just get your data as a string and assign it. If you need to format the text somehow then you can use a StringBuilder and then assign the final result.

Alternatively, if you have multiple strings that represent the individual lines of text then you can create a string array and assign that to the Lines property. For instance, you might create a List<string>, Add the lines to it one by one, then call ToArray and assign the result to the Lines property.
 
最佳 底部