Answered Display ini info in textbox

kwhelchel

Well-known member
Joined
Feb 28, 2020
Messages
51
Programming Experience
Beginner
so what I have is a layout that will take the info that is added into a textbox and then add it to an INI file. Everything there works fine but when the form is reopened I want it to read what is in the INI file and display it into the proper textboxs.
I have looked around and finding nothing but it could be the way I am looking too. Have tried the textbox.items.add(inif.Read("Database", "Devs")); with no luck.
my code is as follows
Thanks


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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,378
Location
Chesapeake, VA
Programming Experience
10+
A TextBox only has a Text property. It does not have an Items collection. So you just need to set the Text property.

Edit after: It looks like you already knew this based on your lines 18-28.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,917
Location
Sydney, Australia
Programming Experience
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.
 
Top Bottom