Changeset 2 for Common/FormFind.cs


Ignore:
Timestamp:
Feb 14, 2019, 5:29:40 PM (5 years ago)
Author:
chronos
Message:
  • Added: Registry extension methods.
  • Added: Search history in Find dialog.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/FormFind.cs

    r1 r2  
    88using System.Threading.Tasks;
    99using System.Windows.Forms;
     10using Microsoft.Win32;
    1011
    1112namespace Common
     
    1415    {
    1516        public RichTextBoxEx richTextBox;
    16         FormDimensions formDimensions;
     17        public string regSubKey;
     18        public int maxHistoryCount;
    1719
    1820        public FormFind()
    1921        {
    2022            InitializeComponent();
    21             formDimensions = new FormDimensions();
     23            regSubKey = "Find";
     24            maxHistoryCount = 20;
    2225        }
    2326
     
    5760        private void SearchText(bool reverse = false)
    5861        {
     62            if (comboBoxWhat.Text.Trim() != "")
     63            {
     64                string previousText = comboBoxWhat.Text;
     65                int index = comboBoxWhat.Items.IndexOf(comboBoxWhat.Text);
     66                if (index >= 0) comboBoxWhat.Items.RemoveAt(index);
     67                comboBoxWhat.Text = previousText;
     68                comboBoxWhat.Items.Insert(0, comboBoxWhat.Text);
     69               
     70                while (comboBoxWhat.Items.Count > maxHistoryCount)
     71                    comboBoxWhat.Items.RemoveAt(comboBoxWhat.Items.Count - 1);
     72            }
     73
    5974            while (true)
    6075            {
    61                 int newPos = FindText(textBoxWhat.Text.Trim(), richTextBox.SelectionStart, richTextBox.SelectionLength, reverse);
     76                int newPos = FindText(comboBoxWhat.Text.Trim(), richTextBox.SelectionStart, richTextBox.SelectionLength, reverse);
    6277                if (newPos >= 0)
    6378                {
    6479                    richTextBox.SelectionColor = Color.Red;
    65                     richTextBox.Select(newPos, textBoxWhat.Text.Length);
     80                    richTextBox.Select(newPos, comboBoxWhat.Text.Length);
    6681                    richTextBox.ScrollToCaret();
    6782                }
     
    94109            DpiScaling.Apply(this);
    95110            this.Icon = Icon.ExtractAssociatedIcon(Application.Execut‌​ablePath);
    96             formDimensions.Load(this, Owner);
    97             textBoxWhat.Focus();
     111            new FormDimensions().Load(this, Owner);
     112            comboBoxWhat.Focus();
     113            LoadFromRegistry();
    98114            UpdateInterface();
    99115        }
     
    106122        private void FormFind_FormClosing(object sender, FormClosingEventArgs e)
    107123        {
     124            SaveToRegistry();
    108125            richTextBox.formFind = null;
    109             formDimensions.Save(this, Owner);
     126            new FormDimensions().Save(this, Owner);
    110127        }
    111128
     
    115132            {
    116133                buttonFindNext.PerformClick();
     134            } else
     135            if (e.KeyChar == 27)
     136            {
     137                Close();
    117138            }
    118139        }
     
    120141        private void UpdateInterface()
    121142        {
    122             buttonFindNext.Enabled = textBoxWhat.Text.Length > 0;
    123             buttonFindPrevious.Enabled = textBoxWhat.Text.Length > 0;
     143            buttonFindNext.Enabled = comboBoxWhat.Text.Length > 0;
     144            buttonFindPrevious.Enabled = comboBoxWhat.Text.Length > 0;
     145        }
     146
     147        public void LoadFromRegistry()
     148        {
     149            RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
     150            if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
     151
     152            checkBoxMatchWholeWordOnly.Checked = regKey.GetValueBool("MatchWholeWordOnly", false);
     153            checkBoxMatchCase.Checked = regKey.GetValueBool("MatchCase", false);
     154
     155            regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey + "\\History");
     156            if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey + "\\History");
     157            int count = regKey.GetValueInt("Count", 0);
     158            comboBoxWhat.BeginUpdate();
     159            while (comboBoxWhat.Items.Count > count) comboBoxWhat.Items.RemoveAt(comboBoxWhat.Items.Count - 1);
     160            while (comboBoxWhat.Items.Count < count) comboBoxWhat.Items.Add("");
     161            for (int i = 0; i < count; i++)
     162            {
     163                comboBoxWhat.Items[i] = (string)regKey.GetValue(i.ToString(), "");
     164            }
     165            comboBoxWhat.EndUpdate();
     166        }
     167
     168        public void SaveToRegistry()
     169        {
     170            RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true);
     171            if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
     172
     173            regKey.SetValue("MatchWholeWordOnly", checkBoxMatchWholeWordOnly.Checked);
     174            regKey.SetValue("MatchCase", checkBoxMatchCase.Checked);
     175
     176            regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey + "\\History", true);
     177            if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey + "\\History");
     178
     179            int i = 0;
     180            regKey.SetValue("Count", comboBoxWhat.Items.Count);
     181            foreach (var item in comboBoxWhat.Items)
     182            {
     183                regKey.SetValue(i.ToString(), item);
     184                i++;
     185            }
     186
     187            while (true)
     188            {
     189                List<string> names = regKey.GetValueNames().ToList();
     190                if (names.IndexOf(i.ToString()) != -1)
     191                {
     192                    regKey.DeleteValue(i.ToString());
     193                }
     194                else break;
     195                i++;
     196            }
    124197        }
    125198    }
Note: See TracChangeset for help on using the changeset viewer.