文本文件和列表框 [英] text file and listbox

查看:68
本文介绍了文本文件和列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除我的问题外,其他所有东西都在起作用;的代码是什么:
*书签文本文件将被加载到列表框中.如果文本文件不存在或发生异常,则将使用消息框显示错误消息.
*关闭表单后,列表框的当前内容将存储在书签文本文件(称为bookmarks.txt)中.如果发生文件错误,请显示指定错误的消息框.

谢谢...

Everything is working except for my questions; what''s the code for:
*A text file of bookmarks will be loaded into the listbox. If the text file does not exist, or an exception occurred, a messagebox will be used to display an error message.
*When the form is closed, the current contents of the listbox will be stored in the bookmarks textfile (called bookmarks.txt). If a file error occurs, display a messagebox specifying the error.

Thank you...

    private void btn_go_Click(object sender, EventArgs e)
    {
        wBrowser.Navigate(tbx_website.Text);
    }

    private void btn_bookmark_Click(object sender, EventArgs e)
    {
        //Add the url to the listbox
        LBlist.Items.Add(tbx_website.Text);

        if (LBlist.Items.Count == 0)
        {
            btn_delete.Enabled = false;
        }
    }

    private void btn_delete_Click(object sender, EventArgs e)
    {
        // The Delete button was clicked
        int i_delete = LBlist.SelectedIndex;

        try
        {
            // Remove the item in the List.
            LBlist.Items.RemoveAt(i_delete);

        }
        catch
        {
        }
    }

    private void listbox_DoubleClick(object sender, EventArgs e)
    {
        wBrowser.Navigate(LBlist.SelectedItem.ToString());
    }

    private void wBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        tbx_website.Text = wBrowser.Url.ToString();
    }

    private void LBlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        btn_delete.Enabled = (LBlist.Items.Count > 0);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        btn_delete.Enabled = false;
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        StreamWriter sw_file;
        string s_bmfile = LBlist.Items.ToString();
        sw_file = new StreamWriter("bookmarks.txt");
        sw_file.Write(s_bmfile);
        sw_file.Close();
    }
}

推荐答案

private void Form1_Load(object sender, EventArgs e)
{
    LBlist.Items.Clear();

    // test to exists 'bookmarks.txt' in the current path
    if (File.Exists("bookmarks.txt"))
    {
        StreamReader sr = new StreamReader("bookmarks.txt");

        foreach (string item in sr.ReadToEnd().Split(new char[] { ';' }))
            LBlist.Items.Add(item);

        sr.Close();
    }
    else
    {
        MessageBox.Show("No found the file!");
    }
    btn_delete.Enabled = false;
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    StreamWriter sw_file;
    string s_bmfile = "";

    foreach (object item in LBlist.Items)
        s_bmfile += item.ToString() + ";";

    try
    {
        sw_file = new StreamWriter("bookmarks.txt");
        sw_file.Write(s_bmfile);
        sw_file.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


*创建一个新的Windows应用项目&添加新表格

*添加控件textBox1,comboBox1,btnAdd(按钮),brnBrowse,btnRemove,btnRemove

*添加
* Create a new windows applicaiton project & add a new form

* Add controls textBox1,comboBox1,btnAdd (button),brnBrowse,btnRemove,btnRemove

* Add
using System.IO;



*尝试以下代码,您可以在文本框中输入字符串,然后单击btnAdd,然后查看数据在组合框中,可以关闭应用程序并将其打开,数据将仍然存在-希望对您有所帮助



*try the following code, you can type in a string in the text box and click btnAdd and see the data is in the combo box, you can close the applicaion and open it the data will be still there -- hope this helps

public partial class Form1 : Form
   {

       public readonly string xsdPath = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "url.xsd");
       public readonly string xmlPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "url.xml");
       private DataSet ds;

       public Form1()
       {
           InitializeComponent();
       }

       private void LoadDataset(){

           try
           {
               if (File.Exists(xsdPath))
               {
                   ds = new DataSet();
                   ds.ReadXmlSchema(xsdPath);
               }
               else
                   CreateSchema();


               if (File.Exists(xmlPath))
                   ds.ReadXml(xmlPath, XmlReadMode.IgnoreSchema);

           }
           catch (Exception ex) { throw ex; }
       }

       private void CreateSchema()
       {
           try{

               ds = new DataSet("dUrl");
               var tUrl = ds.Tables.Add();
               tUrl.Columns.Add("Url",typeof(string));

           }
           catch (Exception ex) { throw ex; }
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           try
           {
               LoadDataset();
               RefreshData();
           }
           catch (Exception ex) { MessageBox.Show(ex.Message); }
       }

       private void RefreshData()
       {
           try
           {
               comboBox1.DataSource = ds.Tables[0];
               comboBox1.DisplayMember = "Url";
           }
           catch (Exception ex) { throw ex; }
       }

       private void Form1_FormClosing(object sender, FormClosingEventArgs e)
       {
           ds.WriteXml(xmlPath, XmlWriteMode.DiffGram);
       }

       private void btnAdd_Click_1(object sender, EventArgs e)
       {
           try
           {
               if (textBox1.Text.Trim().Length != 0)
               {
                   ds.Tables[0].Rows.Add(textBox1.Text);
               }
               RefreshData();
               textBox1.Text = "";
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void btnRemove_Click(object sender, EventArgs e)
       {
           ds.Tables[0].Rows[comboBox1.SelectedIndex].Delete();
       }
   }


这篇关于文本文件和列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆