findreplaceform.cs
来自「一个很好用的html编辑器。带源码」· CS 代码 · 共 456 行 · 第 1/2 页
CS
456 行
// labelReplace
//
this.labelReplace.Location = new System.Drawing.Point(8, 48);
this.labelReplace.Name = "labelReplace";
this.labelReplace.Size = new System.Drawing.Size(96, 23);
this.labelReplace.TabIndex = 0;
this.labelReplace.Text = "Replace With:";
//
// textReplace
//
this.textReplace.Location = new System.Drawing.Point(112, 48);
this.textReplace.Name = "textReplace";
this.textReplace.Size = new System.Drawing.Size(296, 20);
this.textReplace.TabIndex = 2;
this.textReplace.Text = "";
this.textReplace.TextChanged += new System.EventHandler(this.textReplace_TextChanged);
//
// bReplaceAll
//
this.bReplaceAll.BackColor = System.Drawing.SystemColors.Control;
this.bReplaceAll.Location = new System.Drawing.Point(176, 80);
this.bReplaceAll.Name = "bReplaceAll";
this.bReplaceAll.TabIndex = 7;
this.bReplaceAll.Text = "Replace All";
this.bReplaceAll.Click += new System.EventHandler(this.bReplaceAll_Click);
//
// bReplace
//
this.bReplace.BackColor = System.Drawing.SystemColors.Control;
this.bReplace.Location = new System.Drawing.Point(96, 80);
this.bReplace.Name = "bReplace";
this.bReplace.TabIndex = 6;
this.bReplace.Text = "Replace";
this.bReplace.Click += new System.EventHandler(this.bReplace_Click);
//
// bOptions
//
this.bOptions.BackColor = System.Drawing.SystemColors.Control;
this.bOptions.Location = new System.Drawing.Point(8, 80);
this.bOptions.Name = "bOptions";
this.bOptions.TabIndex = 5;
this.bOptions.Text = "Options";
this.bOptions.Click += new System.EventHandler(this.bOptions_Click);
//
// optionMatchCase
//
this.optionMatchCase.Location = new System.Drawing.Point(8, 8);
this.optionMatchCase.Name = "optionMatchCase";
this.optionMatchCase.Size = new System.Drawing.Size(240, 24);
this.optionMatchCase.TabIndex = 8;
this.optionMatchCase.Text = "Match Exact Case";
//
// optionMatchWhole
//
this.optionMatchWhole.Location = new System.Drawing.Point(8, 32);
this.optionMatchWhole.Name = "optionMatchWhole";
this.optionMatchWhole.Size = new System.Drawing.Size(240, 24);
this.optionMatchWhole.TabIndex = 9;
this.optionMatchWhole.Text = "Match Whole Word Only";
//
// panelOptions
//
this.panelOptions.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.panelOptions.Controls.Add(this.optionMatchCase);
this.panelOptions.Controls.Add(this.optionMatchWhole);
this.panelOptions.Location = new System.Drawing.Point(16, 152);
this.panelOptions.Name = "panelOptions";
this.panelOptions.Size = new System.Drawing.Size(424, 64);
this.panelOptions.TabIndex = 8;
//
// panelInput
//
this.panelInput.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.panelInput.Controls.Add(this.labelFind);
this.panelInput.Controls.Add(this.textFind);
this.panelInput.Controls.Add(this.labelReplace);
this.panelInput.Controls.Add(this.textReplace);
this.panelInput.Controls.Add(this.bOptions);
this.panelInput.Controls.Add(this.bReplace);
this.panelInput.Controls.Add(this.bReplaceAll);
this.panelInput.Controls.Add(this.bFindNext);
this.panelInput.Controls.Add(this.bCancel);
this.panelInput.Location = new System.Drawing.Point(16, 40);
this.panelInput.Name = "panelInput";
this.panelInput.Size = new System.Drawing.Size(424, 112);
this.panelInput.TabIndex = 9;
//
// FindReplaceForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.bCancel;
this.ClientSize = new System.Drawing.Size(458, 224);
this.Controls.Add(this.panelOptions);
this.Controls.Add(this.panelInput);
this.Controls.Add(this.tabControl);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FindReplaceForm";
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.Text = "Find and Replace";
this.tabControl.ResumeLayout(false);
this.panelOptions.ResumeLayout(false);
this.panelInput.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
// define the visibility of the options
// based on the user clicking the options button
private void bOptions_Click(object sender, System.EventArgs e)
{
options = !options;
DefineOptionsWindow(options);
} //OptionsClick
// set the state of the form based on the index slected
private void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (this.tabControl.SelectedIndex == 0)
{
findNotReplace = true;
}
else
{
findNotReplace = false;
}
DefineFindWindow(findNotReplace);
} //SelectedIndexChanged
// replace a given text string with the replace value
private void bReplace_Click(object sender, System.EventArgs e)
{
// find and replace the given text
if (!this.FindReplaceOne(findText, replaceText, this.optionMatchWhole.Checked, this.optionMatchCase.Checked))
{
MessageBox.Show("All occurrences have been replaced!", "Find and Replace");
}
} //ReplaceClick
// replace all occurences of a string with the replace value
private void bReplaceAll_Click(object sender, System.EventArgs e)
{
int found = this.FindReplaceAll(findText, replaceText, this.optionMatchWhole.Checked, this.optionMatchCase.Checked);
// indicate the number of replaces found
MessageBox.Show(string.Format("{0} occurrences replaced", found), "Find and Replace");
} // ReplaceAllClick
// find the next occurence of the given string
private void bFindNext_Click(object sender, System.EventArgs e)
{
// once find has completed indicate to the user success or failure
if (!this.FindNext(findText, this.optionMatchWhole.Checked, this.optionMatchCase.Checked))
{
MessageBox.Show("No more occurrences found!", "Find and Replace");
}
} //FindNextClick
// once the text has been changed reset the ranges to be worked with
// initially defined by the set in the constructor
private void textFind_TextChanged(object sender, System.EventArgs e)
{
ResetTextState();
} //FindTextChanged
// once the text has been changed reset the ranges to be worked with
// initially defined by the set in the constructor
private void textReplace_TextChanged(object sender, System.EventArgs e)
{
ResetTextState();
} //TextChanged
// sets the form state based on user input for Replace
private void ResetTextState()
{
// reset the range being worked with
this.FindReplaceReset();
// determine the text values
findText = this.textFind.Text.Trim();
replaceText = this.textReplace.Text.Trim();
// if no find text available disable find button
if (findText != string.Empty)
{
this.bFindNext.Enabled = true;
}
else
{
this.bFindNext.Enabled = false;
}
// if no find text available disable replace button
if (this.textFind.Text.Trim() != string.Empty && replaceText != string.Empty)
{
this.bReplace.Enabled = true;
this.bReplaceAll.Enabled = true;
}
else
{
this.bReplace.Enabled = false;
this.bReplaceAll.Enabled = false;
}
} //ResetTextReplace
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?