根据窗体自动调整控件及文本框记住上次填写内容Demo

     第一次写文章,组词难免没有不通之处。。。

    最近常用到Winform根据窗体大小自动调整空间大小及字体、文本框记住上次填写内容待下次输入某一段时候自动跳出上次输入内容。于是就随便把两个问题放到同一个demo上。

    一、运行效果如下:

    1、 启动时:

     

   2、改变窗体大小时:

     

     

     3、输入文本时:

      

      

     二、代码:

       1、缩放代码:

           

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace myListSelect
{
    public partial class ListSelect : UserControl
    {
        TextBox tbx;

        public ListSelect()
        {
            InitializeComponent();

            this.tbxInputBox.TextChanged += new EventHandler(tbxInputBox_TextChanged);
            this.tbxInputBox.KeyUp += new KeyEventHandler(tbxInputBox_KeyUp);
            this.tbxInputBox.DoubleClick += new EventHandler(tbxInputBox_DoubleClick);
            this.tbxInputBox.KeyDown += new KeyEventHandler(tbxInputBox_KeyDown);
            this.lbxListSelect.MouseClick += new MouseEventHandler(lbxListSelect_MouseClick);
            this.lbxListSelect.MouseMove += new MouseEventHandler(lbxListSelect_MouseMove);

        }

        void lbxListSelect_MouseMove(object sender, MouseEventArgs e)
        {
            lbxListSelect.SelectedIndex = lbxListSelect.IndexFromPoint(new Point(e.X, e.Y));  
        }

        void lbxListSelect_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (tbx != null)
                {
                    tbxInputBox.Text = lbxListSelect.Text.ToString();
                    lbxListSelect.Visible = false;
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        void tbxInputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //记录本次所写内容
                if (!File.Exists("listSelectSetup.ini"))
                {
                    File.WriteAllText("listSelectSetup.ini", tbxInputBox.Text + "
");
                }
                else
                {
                    //读出所有行

                    string[] fileTextStr = File.ReadAllLines("listSelectSetup.ini");

                    if (!fileTextStr.Contains(tbxInputBox.Text))
                    {
                        File.AppendAllText("listSelectSetup.ini", tbxInputBox.Text + "
");
                    }
                }
            }
        }

        void tbxInputBox_DoubleClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxInputBox.Text))
            {
                DisplayOldInput(tbxInputBox.Text, tbxInputBox);
            }
        }

        void tbxInputBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (lbxListSelect.Items.Count < 0)
            {
                return;
            }
            if (e.KeyCode == Keys.Up)
            {
                int idx = lbxListSelect.SelectedIndex;
                if (idx == -1)
                {

                    lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - 1];
                }
                else
                {
                    if (idx == 0)
                    {
                        lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - 1];
                        idx = lbxListSelect.Items.Count;
                    }
                    lbxListSelect.SelectedItem = lbxListSelect.Items[idx - 1];
                }
            }
            else if (e.KeyCode == Keys.Down)
            {

                int idx = lbxListSelect.SelectedIndex;
                if (idx == -1)
                {

                    lbxListSelect.SelectedItem = lbxListSelect.Items[0];

                }
                else
                {
                    if (idx == lbxListSelect.Items.Count - 1)
                    {
                        lbxListSelect.SelectedItem = lbxListSelect.Items[0];
                    }
                    else
                    {
                        lbxListSelect.SelectedItem = lbxListSelect.Items[idx + 1];
                    }
                }
                idx = lbxListSelect.SelectedIndex;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    if (tbx != null)
                    {
                        tbxInputBox.Text = lbxListSelect.Text.ToString();
                        lbxListSelect.Visible = false;
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }
        }

        void tbxInputBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxInputBox.Text))
            {
                DisplayOldInput(tbxInputBox.Text, tbxInputBox);
            }
        }

        private void DisplayOldInput(string str, TextBox tb)
        {
            List<string> list = new List<string>();
            List<string> listSource = new List<string>();

            if (File.Exists("listSelectSetup.ini"))
            {
                list.AddRange(File.ReadAllLines("listSelectSetup.ini"));
                listSource=list.FindAll((line) => { return line.IndexOf(str) == 0; });
            }
            Point p = lbxListSelect.Location;
            p.X = tb.Location.X;
            lbxListSelect.Location = p;
            tbx = tb;
            lbxListSelect.DataSource = listSource;
            if (listSource != null && listSource.Count != 0)
            {
                lbxListSelect.Visible = true;
            }

        }
    }
}
View Code

     

       2、显示上次输入文本代码:

      

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace myListSelect
{
    public partial class ListSelect : UserControl
    {
        TextBox tbx;

        public ListSelect()
        {
            InitializeComponent();

            this.tbxInputBox.TextChanged += new EventHandler(tbxInputBox_TextChanged);
            this.tbxInputBox.KeyUp += new KeyEventHandler(tbxInputBox_KeyUp);
            this.tbxInputBox.DoubleClick += new EventHandler(tbxInputBox_DoubleClick);
            this.tbxInputBox.KeyDown += new KeyEventHandler(tbxInputBox_KeyDown);
            this.lbxListSelect.MouseClick += new MouseEventHandler(lbxListSelect_MouseClick);
            this.lbxListSelect.MouseMove += new MouseEventHandler(lbxListSelect_MouseMove);

        }

        void lbxListSelect_MouseMove(object sender, MouseEventArgs e)
        {
            lbxListSelect.SelectedIndex = lbxListSelect.IndexFromPoint(new Point(e.X, e.Y));  
        }

        void lbxListSelect_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (tbx != null)
                {
                    tbxInputBox.Text = lbxListSelect.Text.ToString();
                    lbxListSelect.Visible = false;
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        void tbxInputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //记录本次所写内容
                if (!File.Exists("listSelectSetup.ini"))
                {
                    File.WriteAllText("listSelectSetup.ini", tbxInputBox.Text + "
");
                }
                else
                {
                    //读出所有行

                    string[] fileTextStr = File.ReadAllLines("listSelectSetup.ini");

                    if (!fileTextStr.Contains(tbxInputBox.Text))
                    {
                        File.AppendAllText("listSelectSetup.ini", tbxInputBox.Text + "
");
                    }
                }
            }
        }

        void tbxInputBox_DoubleClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxInputBox.Text))
            {
                DisplayOldInput(tbxInputBox.Text, tbxInputBox);
            }
        }

        void tbxInputBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (lbxListSelect.Items.Count < 0)
            {
                return;
            }
            if (e.KeyCode == Keys.Up)
            {
                int idx = lbxListSelect.SelectedIndex;
                if (idx == -1)
                {

                    lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - 1];
                }
                else
                {
                    if (idx == 0)
                    {
                        lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - 1];
                        idx = lbxListSelect.Items.Count;
                    }
                    lbxListSelect.SelectedItem = lbxListSelect.Items[idx - 1];
                }
            }
            else if (e.KeyCode == Keys.Down)
            {

                int idx = lbxListSelect.SelectedIndex;
                if (idx == -1)
                {

                    lbxListSelect.SelectedItem = lbxListSelect.Items[0];

                }
                else
                {
                    if (idx == lbxListSelect.Items.Count - 1)
                    {
                        lbxListSelect.SelectedItem = lbxListSelect.Items[0];
                    }
                    else
                    {
                        lbxListSelect.SelectedItem = lbxListSelect.Items[idx + 1];
                    }
                }
                idx = lbxListSelect.SelectedIndex;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    if (tbx != null)
                    {
                        tbxInputBox.Text = lbxListSelect.Text.ToString();
                        lbxListSelect.Visible = false;
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }
        }

        void tbxInputBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxInputBox.Text))
            {
                DisplayOldInput(tbxInputBox.Text, tbxInputBox);
            }
        }

        private void DisplayOldInput(string str, TextBox tb)
        {
            List<string> list = new List<string>();
            List<string> listSource = new List<string>();

            if (File.Exists("listSelectSetup.ini"))
            {
                list.AddRange(File.ReadAllLines("listSelectSetup.ini"));
                listSource=list.FindAll((line) => { return line.IndexOf(str) == 0; });
            }
            Point p = lbxListSelect.Location;
            p.X = tb.Location.X;
            lbxListSelect.Location = p;
            tbx = tb;
            lbxListSelect.DataSource = listSource;
            if (listSource != null && listSource.Count != 0)
            {
                lbxListSelect.Visible = true;
            }

        }
    }
}
View Code

       三、源码下载:

         ConsoleApplication1.zip

原文地址:https://www.cnblogs.com/wangjiahong/p/3375967.html