locked
How can i make the lines to move smooth to the right when padding the lines with numbers ? RRS feed

  • Question

  • First i recorded a very short video clip showing the problems. Here is a link:

    padding numbers

    The problems happen when i'm using the numericupdown to change the padding numbers by changing the numericupdown value.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Reflection;
    
    namespace Richtbox
    {
        public partial class Form1 : Form
        {
            private ContextMenu contextMenu;
            private List<string> oldlines = new List<string>();
            private List<string> padnumbers = new List<string>();
    
            public Form1()
            {
                InitializeComponent();
                
                richTextBox2.TabStop = false;
                richTextBox1.TabStop = false;
                string text = File.ReadAllText(@"C:\Users\Chocolade\Documents\new 2.txt");
                richTextBox1.Text = text;
                string[] lines = richTextBox1.Lines;
                richTextBox2.Lines = lines;
                oldlines = richTextBox1.Lines.ToList();
                AddNumbersToLines(richTextBox1, (int)numericUpDown1.Value);
                RichTextboxColorFirstLetter(richTextBox1, Color.Red);
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                contextMenu = new System.Windows.Forms.ContextMenu();
                MenuItem menuItem = new MenuItem("Cut");
                menuItem.Click += new EventHandler(CutAction);
                contextMenu.MenuItems.Add(menuItem);
                menuItem = new MenuItem("Copy");
                menuItem.Click += new EventHandler(CopyAction);
                contextMenu.MenuItems.Add(menuItem);
                menuItem = new MenuItem("Paste");
                menuItem.Click += new EventHandler(PasteAction);
                contextMenu.MenuItems.Add(menuItem);
                richTextBox1.ContextMenu = contextMenu;
    
                for (int i = 0; i < contextMenu.MenuItems.Count; i++)
                {
                    contextMenu.MenuItems[i].Enabled = false;
                }
            }
            
            private void AddNumbersToLines(RichTextBox Rchtxt, int PadValue)
            {
                List<string> lines = new List<string>();
                padnumbers = new List<string>();
                lines = richTextBox1.Lines.ToList();
    
                for (int i = 0; i < lines.Count; i++)
                {
                    string padnumber = (i + 1).ToString("D" + PadValue);
                    padnumbers.Add(padnumber);
    
                    lines[i] = padnumber + " " + oldlines[i];
                }
                Rchtxt.Lines = lines.ToArray();
            }
    
            void RichtextboxChangeLine(RichTextBox RTB, int line, string text)
            {
                int s1 = RTB.GetFirstCharIndexFromLine(line);
                int s2 = line < RTB.Lines.Count() - 1 ?
                          RTB.GetFirstCharIndexFromLine(line + 1) - 1 :
                          RTB.Text.Length;
                RTB.Select(s1, s2 - s1);
                RTB.SelectedText = text;
            }
    
            private void RichTextboxColorFirstLetter(RichTextBox Rchtxt, Color color)
            {
                if (padnumbers.Count > 0)
                {
                    for (int i = 0; i < padnumbers.Count; i++)
                    {
                        string text = Rchtxt.Lines[i].Substring(0, padnumbers[i].Length);
                        Rchtxt.Select(Rchtxt.GetFirstCharIndexFromLine(i), text.Length);
                        Rchtxt.SelectionColor = color;
                    }
                }
            }
    
            private void CheckKeyword(RichTextBox Rchtxt, string word, Color color, int startIndex)
            {
                if (Rchtxt.Text.Contains(word))
                {
                    int index = -1;
                    int selectStart = Rchtxt.SelectionStart;
    
                    while ((index = Rchtxt.Text.IndexOf(word, (index + 1))) != -1)
                    {
                        Rchtxt.Select((index + startIndex), word.Length);
                        Rchtxt.SelectionColor = color;
                        Rchtxt.Select(selectStart, 0);
                        Rchtxt.SelectionColor = Color.Black;
                    }
                }
            }
    
            private void richTextBox2_TextChanged(object sender, EventArgs e)
            {
                richTextBox2.SelectionStart = 0;
                richTextBox2.ScrollToCaret();
            }
    
            void CutAction(object sender, EventArgs e)
            {
                richTextBox1.Cut();
            }
    
            void CopyAction(object sender, EventArgs e)
            {
                Clipboard.SetText(richTextBox1.SelectedText);
            }
    
            void PasteAction(object sender, EventArgs e)
            {
                if (Clipboard.ContainsText())
                {
                    richTextBox1.Text
                        += Clipboard.GetText(TextDataFormat.Text).ToString();
                }
            }
    
            private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
            {
                int positionToSearch = richTextBox1.GetCharIndexFromPosition(e.Location);
                bool isInSelectedText = positionToSearch >= richTextBox1.SelectionStart && positionToSearch < richTextBox1.SelectionStart + richTextBox1.SelectionLength;
                for (int i = 0; i < contextMenu.MenuItems.Count; i++)
                {
                    if (richTextBox1.SelectedText != "" &&
                        isInSelectedText == true)
                    {
                        contextMenu.MenuItems[i].Enabled = true;
                    }
                    else
                    {
                        contextMenu.MenuItems[i].Enabled = false;
                    }
                }
            }
    
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                AddNumbersToLines(richTextBox1, (int)numericUpDown1.Value);
    
                richTextBox1.SelectAll();
                richTextBox1.SelectionColor = Color.Black;
                RichTextboxColorFirstLetter(richTextBox1, Color.Red);
            }
        }
    }


    The first problem is when i change the value in the numericupdown event the whole lines are not moving so smooth.

    The lines seems to be a bit flickering.

    The second problem is that are some lines for example line 13 already have spaces from the start so when it's adding more and more padding numbers it's waiting and then push the line to the right and push it with the others keeping the line original spaces.

    In the video it's line number 13: 

    GenerateRoad(points, null);

    you can see what is happening when i change the numericupdown value with this line.

    And last problem is when i keep pressing the numericupdown arrows up or down not clicking each time once but keep pressing on it the padding action start slow and then it's getting faster and faster....like a car start driving slow and then get faster.


    Friday, July 14, 2017 9:36 PM

Answers

  • Hi Chocolade1972,

    I tested the code you provided, but I can not reproduce your issue, and the video link direct to this thread itself.

    >>The first problem is when i change the value in the numericupdown event ...

    How many lines do you have in your .txt file? I test the code but it does not flicker, and I am also not sure what's the meaning of your flicker.

    >>The second problem is that are some lines for example line 13 already ...

    So what do you want to do with the line 13?

    >>And last problem is when i keep pressing the numericupdown arrows ...

    I think it is some of the reasons for the windows system.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Monday, July 17, 2017 3:10 AM
    • Marked as answer by Chocolade1972 Friday, July 28, 2017 2:13 PM
    Monday, July 17, 2017 3:10 AM