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. 第八篇 Flask配置

    Flask 是一个非常灵活且小而精的web框架 , 那么灵活性从什么地方体现呢? 列如  Flask配置,这个东西怎么用呢? 它能给我们带来怎么样的方便呢? app配置 首先展示一下: from fl ...

  2. MariaDB基本操作--(创建用户)(转)

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  3. select应用

    服务端源码 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: zengchunyun "& ...

  4. vue_条件渲染_v-if_v-else_v-show

    data: { ok: true flag: false } 1. 成对出现的 v-if 和 v-else 原理是: 标签的删除与重新创建 ,有些情况必须用 v-if <p v-if=" ...

  5. [LeetCode] Implement Rand10() Using Rand7() 使用Rand7()来实现Rand10()

    Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...

  6. CUDA相关问题

    之前装了GPU驱动后,再装了CUDA 9.0,再装了cuDNN,并且对样例mnistCUDNN进行执行,显示“Test passed!"通过.但是倒忘了有没有测试CUDA是否安装成功.驱动也 ...

  7. oralce 将浮点型字段,转为指定精度 并且四舍五入

    使用 Round()  函数,该函数有两个参数,第一个参数: 将要转换的字段,第二个参数:保留的小数点位数 1 代表保留1位 2 代表保留2位 例子: SELECT round(123.446,1) ...

  8. idea创建第一个普通java web项目

    1.新建项目(工作空间) 选择空项目 输入项目名称,点击完成 系统会默认选中Modules,点击上面的小+号创建模块->New Module 新建一个Java EE Web Applicatio ...

  9. javascript的数组之pop()

    pop()方法从数组中删除最后一个元素,并返回该元素的值.此方法更改数组的长度. let a = [1, 2, 3]; a.length; a.pop(); console.log(a); // [1 ...

  10. Pycharm汉化

    简单的汉化方法:    需要把以下链接的文件放到Pycharm安装目录的lib目录下记得重启pycharm即可https://pan.baidu.com/s/1GG4zXZ_0xSB-AlDdu0Tu ...