Create a flicker-free text field that can be quickly updated

I am trying to create a search tool and want to display the results in a text box, just like in Visual Studio - this means that for long results, the search will be added to the end of the text box while the user is trying to view the results at the top of the text box.

I am currently using a standard text box, however it has a lot of problems:

  • The text box flickers insanely while searching.
  • It is not possible to scroll through a user while results are added to the field.
  • It is not possible for the user to copy and paste the results into the text box while the results are being added.

Is there a way around these issues, or should I consider using another control / creating mine?

+3
source share
3 answers

Microsoft forgot to implement Begin / EndUpdate () methods for TextBox. You can add them yourself, this solves the problem. However, you cannot get rid of the flicker. Code example:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            timer1.Interval = 10;
            timer1.Tick += new EventHandler(timer1_Tick);
            button1.Click += new EventHandler(button1_Click);
        }
        void timer1_Tick(object sender, EventArgs e) {
            int pos = textBox1.SelectionStart;
            int len = textBox1.SelectionLength;
            SendMessage(textBox1.Handle, 11, IntPtr.Zero, IntPtr.Zero);
            textBox1.AppendText(DateTime.Now.ToString() + Environment.NewLine);
            SendMessage(textBox1.Handle, 11, (IntPtr)1, IntPtr.Zero);
            //if (textBox1 is RichTextBox) textBox1.Invalidate();
            textBox1.SelectionStart = pos;
            textBox1.SelectionLength = len;
        }
        private void button1_Click(object sender, EventArgs e) {
            timer1.Enabled = !timer1.Enabled;
        }
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    }
}
+2
source

First, in order to access your TextBox during your search, you must put your search in BackgroundWorkerand place the (intermediate) results with BeginInvoke()in your TextBox. Thus, your GUI does not freeze during the search.

TextBox.AppendText(), . , AppendText:

textBoxMessages.SelectionStart = textBoxMessages.Text.Length;
textBoxMessages.ScrollToCaret();

/ , , AppendText , SelectionStart Text.Lenght, , AppendText

Update

, , . , RichTextBox, .

, : ScintillaNET. .

, ScintillaNet ( Scintilla), - . , .

+3

Text fields are good for editable content - are search results available?

If you are showing search results, why not use a DataGrid? You can create it to look as you want (it can look like a text box with lines and lines of text)

+1
source

Source: https://habr.com/ru/post/1728636/


All Articles