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>

 

posted @ 2017-08-29 00:38  keyiei  阅读(134)  评论(0编辑  收藏  举报