Changeset 2 for Common


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

Legend:

Unmodified
Added
Removed
  • Common/Database.cs

    r1 r2  
    9494        }
    9595
    96         private static DbConnection GetConnection()
     96        public static DbConnection GetConnection()
    9797        {
    9898            DbConnection connection = GetFactory().CreateConnection();
     
    529529        public void ExecuteQueries()
    530530        {
    531 #if SSS
     531#if NO_TRANSACTION
    532532            foreach (Query item in queries)
    533533            {
    534534                Database.ExecuteNonQuery(item.query, item.parameters);
    535535            }
    536 #endif
     536#else
    537537            lock (threadQueries)
    538538            {
     
    540540                Monitor.Pulse(threadQueries);
    541541            }
     542#endif
    542543            queries.Clear();
    543544        }
  • Common/FormFind.Designer.cs

    r1 r2  
    3131            this.buttonFindNext = new System.Windows.Forms.Button();
    3232            this.buttonCancel = new System.Windows.Forms.Button();
    33             this.textBoxWhat = new System.Windows.Forms.TextBox();
    3433            this.label1 = new System.Windows.Forms.Label();
    3534            this.checkBoxMatchWholeWordOnly = new System.Windows.Forms.CheckBox();
    3635            this.checkBoxMatchCase = new System.Windows.Forms.CheckBox();
    3736            this.buttonFindPrevious = new System.Windows.Forms.Button();
     37            this.comboBoxWhat = new System.Windows.Forms.ComboBox();
    3838            this.SuspendLayout();
    3939            //
     
    4141            //
    4242            this.buttonFindNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    43             this.buttonFindNext.Location = new System.Drawing.Point(304, 12);
     43            this.buttonFindNext.Location = new System.Drawing.Point(405, 15);
     44            this.buttonFindNext.Margin = new System.Windows.Forms.Padding(4);
    4445            this.buttonFindNext.Name = "buttonFindNext";
    45             this.buttonFindNext.Size = new System.Drawing.Size(88, 23);
     46            this.buttonFindNext.Size = new System.Drawing.Size(117, 28);
    4647            this.buttonFindNext.TabIndex = 4;
    4748            this.buttonFindNext.Text = "Find next";
     
    5354            this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    5455            this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    55             this.buttonCancel.Location = new System.Drawing.Point(304, 70);
     56            this.buttonCancel.Location = new System.Drawing.Point(405, 86);
     57            this.buttonCancel.Margin = new System.Windows.Forms.Padding(4);
    5658            this.buttonCancel.Name = "buttonCancel";
    57             this.buttonCancel.Size = new System.Drawing.Size(87, 23);
     59            this.buttonCancel.Size = new System.Drawing.Size(116, 28);
    5860            this.buttonCancel.TabIndex = 6;
    5961            this.buttonCancel.Text = "Cancel";
     
    6163            this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
    6264            //
    63             // textBoxWhat
    64             //
    65             this.textBoxWhat.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    66             | System.Windows.Forms.AnchorStyles.Right)));
    67             this.textBoxWhat.Location = new System.Drawing.Point(74, 14);
    68             this.textBoxWhat.Name = "textBoxWhat";
    69             this.textBoxWhat.Size = new System.Drawing.Size(224, 20);
    70             this.textBoxWhat.TabIndex = 1;
    71             this.textBoxWhat.TextChanged += new System.EventHandler(this.textBoxWhat_TextChanged);
    72             this.textBoxWhat.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxWhat_KeyPress);
    73             //
    7465            // label1
    7566            //
    7667            this.label1.AutoSize = true;
    77             this.label1.Location = new System.Drawing.Point(12, 17);
     68            this.label1.Location = new System.Drawing.Point(16, 21);
     69            this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
    7870            this.label1.Name = "label1";
    79             this.label1.Size = new System.Drawing.Size(56, 13);
     71            this.label1.Size = new System.Drawing.Size(72, 17);
    8072            this.label1.TabIndex = 0;
    8173            this.label1.Text = "Find what:";
     
    8476            //
    8577            this.checkBoxMatchWholeWordOnly.AutoSize = true;
    86             this.checkBoxMatchWholeWordOnly.Location = new System.Drawing.Point(15, 47);
     78            this.checkBoxMatchWholeWordOnly.Location = new System.Drawing.Point(20, 58);
     79            this.checkBoxMatchWholeWordOnly.Margin = new System.Windows.Forms.Padding(4);
    8780            this.checkBoxMatchWholeWordOnly.Name = "checkBoxMatchWholeWordOnly";
    88             this.checkBoxMatchWholeWordOnly.Size = new System.Drawing.Size(135, 17);
     81            this.checkBoxMatchWholeWordOnly.Size = new System.Drawing.Size(172, 21);
    8982            this.checkBoxMatchWholeWordOnly.TabIndex = 2;
    9083            this.checkBoxMatchWholeWordOnly.Text = "Match whole word only";
     
    9487            //
    9588            this.checkBoxMatchCase.AutoSize = true;
    96             this.checkBoxMatchCase.Location = new System.Drawing.Point(15, 70);
     89            this.checkBoxMatchCase.Location = new System.Drawing.Point(20, 86);
     90            this.checkBoxMatchCase.Margin = new System.Windows.Forms.Padding(4);
    9791            this.checkBoxMatchCase.Name = "checkBoxMatchCase";
    98             this.checkBoxMatchCase.Size = new System.Drawing.Size(82, 17);
     92            this.checkBoxMatchCase.Size = new System.Drawing.Size(102, 21);
    9993            this.checkBoxMatchCase.TabIndex = 3;
    10094            this.checkBoxMatchCase.Text = "Match case";
     
    10498            //
    10599            this.buttonFindPrevious.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    106             this.buttonFindPrevious.Location = new System.Drawing.Point(304, 41);
     100            this.buttonFindPrevious.Location = new System.Drawing.Point(405, 50);
     101            this.buttonFindPrevious.Margin = new System.Windows.Forms.Padding(4);
    107102            this.buttonFindPrevious.Name = "buttonFindPrevious";
    108             this.buttonFindPrevious.Size = new System.Drawing.Size(87, 23);
     103            this.buttonFindPrevious.Size = new System.Drawing.Size(116, 28);
    109104            this.buttonFindPrevious.TabIndex = 5;
    110105            this.buttonFindPrevious.Text = "Find previous";
     
    112107            this.buttonFindPrevious.Click += new System.EventHandler(this.buttonFindPrevious_Click);
    113108            //
     109            // comboBoxWhat
     110            //
     111            this.comboBoxWhat.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     112            | System.Windows.Forms.AnchorStyles.Right)));
     113            this.comboBoxWhat.FormattingEnabled = true;
     114            this.comboBoxWhat.Location = new System.Drawing.Point(95, 18);
     115            this.comboBoxWhat.Name = "comboBoxWhat";
     116            this.comboBoxWhat.Size = new System.Drawing.Size(297, 24);
     117            this.comboBoxWhat.TabIndex = 7;
     118            this.comboBoxWhat.TextChanged += new System.EventHandler(this.textBoxWhat_TextChanged);
     119            this.comboBoxWhat.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxWhat_KeyPress);
     120            //
    114121            // FormFind
    115122            //
    116             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     123            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
    117124            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    118             this.ClientSize = new System.Drawing.Size(403, 102);
     125            this.ClientSize = new System.Drawing.Size(537, 126);
     126            this.Controls.Add(this.comboBoxWhat);
    119127            this.Controls.Add(this.buttonFindPrevious);
    120128            this.Controls.Add(this.checkBoxMatchCase);
    121129            this.Controls.Add(this.checkBoxMatchWholeWordOnly);
    122130            this.Controls.Add(this.label1);
    123             this.Controls.Add(this.textBoxWhat);
    124131            this.Controls.Add(this.buttonCancel);
    125132            this.Controls.Add(this.buttonFindNext);
     133            this.Margin = new System.Windows.Forms.Padding(4);
    126134            this.MaximizeBox = false;
    127135            this.MinimizeBox = false;
    128             this.MinimumSize = new System.Drawing.Size(300, 140);
     136            this.MinimumSize = new System.Drawing.Size(394, 162);
    129137            this.Name = "FormFind";
    130138            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
     
    142150        private System.Windows.Forms.Button buttonFindNext;
    143151        private System.Windows.Forms.Button buttonCancel;
    144         private System.Windows.Forms.TextBox textBoxWhat;
    145152        private System.Windows.Forms.Label label1;
    146153        private System.Windows.Forms.CheckBox checkBoxMatchWholeWordOnly;
    147154        private System.Windows.Forms.CheckBox checkBoxMatchCase;
    148155        private System.Windows.Forms.Button buttonFindPrevious;
     156        private System.Windows.Forms.ComboBox comboBoxWhat;
    149157    }
    150158}
  • 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    }
  • Common/FormProgress.cs

    r1 r2  
    7777        private void bg_DoWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    7878        {
     79            if (e.Error != null)
     80            {
     81                currentTask.terminated = true;
     82                MessageBox.Show(e.Error.Message, "Error in background task", MessageBoxButtons.OK, MessageBoxIcon.Error);
     83            }
    7984            if (!currentTask.terminated)
    8085            {
     
    9196            }
    9297            else Close();
    93             if (e.Error != null)
    94             {
    95                 MessageBox.Show(e.Error.Message, "Error in background task", MessageBoxButtons.OK, MessageBoxIcon.Error);
    96             }
    9798        }
    9899
     
    134135            foreach (var task in tasks)
    135136            {
    136                 if (task.completed) completed += 1;               
     137                if (task.completed) completed += 1;
    137138            }
    138139            return completed == tasks.Count;
     
    154155        public int current;
    155156        public bool terminated;
    156         public bool completed; // If task was not terminated durring execution.
     157        public bool completed; // If task was not terminated during execution.
    157158        public string operation;
    158159        public delegate void CallbackEventHandler(Progress progress);
Note: See TracChangeset for help on using the changeset viewer.