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】【图论】【数量】【哈密顿路径】Fake bullions (CodeForces - 804F)

    题意 有n个黑帮(gang),每个黑帮有siz[i]个人,黑帮与黑帮之间有有向边,并形成了一个竞赛完全图(即去除方向后正好为一个无向完全图).在很多年前,有一些人参与了一次大型抢劫,参与抢劫的人都获得 ...

  2. Laravel使用心得

    Laravel使用心得 1.session使用 laravel的session使用时,不要使用exit和die,否则session会为空. 2.ajax提交注意框架对post的CSRF保护 在头加上& ...

  3. CentOS7 安装 jexus-5.8.2-x64

    前要提示: 如果你要安装asp.net 请参考: http://www.cnblogs.com/xiaoruilin/p/7867823.html 背景: CentOS7 Mono (Mono JIT ...

  4. 切换controller 后面的最好不要用id参数,不然会根据路由规则改变

    //切换actionResult             return RedirectToAction("Edit", "EngineeringCase",  ...

  5. 洛谷P3835 【模板】可持久化平衡树

    题目背景 本题为题目 普通平衡树 的可持久化加强版. 数据已经经过强化 感谢@Kelin 提供的一组hack数据 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作( ...

  6. easyui dialog 中 panel-body 高度太小出现 滚动条 的原因

    easyui dialog 中 panel-body 高度太小出现 滚动条 的原因: dialog 高度比 iframe 高度大79 就可以了 $.editcompanypersoninfo = fu ...

  7. 需要优化代码的leetcode

    1  关于验证字符串的问题: 2

  8. 干货|爱奇艺CDN巡检系统技术解析

    小结: 1. 中心处理系统 /1/将定制后的巡检任务拆分,通过配置与任务分发系统.CMDB*( configuration management database)将派发到边缘拨测系统/2/处理边缘拨 ...

  9. C#开发问题汇总

    问题1:HTTP 错误 500.21 - Internal Server Error处理程序“NickLeeCallbackHandler”在其模块列表中有一个错误模块“ManagedPipeline ...

  10. [vue--开发记录]使用location.href修改地址跳转页面在ie上遇到的坑

    管理后台项目上在用vue2.0开发,因为刚转vue2.0,不是太熟悉.在跳转页面的时候直接用location.href来修改地址跳转,在chrome和火狐上展现都是正常的.后面因为说要兼容到IE9,就 ...