textbox 未
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 未的更多相关文章
- WPF的TextBox的焦点获取与失去焦点的死循环解决方案
在WPF中实现一个弹出层自动获取焦点,弹出层实现是通过其UserControl的依赖属性Visibility的绑定实现的,让UserControl上的TextBox获取焦点,初始实现代码如下: pub ...
- ApplicationCommands 应用程序常见命令
ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c 在WPF中,许多控件都自动集成了固有的命令集.比如文本框TextBox就提供了复制(Copy),粘贴 ...
- Windows Presentation Foundation (WPF)中的命令(Commands)简述
原文:Windows Presentation Foundation (WPF)中的命令(Commands)简述 ------------------------------------------- ...
- ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c
在WPF中,许多控件都自动集成了固有的命令集.比如文本框TextBox就提供了复制(Copy),粘贴(Paste),裁切(Cut),撤消(Undo)和重做(Redo)命令等. WPF提供常用应用程序所 ...
- C#用户自定义控件(含源代码)-透明文本框
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...
- [转]WPF命令集 Command
在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制.粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节 ...
- Ie浏览器TextBox文本未居中
Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中 vertical-align:middle
- 【MVVM DEV】DataColumn中的TextBox与ComboBox的并存
一.前言 在WPF编程中,有时候我们使用DataGrid会需要在一个DataColumn中既有TextBox,也要有ComboBox或者TextBlock等其他数据显示样式. 这个时候我们 ...
- ASP.NET 给作为隐藏域的TextBox赋值之后提交表单,无响应?
操作步骤: 给页面隐藏TextBox赋值,然后触发ASP.NET change事件,调用ASP.NET后台方法,调用后执行客户端脚本this.RegisterClientScriptBlock(Dat ...
随机推荐
- VScode查找替换常用正则表达式
1.从字符串开始到结束,例如:<a href="#">测试</a> (<'.*?>) //匹配到整个a标签 (>'.*?<) //匹 ...
- UVA 536 Tree Recovery 建树+不建树
题意: 给出先序和中序,求后序. 思路: ①建树然后递归输出. //建树的 #include<iostream> #include<cstdio> #include<qu ...
- Django与ajax、分页器
ajax简单数据响应 ajax请求,后台只需要返回信息,所以不会出现render.redirect 模板层: $('.btn').click(function() { $.ajax({ url: '/ ...
- SSM框架主要几个注解的位置
@Controller @Service @Repository @Component Controller (控制层) Service (业务层) daoImpl (实现层) 模糊注解 @Autow ...
- Elasticsearch学习笔记二
PS:上一篇已经介绍了ES的一些基础概念以及单机版ES的安装,配置,本文主要介绍ES的集群管理,CRUD以及简单聚合查询. 集群管理 ES的集群部署起来也很方便,将单机版SCP复制几分,修改elast ...
- Git Sever搭建与相关错误处理
搭建 安装git: sudo apt-get install git 创建一个git用户,用来运行git服务:(用自己的用户也可以,其实) sudo adduser git 创建证书登录: 收集所有需 ...
- python#读csv,excel,json数据
#读csv,excel,json数据 with open('E:\\test\\xdd.csv','r') as f: for line in f.readlines(): print(line) i ...
- Hibernate 单向一对多映射
单向 n-1: 单向 n-1 关联只需从 n 的一段访问 1 的一端 此处 Order 类和 Customer 类,其中 Order 类需要引用 Customer 类 代码: public class ...
- 最大子矩阵和问题dp
给定一个矩阵 matrix,其中矩阵中的元素可以包含正数.负数.和0,返回子矩阵的最大累加和.例如,矩阵 matrix 为: 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 - ...
- Snackbar 提醒
类似于Toast,属于design包,不要忘记导入design package com.xiaozhuyisheng.jinjiedemos.activity; import android.supp ...