• 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 == '
    ')
                    {
                        autoNewLine = false;
                        continue;
                    }
                    else if (c == '
    ')
                    {
                        if (autoNewLine)
                        {
                            if(index < 19)
                            {
                                continue;
                            }
                        }else
                        {
                            sb.Append("
    ");
                            index = 0;
                            rowcont++;
                            autoNewLine = true;
                        }
                    }
                    else
                    {
                        
                        if (index > 19)
                        {
                            sb.Append('
    ');
                            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>
    
  • 相关阅读:
    c#新语法
    关于js 原生原生链
    网站置灰
    ie时间格式NAN-NAN-NAN
    关于vue+element对ie9的兼容el-upload不支持在IE9上传
    屏幕录制+网页页面截图
    javascript -数组常用方法
    javascript -字符串常用方法
    JavaScript 实用技巧
    vue兼容ie
  • 原文地址:https://www.cnblogs.com/keyiei/p/7446440.html
Copyright © 2020-2023  润新知