private bool ExecFind()
{
// TextBox コントロール内の文字列
string editString = _textBox.Text;
// 検索を行う文字列
string findString = findTextBox.Text;
// 検索を行う文字列の長さ
int findStringLength = findString.Length;
// 検索開始位置の設定([先頭から]ラジオボタンが選択されている場合は 0 = 先頭 を設定)
int findPoint = (topPosRadio.Checked) ? 0 : _textBox.SelectionStart;
// 二回目以降の検索位置の指定
findStartIndex = (findStartIndex == 0) ? findPoint : findStartIndex;
// 検索処理
findPoint = editString.IndexOf(findString, findStartIndex, (LgSmCheckBox.Checked) ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase);
// 検索する文字列が見つからなかった場合
if (findPoint == -1) {
const string MSGBOX_FINDED_STRING = "ドキュメントの最後まで検索しました。\nもう一度先頭から検索しますか?";
const string MSGBOX_REPLACED_STRING = " 件置換しました";
string msgboxString = (_mode == dialogMode.Find) ? MSGBOX_FINDED_STRING : findCount.ToString() + MSGBOX_REPLACED_STRING;
string msbox_NothingWord = "\"" + findString + "\"は見つかりません。";
if (findCount != 0) {
if (MessageBox.Show(this, msgboxString, dialogTitle,
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
ResetFindPosition();
}
else {
this.Close();
this.Dispose();
}
}
else { // 検索の開始位置が[現在位置から]の場合は先頭から検索しなおすかいちおう確認
if (currentPosRadio.Checked) {
string msgbox_string = "現在位置から" + MSGBOX_FINDED_STRING;
if (MessageBox.Show(this, msgbox_string, dialogTitle,
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
ResetFindPosition();
}
else {
this.Close();
this.Dispose();
}
}
else {
MessageBox.Show(this, msbox_NothingWord, dialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
return false;
}
else {
// 見つかった文字を選択
_textBox.Select(findPoint, findStringLength);
// TextBox が選択された文字列を表示するようキャレットを移動
_textBox.ScrollToCaret();
// [次を検索] ボタンをクリックされた際の検索開始位置を設定
findStartIndex = findPoint + findStringLength;
// 検索がヒットした回数をカウント
findCount++;
// テキストボックスにフォーカスをあてる
_textBox.Focus();
return true;
}
}