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. Metasploit学习记录---Nessus安装部署

    1.Nessus介绍 nessus是目前世界上最为流行的漏洞扫描器之一.她提供完整的电脑漏洞扫描服务,并随时更新其漏洞数据库.Nessus不同于传统的漏洞扫描软件,可同时在本机或远端上遥控,进行系统的 ...

  2. python底层原理

    有同学问到了一个问题,python中存储变量是通过内存地址来存储,那么python又是如何去判断内存中的地址是什么数据类型的呢.经过查找,找到这篇文章: 原博客地址:http://www.cnblog ...

  3. XVIII Open Cup named after E.V. Pankratiev. GP of Romania

    A. Balance 不难发现确定第一行第一列后即可确定全部,列不等式单纯形求解线性规划即可. #include<cstdio> #include<algorithm> usi ...

  4. [LeetCode] Most Common Word 最常见的单词

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  5. 电子产品使用感受之-- AirPods + Apple Watch S4 = Smart iPod ?

  6. java_xml_解析

    xml解析的两种的基本方式 1:SAX解析:一行一行的解析,不回头 2:DOM解析:将整个XML以树状读到内存中,然后需要哪一部分就取哪一部分 SAX解析: 基于java的步骤: //1.获取解析工厂 ...

  7. django的闪现和增、删、改、查

    使用 messages 闪现在views.py中导入 from django.contrib import messages 在html中 {% if messages %} {% for mess ...

  8. python升级pip和Django安装

    1.centos7默认python版本为2.7.5,现升级到3.6.0 2.wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz ...

  9. C# DataTable 转 实体类

    C# 中查询结果DataTable转实体类: 比如:List<RtmInterview> rtmList = GetDataById( id); public List<RtmInt ...

  10. MarkDown语言

    参考: 参考:https://typora.io/ 参考:https://caret.io/ Markdown是一种轻量级标记语言,创始人为約翰·格魯伯(英语:John Gruber). 它允许人们“ ...