FindReplaceForm.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #region Using directives
  2. using System.Windows.Forms;
  3. #endregion
  4. namespace WinHtmlEditor
  5. {
  6. /// <summary>
  7. /// Form designed to control Find and Replace operations
  8. /// Find and Replace operations performed by the user control class
  9. /// Delegates need to be defined to reference the class instances
  10. /// </summary>
  11. internal partial class FindReplaceForm : Form
  12. {
  13. // constants defining the form sizes
  14. private const int FORM_HEIGHT_WITH_OPTION = 264;
  15. private const int TAB_HEIGHT_WITH_OPTION = 216;
  16. private const int FORM_HEIGHT_WITHOUT_OPTION = 200;
  17. private const int TAB_HEIGHT_WITHOUT_OPTION = 152;
  18. // private variables defining the state of the form
  19. private bool options = false;
  20. private bool findNotReplace = true;
  21. private string findText;
  22. private string replaceText;
  23. // internal delegate reference
  24. private FindReplaceResetDelegate FindReplaceReset;
  25. private FindReplaceOneDelegate FindReplaceOne;
  26. private FindReplaceAllDelegate FindReplaceAll;
  27. private FindFirstDelegate FindFirst;
  28. private FindNextDelegate FindNext;
  29. /// <summary>
  30. /// Public constructor that defines the required delegates
  31. /// Delegates must be defined for the find and replace to operate
  32. /// </summary>
  33. public FindReplaceForm(string initText, FindReplaceResetDelegate resetDelegate, FindFirstDelegate findFirstDelegate, FindNextDelegate findNextDelegate, FindReplaceOneDelegate replaceOneDelegate, FindReplaceAllDelegate replaceAllDelegate, bool findOrReplace)
  34. {
  35. //
  36. // Required for Windows Form Designer support
  37. //
  38. InitializeComponent();
  39. // Define the initial state of the form assuming a Find command to be displayed first
  40. DefineFindWindow(findOrReplace);
  41. DefineOptionsWindow(options);
  42. // ensure buttons not initially enabled
  43. this.bFindNext.Enabled = false;
  44. this.bReplace.Enabled = false;
  45. this.bReplaceAll.Enabled = false;
  46. // save the delegates used to perform find and replcae operations
  47. this.FindReplaceReset = resetDelegate;
  48. this.FindFirst = findFirstDelegate;
  49. this.FindNext = findNextDelegate;
  50. this.FindReplaceOne = replaceOneDelegate;
  51. this.FindReplaceAll = replaceAllDelegate;
  52. // define the original text
  53. this.textFind.Text = initText;
  54. } //FindReplaceForm
  55. /// <summary>
  56. /// Setup the properties based on the find or repalce functionality
  57. /// </summary>
  58. private void DefineFindWindow(bool find)
  59. {
  60. this.textReplace.Visible = !find;
  61. this.labelReplace.Visible = !find;
  62. this.bReplace.Visible = !find;
  63. this.bReplaceAll.Visible = !find;
  64. this.textFind.Focus();
  65. this.tabControl.SelectedIndex = find ? 0 : 1;
  66. } //DefineFindWindow
  67. /// <summary>
  68. /// Defines if the options dialog is shown
  69. /// </summary>
  70. private void DefineOptionsWindow(bool options)
  71. {
  72. if (options)
  73. {
  74. // Form displayed with the options shown
  75. this.bOptions.Text = "简单";
  76. this.panelOptions.Visible = true;
  77. this.tabControl.Height = TAB_HEIGHT_WITH_OPTION;
  78. this.Height = FORM_HEIGHT_WITH_OPTION;
  79. this.optionMatchCase.Focus();
  80. }
  81. else
  82. {
  83. // Form displayed without the options shown
  84. this.bOptions.Text = "高级";
  85. this.panelOptions.Visible = false;
  86. this.tabControl.Height = TAB_HEIGHT_WITHOUT_OPTION;
  87. this.Height = FORM_HEIGHT_WITHOUT_OPTION;
  88. this.textFind.Focus();
  89. }
  90. } //DefineOptionsWindow
  91. /// <summary>
  92. /// Event defining the visibility of the options
  93. /// Based on the user clicking the options button
  94. /// </summary>
  95. private void bOptions_Click(object sender, System.EventArgs e)
  96. {
  97. options = !options;
  98. DefineOptionsWindow(options);
  99. } //OptionsClick
  100. /// <summary>
  101. /// Event setting the state of the form
  102. /// Based on the user clicking a new form tab
  103. /// </summary>
  104. private void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
  105. {
  106. if (this.tabControl.SelectedIndex == 0)
  107. {
  108. findNotReplace = true;
  109. }
  110. else
  111. {
  112. findNotReplace = false;
  113. }
  114. DefineFindWindow(findNotReplace);
  115. } //SelectedIndexChanged
  116. /// <summary>
  117. /// Event replacing a single occurrence of a given text with another
  118. /// Based on the user clicking the replace button
  119. /// </summary>
  120. private void bReplace_Click(object sender, System.EventArgs e)
  121. {
  122. // find and replace the given text
  123. if (!this.FindReplaceOne(findText, replaceText, this.optionMatchWhole.Checked, this.optionMatchCase.Checked))
  124. {
  125. MessageBox.Show("所有的内容都被替换!", "查找和替换");
  126. }
  127. } //ReplaceClick
  128. /// <summary>
  129. /// Event replacing all the occurrences of a given text with another
  130. /// Based on the user clicking the replace all button
  131. /// </summary>
  132. private void bReplaceAll_Click(object sender, System.EventArgs e)
  133. {
  134. int found = this.FindReplaceAll(findText, replaceText, this.optionMatchWhole.Checked, this.optionMatchCase.Checked);
  135. // indicate the number of replaces found
  136. MessageBox.Show(string.Format("{0} 个内容已被替换", found), "查找和替换");
  137. } // ReplaceAllClick
  138. /// <summary>
  139. /// Event finding the next occurrences of a given text
  140. /// Based on the user clicking the find next button
  141. /// </summary>
  142. private void bFindNext_Click(object sender, System.EventArgs e)
  143. {
  144. // once find has completed indicate to the user success or failure
  145. if (!this.FindNext(findText, this.optionMatchWhole.Checked, this.optionMatchCase.Checked))
  146. {
  147. MessageBox.Show("没有找到更多的内容!", "查找和替换");
  148. }
  149. } //FindNextClick
  150. /// <summary>
  151. /// Once the text has been changed reset the ranges to be worked with
  152. /// Initially defined by the set in the constructor
  153. /// </summary>
  154. private void textFind_TextChanged(object sender, System.EventArgs e)
  155. {
  156. ResetTextState();
  157. } //FindTextChanged
  158. /// <summary>
  159. /// Once the text has been changed reset the ranges to be worked with
  160. /// Initially defined by the set in the constructor
  161. /// </summary>
  162. private void textReplace_TextChanged(object sender, System.EventArgs e)
  163. {
  164. ResetTextState();
  165. } //TextChanged
  166. /// <summary>
  167. /// Sets the form state based on user input for Replace
  168. /// </summary>
  169. private void ResetTextState()
  170. {
  171. // reset the range being worked with
  172. this.FindReplaceReset();
  173. // determine the text values
  174. findText = this.textFind.Text.Trim();
  175. replaceText = this.textReplace.Text.Trim();
  176. // if no find text available disable find button
  177. if (findText != string.Empty)
  178. {
  179. this.bFindNext.Enabled = true;
  180. }
  181. else
  182. {
  183. this.bFindNext.Enabled = false;
  184. }
  185. // if no find text available disable replace button
  186. if (this.textFind.Text.Trim() != string.Empty && replaceText != string.Empty)
  187. {
  188. this.bReplace.Enabled = true;
  189. this.bReplaceAll.Enabled = true;
  190. }
  191. else
  192. {
  193. this.bReplace.Enabled = false;
  194. this.bReplaceAll.Enabled = false;
  195. }
  196. } //ResetTextReplace
  197. }
  198. }