首页  编辑  

TextBox自动完成载入和保存

Tags: /C#/界面处理/TextBox_RichEdit/   Date Created:

private string CompleteCustomFile;

private void Form1_Load( object sender , EventArgs e)

{

   CompleteCustomFile = Path . ChangeExtension(

        Application . ExecutablePath, ".txt" );

    if ( File . Exists(CompleteCustomFile))

   {

        StreamReader vStreamReader = new StreamReader (

           CompleteCustomFile, Encoding . Default);

       textBox1 . AutoCompleteCustomSource . Clear();

        string vLine;

        while ((vLine = vStreamReader . ReadLine()) != null )

           textBox1 . AutoCompleteCustomSource . Add(vLine);

       vStreamReader . Close();

   }

}

private void button1_Click( object sender , EventArgs e)

{

    if ( ! textBox1 . AutoCompleteCustomSource . Contains(textBox1 . Text))

       textBox1 . AutoCompleteCustomSource . Insert( 0 , textBox1 . Text);

}

private void Form1_FormClosed( object sender , FormClosedEventArgs e)

{

    StreamWriter vStreamWriter = new StreamWriter (

       CompleteCustomFile, false , Encoding . Default);

    int vCount = 0 ;

    foreach ( string vLine in textBox1 . AutoCompleteCustomSource)

   {

       vStreamWriter . WriteLine(vLine);

       vCount ++ ;

        if (vCount >= 20 ) break ;

   }

   vStreamWriter . Close();

}