1 aaaaaaaaaaaaaaaaa
aaaaaaaaaa
2 bbbbbbb
3 ccccccccccccccccccc
cc
4 ddd
5 eeeee
public class TextBoxHelper
{
public Color BackgroundColor = Colors.White; // 背景色
public Color TextColor = Colors.Black; // 文字颜色
public Color LinecodeBackground = Colors.Gainsboro; // 显示行号背景色
public Color LinecodeColor = Colors.SteelBlue; // 显示行号文字色
public FontFamily fontFamily = new FontFamily("Microsoft YaHei"); // 字体
public double Fontsize = 12; // 字体大小
public double Line_Height = 20; // 行高度
public double Linecode_Width = 30; // 行号宽度
public double Linecode_Interval = 5; // 行号右侧间隔
public TextBox Editor; // TextBox组件
private ScrollTemplate scrollTemplate_TextBox;
public Canvas Editor_Box;
private TextBox Linemeasure;
private Canvas Linenumber;
/// <summary>
/// 带行号的TextBox
/// </summary>
/// <param name="obj">父容器</param>
/// <param name="x">位置 x</param>
/// <param name="y">位置 y</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public void LinenumberEditor(Canvas obj, double x, double y, double width, double height)
{
Editor_Box = new Canvas()
{
Width = width + Linecode_Width,
Height = height,
Background = new SolidColorBrush(BackgroundColor),
Clip = new RectangleGeometry(new Rect(0, 0, width, height))
};
Canvas.SetLeft(Editor_Box, x);
Canvas.SetTop(Editor_Box, y);
Editor = new TextBox()
{
Width = width - Linecode_Width,
BorderThickness = new Thickness(0),
TextWrapping = TextWrapping.Wrap,
AcceptsReturn = true,
FontFamily = fontFamily,
FontSize = Fontsize,
Foreground = new SolidColorBrush(TextColor),
Background =Brushes.Transparent
};
Editor.TextChanged += Editor_TextChanged;
TextBlock.SetLineHeight(Editor, Line_Height);
Canvas.SetLeft(Editor, 30);
WrapPanel text_panel = new WrapPanel()
{
Width = Editor.Width,
Orientation = Orientation.Vertical,
};
text_panel.Children.Add(Editor);
scrollTemplate_TextBox = new ScrollTemplate();
scrollTemplate_TextBox.ScrollViewer.Background = Brushes.WhiteSmoke;
scrollTemplate_TextBox.InitializeScrollViewer(Editor_Box, text_panel, Linecode_Width, 0, width- Linecode_Width, height, ScrollViewer_Type.Win10);
scrollTemplate_TextBox.ScrollViewer.ScrollChanged += (s, e) => {
GeneralTransform generalTransform = Editor.TransformToAncestor(Editor_Box);
Point point = generalTransform.Transform(new Point(0, 0));
Canvas.SetTop(Linenumber, point.Y);
};
scrollTemplate_TextBox.ScrollViewer.PreviewMouseDown += (s, e) => {
obj.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => { Keyboard.Focus(Editor); }));
};
Linenumber = new Canvas()
{
Width = Linecode_Width,
Height = height,
Background = new SolidColorBrush(LinecodeBackground)
};
Editor_Box.Children.Add(Linenumber);
TextBlock Linecode = new TextBlock()
{
Height = Line_Height,
Width = Linecode_Width - Linecode_Interval,
TextAlignment = TextAlignment.Right,
Text = "1",
FontFamily = fontFamily,
FontSize = Fontsize,
Foreground = new SolidColorBrush(LinecodeColor),
Background = Brushes.Transparent
};
Linenumber.Children.Add(Linecode);
Linemeasure = new TextBox()
{
Width = width - Linecode_Width,
Height = 100,
TextWrapping = TextWrapping.Wrap,
BorderThickness = new Thickness(0),
FontFamily = fontFamily,
FontSize = Fontsize,
Visibility = Visibility.Hidden
};
Canvas.SetLeft(Linemeasure, -x);
Canvas.SetTop(Linemeasure, -1);
obj.Children.Add(Linemeasure);
Editor_Box.MouseDown += (s, e) => { Editor.Focus(); };
obj.Children.Add(Editor_Box);
}
private void Editor_TextChanged(object sender, TextChangedEventArgs e)
{
if (Linenumber.Children.Count != Editor.LineCount)
{
if (Linenumber.Children.Count < Editor.LineCount)
{
for (int i = Linenumber.Children.Count; i < Editor.LineCount; i++)
{
TextBlock Linecode = new TextBlock()
{
Height = Line_Height,
Width = Linecode_Width - Linecode_Interval,
TextAlignment = TextAlignment.Right,
FontFamily = fontFamily,
FontSize = Fontsize,
Foreground = new SolidColorBrush(LinecodeColor),
Background = Brushes.Transparent
};
Canvas.SetTop(Linecode, i * Line_Height);
Linenumber.Children.Add(Linecode);
}
}
else
if (Linenumber.Children.Count > Editor.LineCount)
{
for (int i = Linenumber.Children.Count-1 ; i > Editor.LineCount - 1; i--)
{
Linenumber.Children.RemoveAt(i);
}
}
for (int i = 0; i < Linenumber.Children.Count; i++)
{
(Linenumber.Children[i] as TextBlock).Text = "";
}
string[] split = Editor.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
int line = 0;
for (int i = 0; i < split.Length; i++)
{
(Linenumber.Children[line] as TextBlock).Text = (i + 1).ToString();
Linemeasure.Text = split[i];
line += Linemeasure.LineCount;
}
Linenumber.Height = Editor.LineCount* Line_Height < Editor_Box.Height? Editor_Box.Height: Editor.LineCount * Line_Height;
}
}
1、TextBox不自动换行 这种方式处理最简单 下载: 链接:https://pan.baidu.com/s/1RViZ5F6ypf7WOzg4bBzHYw 提取码:9qvf using System; using System.Windows; using System.Windows.Controls; using System....
一般的,我们做一个带行号的编辑控件,通常都使用RichTextBox。 网络上很多的都是 RichTextBox + Panel 来实现。 为 TextBox 实现带行号功能。在网上查了查,发现例子很少,通常都是考虑两个TextBox。好不...
文章目录RichTextBox实现思路代码TextBox实现思路代码遇到的问题效果 RichTextBox实现 思路 利用TextPointer 使用GetPositionAtOffst(double offset)方法获取需要高亮的文字开始位置的索引和结尾位置的索引 获取...
wpf自定义文本控件,高亮显示文本内内容及行号
WPF TextBox如果行数过多,比如1、2千行,这时候按下ctrl-a进行全选(卡顿很慢),再点击右键,这时候右键菜单要么打开迟缓,要么不停闪烁,点击菜单项后,不会执行相应的菜单命令。根据这个情况,脱离右键菜单,...
但是这一列似乎和 TextBox 的属性有关,也就是一旦你在资源中为TextBox定义了全局样式,又在这个样式中指定了宽或高时,这个列就会出现。 解决: 如果不让它显示就要重新设置它的宽度为0就行了,也就是在GridView...
当textbox为多行时,获取它的行数:int count = this.txtMsgInfo.Lines.GetUpperBound(0); 转载于:https://www.cnblogs.com/pnljs/p/3746338.html
一。绑定到元素对象 1。元素和元素(XAML、代码) 1.1XAML格式 目标属性="{Binding ElementName=源控件, Path=源属性, Mode=…}" 1.2代码: Binding binding = new Binding(); binding.Source = 源控件;...
我将textbox设置为多行,如果想获取其中文本的行数,可以采用textbox1.Lines.Length来获取,这个方法应该是查找文本中的换行符数量吧。我现在遇到的问题是:如果我连续输入字符,不敲回车,当字符到达文本框的右边框...
编者:乌龙哈里 2017-02-23参考:King Cobra 博客流泉飞石 博客董辉 百度知道答题章节:添加列和数据源选择单位设置自动添加行号Enter 键做成 Tab 键的效果同步更新数据源在选中 Cell 所在行上插入新行或删除选中.....
int charIndex = this.richTextBox1.... 其中x,y为坐标位置 如想获得RichTextBox窗口内第一行的行号(因窗口可滚动,第1行的行号不一定是1) 可指定x,y=5,5
今天遇到一个超级bug, Textbox做了限制, 只能输入数字。 结果在搜狗输入法输入中文时导致崩溃, 出错信息如下: 未处理 System.InvalidOperationException Message=无法关闭撤消单元,因为不存在已打开的...
写过程序的人应该不会对RichTextBox感到陌生,与TextBox相比,它封装了更丰富的对象,使你的程序使用起来更加方便。但是它也有美中不足的地方。比如说没有行号,滚动条滚动进度无法控制等一系列的问题。 今天,就据我...
int totalline = textBox1.GetLineFromCharIndex(textBox1.Text.Length) + 1;//得到总行数。该行数会随着文本框的大小改变而改变;若只认回车符为一行(不考虑排版变化)请用 总行数=textBox1.Lines.Length;(记事本2是...
写过程序的人应该不会对RichTextBox感到陌生,与TextBox相比,它封装了更丰富的对象,使你的程序使用起来更加方便。但是它也有美中不足的地方。比如说没有行号,滚动条滚动进度无法控制等一系列的问题。 今天,就据...
************************************************************功能:取得TextBox、RichTextBox光标所在的行和列支持中文,一个汉字算一列有问题请给我写邮件作者:Matrix邮件:ASPBIT@163.COM2003-01-24修正了马虎...
方法一: private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e){ if (dataGrid1.SelectedIndex != -1) { this.textBox1.Text=(e.AddedI
WPF XamlObjectWriterException:无法创建未知类型"Grid" 原文:WPF XamlObjectWriterException:无法创建未知类型"Grid" using (FileStream fs = new FileStream("UnitFile/Report2.xaml", FileMode....
在项目中碰到了这个问题,在网上搜了半天,发现了一种解决方案,这种方法主要是在DataGrid加载Row时设定Row的行号,主要代码如下: 1 DataGrid.LoadingRow += (sender,e)=>2 {3 e.Row.Header = e.Row.G
AvalonEdit是基于WPF的可扩展的文本编辑器。
1 编写一个Hex Edit控件,使用VS2010建立一个类控件库,分别增加两个类文件,一个命名为HexEdit.cs, 一个命名为TextEditControl.cs,具体内容如下所示: 1)HexEdit.cs using System.Drawing;...
今天在做WPF项目的时候,用到了一些自定义控件,以前项目的中的控件,但是由于要用到自己现在的项目中,对命名控件改动,单是没有完全改过来。导致出现如下的错误。 “设置属性“System.Windows.ResourceDictionary...
首先感谢我的老王同学,给了我很大的帮助,再次献上诚挚的敬意!...括号里面写的是行号,但我之前一直用的都不是MVVM,都是直接 控件名.ScrollIntoView()来实现这个功能,我试了下 控件名.View.MoveFocusedRow()确
1. ProgressBar ProgressBar 有一个默认的Minimum 属性0,和一个默认的Maximum 属性100,它只添加了两个RangeBase 已经提供了的公共属性: ·IsIndeterminate —— 当把它设置为true 时,ProgressBar 会显示一段...
数据banding的最简单情形是,源对象时WPF元素而且源属性是依赖性属性。前面章节解释过,依赖项属性具有内置的更改通知支持。因此,当在源对象中改变依赖项属性的值时,会立即更新目标对象中的绑定属性。这正是我们所...
wpf中如何确定 Freezable 是否处于冻结状态wpf中如何对 FrameworkElement 的大小进行动画处理wpf如何使 UIElement 呈现为透明或半透明wpf 全屏Tooltip 内容变化问题wpf中用C#代码写动画wpf 获取屏幕上固定坐标的颜色...
WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0; 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 流泉飞石 博客 董辉 百度知道答题 章节: 添加列和数据源 选择...
上周,微软发布Visual Studio LightSwitch 2011正式版,为商业应用开发人员推出一个新的应用开发产品。Visual Studio LightSwitch 2011 作为一种简单的,自助服务式开发工具,能够迅速而且轻松的创建桌面或Web应用...
title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2018-05-25 21:18:45 +0800 dotnet ...本文是记录我从入...
包括招聘的机考题,及面试过程中会问到的数据结构的相关内容,排序算法全部包括并且有改进算法,一点点改进可以让你表现的与众不同,如果好的话给点评价吧亲