using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace textbox
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
textarea.TextChanged += textarea_TextChanged;
textarea.MaxWidth = 157;
} private void textarea_TextChanged(object sender, TextChangedEventArgs e)
{ //textarea.TextChanged -= textarea_TextChanged;
int pos = textarea.SelectionStart;
string text = textarea.Text;
string modifyTxt = getTextWithNewLine(text); if(text.Length != modifyTxt.Length)
{
int pos_x = lenb(modifyTxt) - lenb(text);
textarea.Text = modifyTxt;
if(pos%20 == pos/20)
{
int val = pos + pos_x;
if(val < 0)
{
val = 0;
}
textarea.SelectionStart = val;
}
else
{
textarea.SelectionStart = pos;
}
}
//Console.WriteLine(modifyTxt); //textarea.TextChanged += textarea_TextChanged; }
int lenb(string c)
{
int lens = Encoding.GetEncoding("Shift_Jis").GetByteCount(c);
return lens;
}
string getTextWithNewLine(string text)
{
char[] array = text.ToCharArray();
StringBuilder sb = new StringBuilder();
bool autoNewLine = true;
int index = 0;
int rowcont = 0;
for(int i=0;i<array.Length;i++)
{
char c = array[i];
if(c == '\r')
{
autoNewLine = false;
continue;
}
else if (c == '\n')
{
if (autoNewLine)
{
if(index < 19)
{
continue;
}
}else
{
sb.Append("\r\n");
index = 0;
rowcont++;
autoNewLine = true;
}
}
else
{ if (index > 19)
{
sb.Append('\n');
index = 0;
rowcont++;
}
sb.Append(c);
index += lenb(c + "");
}
}
return sb.ToString(); }
}
}
<TextBox x:Name="textarea" AcceptsReturn="True"  Width="157" Height="200" VerticalScrollBarVisibility="Visible" BorderThickness="1">12345678901234567890</TextBox>

textbox 未的更多相关文章

  1. WPF的TextBox的焦点获取与失去焦点的死循环解决方案

    在WPF中实现一个弹出层自动获取焦点,弹出层实现是通过其UserControl的依赖属性Visibility的绑定实现的,让UserControl上的TextBox获取焦点,初始实现代码如下: pub ...

  2. ApplicationCommands 应用程序常见命令

    ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c 在WPF中,许多控件都自动集成了固有的命令集.比如文本框TextBox就提供了复制(Copy),粘贴 ...

  3. Windows Presentation Foundation (WPF)中的命令(Commands)简述

    原文:Windows Presentation Foundation (WPF)中的命令(Commands)简述 ------------------------------------------- ...

  4. ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c

    在WPF中,许多控件都自动集成了固有的命令集.比如文本框TextBox就提供了复制(Copy),粘贴(Paste),裁切(Cut),撤消(Undo)和重做(Redo)命令等. WPF提供常用应用程序所 ...

  5. C#用户自定义控件(含源代码)-透明文本框

    using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...

  6. [转]WPF命令集 Command

    在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制.粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节 ...

  7. Ie浏览器TextBox文本未居中

    Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中  vertical-align:middle

  8. 【MVVM DEV】DataColumn中的TextBox与ComboBox的并存

    一.前言       在WPF编程中,有时候我们使用DataGrid会需要在一个DataColumn中既有TextBox,也要有ComboBox或者TextBlock等其他数据显示样式. 这个时候我们 ...

  9. ASP.NET 给作为隐藏域的TextBox赋值之后提交表单,无响应?

    操作步骤: 给页面隐藏TextBox赋值,然后触发ASP.NET change事件,调用ASP.NET后台方法,调用后执行客户端脚本this.RegisterClientScriptBlock(Dat ...

随机推荐

  1. Codeforces.871D.Paths(莫比乌斯反演 根号分治)

    题目链接 \(Description\) 给定\(n\),表示有一张\(n\)个点的无向图,两个点\(x,y\)之间有权值为\(1\)的边当且仅当\(\gcd(x,y)\neq1\).求\(1\sim ...

  2. EMI优化

    一般印刷电路板之间的高速信号线路无法通过FCC和VDE辐射测试. 优化方案有以下3种: 1.高频滤波 通常做法在每个逻辑驱动器上串联一个小阻抗,并经过一个旁路电容接地. 旁路电容接地需足够干净,如机箱 ...

  3. angular.injector()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 转 MYSQL InnoDB Record, Gap, and Next-Key Locks

    http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html InnoDB has several types of re ...

  5. JS-函数的构造函数Function

    ##  在js中使用Function可以实例化函数对象,也就是说在js中函数和普通对象一样. 函数在Js中是比较特殊的,拥有很多功能. 1,函数是对象,就可以使用对象的动态特性. 2,函数是对象,就有 ...

  6. shell 环境下MySQL的基本操作指令总结

    一.对数据库的基本操作 show databases;                      //列出数据库use database_name;               //使用databas ...

  7. Promise 用法

    Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法. 那就new一个 Promise的构造函数接收一个参数,是 ...

  8. Python练手例子(7)

    37.对10个数进行排序. 程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换. #python 3.7 if __n ...

  9. 架构.Net 到 Linux

    nginx + .net core + mysql + radis + rabbitmq

  10. iOS HTML特殊字符转译

    something about you Bye Bye è; un saluto ​ 我使用NSXMLParser,它是顺利,直到它找到èHTML实体.它要求foundCharacters:为“再见” ...