zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    论文阅读:Single Image Dehazing via Conditional Generative Adversarial Network
    lintcode-720重排带整数字符串
    lintcode-828. 字模式
    lintcode-1038. 珠宝和石头
    lintcode-1174.下一个更大的元素 III
    lintcode-80.中位数
    《大道至简》第二章读后感
    从命令行输出数字,求和计算
    《大道至简》第一章伪代码观后感
    ngx_http_referer_module模块说明
  • 原文地址:https://www.cnblogs.com/keyiei/p/7446440.html
Copyright © 2011-2022 走看看