1.如何查找RichTextBox内容并选择它?
WinFrom 中可以用RichTextBox1.Select(pos, keyword.Length);WPF中如何选中查找的内容?2.要做个ListBox的鼠标滑入效果,鼠标滑入到ListBox条目上面,那一条自动选中。
在WinFrom里面是 private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
Point m = new Point(e.X, e.Y);
int index = GetItemAt(this.listBox1, e.X, e.Y);
if (this.listBox1.SelectedItems.Count > 0 && this.listBox1.SelectedIndex != index)
{
this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
} if (index != -1 && this.listBox1.SelectedIndex != index)
{ this.listBox1.(index, true);
}
} private int GetItemAt(ListBox listBox, int X, int Y)
{
int index = -1;
for (int i = 0; i < listBox.Items.Count; i++)
{
System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
if (r.Contains(new Point(X, Y)))
{
index = i; ;
break;
}
}
return index;ListBox里面没有GetItemRectangle这个属性,请问如何实现该效果?谢谢。
WinFrom 中可以用RichTextBox1.Select(pos, keyword.Length);WPF中如何选中查找的内容?2.要做个ListBox的鼠标滑入效果,鼠标滑入到ListBox条目上面,那一条自动选中。
在WinFrom里面是 private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
Point m = new Point(e.X, e.Y);
int index = GetItemAt(this.listBox1, e.X, e.Y);
if (this.listBox1.SelectedItems.Count > 0 && this.listBox1.SelectedIndex != index)
{
this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
} if (index != -1 && this.listBox1.SelectedIndex != index)
{ this.listBox1.(index, true);
}
} private int GetItemAt(ListBox listBox, int X, int Y)
{
int index = -1;
for (int i = 0; i < listBox.Items.Count; i++)
{
System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
if (r.Contains(new Point(X, Y)))
{
index = i; ;
break;
}
}
return index;ListBox里面没有GetItemRectangle这个属性,请问如何实现该效果?谢谢。
解决方案 »
- 求助:重定向输入的问题
- 我发现TEXTBOX控件的一个疑似BUG,求验证。。。
- Udp一对多传输文件
- 请问 C#类 代码可不可以写成几个文件
- Infragistics NetAdvantage中的UltraWebGrid怎么实现分页
- [DllImport("user32.dll")]?
- 请高手指点一下UPDATE语句,使其更有效率,更为精简(oracle)
- 如何让一个TabControl对象默认选中一个选项卡?
- 哪个有C#开发的实例用书(电子版的或者那里有do)
- 水晶报表
- C# 子线程怎么操作主线程的组件??
- 运用.net三层思想(models层,DAL层,BLL层) 怎么处理数据库中的一个动态表?
int selectLength = 10; //获取起始,结束的TextPointer
TextPointer tpStart = richTextBox1.Document.ContentStart;
TextPointer tpOffset = tpStart.GetPositionAtOffset(selectLength); //获取Range对象,并做选择
TextRange range = richTextBox1.Selection;
range.Select(tpStart, tpOffset); //高亮选择
range.ApplyPropertyValue(TextElement.ForegroundProperty,
new SolidColorBrush(Colors.Blue));
range.ApplyPropertyValue(TextElement.FontWeightProperty,
FontWeights.Bold);
Q2:你需要让ListBox中的ListBoxItem响应MouseEnter事件,
因为ListBoxItem也是一个FrameworkElement,能响应路由事件.
假设你的ListBoxItem事先已经加好,我们遍历加上事件响应 foreach (object item in listBox1.Items)
{
ListBoxItem lbi = item as ListBoxItem;
if (lbi != null)
{
lbi.MouseEnter += delegate
{
foreach (object obj in listBox1.Items)
{
ListBoxItem lbItem = obj as ListBoxItem;
if(lbItem != null)
{
lbItem.IsSelected = lbi == lbItem;
}
}
};
}
}
1.我根据一个字符串查询,richTextBox1如何让内容里面所有的改字符串高亮?如何让第一个字符串属于选中状态。2。你的方法不行。试了没用。。
选中部分的背景笔刷为一种颜色(蓝,黑),选中部分的字体为高亮色,跟背景笔刷有明显差异即可.
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Blue)); range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));2。如果你直接通过ItemSource绑定,是不行的,因为你的ListBox的Items中的对象类型不是ListBoxItem.如果你一个个加ListBoxItem自然没问题.Trigger的例子
ObservableCollection<string> collection = new ObservableCollection<string>();
collection.Add("aa");
collection.Add("bb");
collection.Add("cc");
listBox1.ItemsSource = collection; //需要判断OriginalSource
void listBox1_MouseMove(object sender, MouseEventArgs e)
{
//自动让ListBox获得焦点
if (!listBox1.IsFocused)
{
listBox1.Focus();
} TextBlock tb = e.OriginalSource as TextBlock;
if (tb != null)
{
//如果绑定的Source都是String,这么处理即可,
//否则,转成相应的类型再做赋值
listBox1.SelectedItem = tb.Text;
}
}
collection.Add("aa");
collection.Add("bb");
collection.Add("cc");
listBox1.ItemsSource = collection; //需要判断OriginalSource
void listBox1_MouseMove(object sender, MouseEventArgs e)
{
//自动让ListBox获得焦点
if (!listBox1.IsFocused)
{
listBox1.Focus();
} TextBlock tb = e.OriginalSource as TextBlock;
if (tb != null)
{
//如果绑定的Source都是String,这么处理即可,
//否则,转成相应的类型再做赋值
listBox1.SelectedItem = tb.Text;
}
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
string keyword = "aa";
foreach (var ret in FindAllMatchedTextRanges(richTextBox1, keyword))
{
ret.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Blue));
ret.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));
}
} private List<TextRange> FindAllMatchedTextRanges(RichTextBox rtb, string keyword)
{
List<TextRange> trList = new List<TextRange>();
//设置文字指针为Document初始位置
TextPointer position = rtb.Document.ContentStart;
while (position != null)
{
//向前搜索,需要内容为Text
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
//拿出Run的Text
string text = position.GetTextInRun(LogicalDirection.Forward);
//可能包含多个keyword,做遍历查找
int index = 0;
while (index < text.Length)
{
index = text.IndexOf(keyword, index);
if (index == -1)
{
break;
}
else
{
//添加为新的Range
TextPointer start = position.GetPositionAtOffset(index);
TextPointer end = start.GetPositionAtOffset(keyword.Length);
trList.Add(new TextRange(start, end));
index += keyword.Length;
}
}
}
//文字指针向前偏移
position = position.GetNextContextPosition(LogicalDirection.Forward);
}
return trList;
}
, 学习了,之前只想到了到RichTextBox.Document中去找这个TextPointer,原来是这么用的