我可以向后浏览Visual C#中的字符串吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了我可以向后浏览Visual C#中的字符串吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4181字,纯文字阅读大概需要6分钟。
内容图文
我有一个搜索功能,我可以从文本的开头到结尾搜索我的richtextbox中的字符串.当我发现最后一个从它开始的时候开始.
这里的代码:
#region Search
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
start = 0;
end = 0;
}
//Searchfield
private void toolStripTextBoxSearch_Click(object sender, EventArgs e)
{
}//end TextBoxSearch
public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
{
// Set the return value to -1 by default.
int retVal = -1;
// A valid starting index should be specified.
if (searchStart >= 0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexOfSearchText = richTextBox.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
// Determine whether the text was found in richTextBox1.
if (indexOfSearchText != -1)
{
// Return the index to the specified search text.
retVal = indexOfSearchText;
}
}
}
return retVal;
}//end FindMyText
private void buttonSearch_left_Click(object sender, EventArgs e)
{
}
private void buttonSearch_right_Click(object sender, EventArgs e)
{
int startindex = 0;
if (txtSearch.Text.Length > 0)
{
startindex = FindMyText(txtSearch.Text, start, richTextBox.Text.Length);
}
// If string was not found report it
if (startindex < 0 )
{
if (stringfoundflag==1)
{
startindex = FindMyText(txtSearch.Text, 0, richTextBox.Text.Length); //Start at Pos. 0
}
else
{
MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}//end if
if (startindex >= 0)
{
stringfoundflag = 1;
// Set the highlight color as red
//richTextBox.SelectionColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtSearch.Text.Length;
// Highlight the search string
richTextBox.Select(startindex, endindex);
// mark the start position after the position of
// last search string
start = startindex + endindex;
}
}
// Reset the richtextbox when user changes the search string
private void textBox1_TextChanged(object sender, EventArgs e)
{
start = 0;
indexOfSearchText = 0;
}
private void txtSearch_Enter(object sender, EventArgs e)
{
if (txtSearch.Text == "Search")
{
txtSearch.Text = "";
}
}
private void txtSearch_Leave(object sender, EventArgs e)
{
if (txtSearch.Text == "")
{
txtSearch.Text = "Search";
}
}
#endregion
我有两个按钮,一个搜索右边,一个左边,右边的工作就像开头的描述.
题:
我可以使用我的解决方案从按钮向右反转搜索功能,还是必须更改整个搜索功能?
示例:首先按照并跳转到最后一次,然后是倒数第二次,依此类推.
解决方法:
嗨,我发现我的问题的解决方案,我希望有人可以帮助这个.
码:
public int FindMyTextleft(string txtToSearch, int searchStart, int searchEnd)
{
// Set the return value to -1 by default.
int retVal = -1;
// A valid starting index should be specified.
if (searchStart >= 0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexOfSearchText = richTextBox.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.Reverse);
// Determine whether the text was found in richTextBox1.
if (indexOfSearchText != -1)
{
// Return the index to the specified search text.
retVal = indexOfSearchText;
}
}
}//end FindMyTextleft
return retVal;
}//end FindMyText
private void buttonSearch_left_Click(object sender, EventArgs e)
{
int startindex = 0;
if (txtSearch.Text.Length > 0 & stringfoundflag == 1)
{
startindex = FindMyTextleft(txtSearch.Text, startindex, end);
}
else if (txtSearch.Text.Length > 0)
{
startindex = FindMyTextleft(txtSearch.Text, start, richTextBox.Text.Length);
}
// If string was not found report it
if (startindex < 0)
{
if (stringfoundflag == 1)
{
startindex = FindMyTextleft(txtSearch.Text, 0, richTextBox.Text.Length); //Start at Pos. 0
}
else
{
MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}//end if
if (startindex >= 0)
{
stringfoundflag = 1;
// Set the highlight color as red
//richTextBox.SelectionColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtSearch.Text.Length;
// Highlight the search string
richTextBox.Select(startindex, endindex);
// mark the start position after the position of
// last search string
end = startindex;
}
}//buttonSearch_left_Click
友好的祝福嗅探
内容总结
以上是互联网集市为您收集整理的我可以向后浏览Visual C#中的字符串吗?全部内容,希望文章能够帮你解决我可以向后浏览Visual C#中的字符串吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。