G
G
gerkenGN2020-05-20 15:09:21
C++ / C#
gerkenGN, 2020-05-20 15:09:21

How to add autocomplete option to custom textbox?

Good time!
Tell me please, I wrote my textbox because the standard one is ugly, but I needed to add the autocomple function and I don’t even understand how to connect it. For now, here is the code. well, there is further design and that's it

public class textBoxRound : Control 
    {
        #region --переменные--

        StringFormat SF = new StringFormat();
        int TopBorderOffset = 0;
        TextBox tbInput = new TextBox();

        #endregion
        #region --свойства--
        public string TextPreview { get; set; } = "Input text";
        public Font FontTextPreview { get; set; } = new Font("Arial", 24, FontStyle.Bold);
        public Color BorderColor { get; set; } = Color.Blue;
        public Color BorderColorNotActive { get; set; } = Color.DarkGray;

        public string TextInput
        {
            get => tbInput.Text;
            set => tbInput.Text = value;
        }

        [Browsable(false)]
        public new string Text { get; set; }
        #endregion
        
        
        public textBoxRound()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
            DoubleBuffered = true;
            Size = new Size(850, 120);
            Font = new Font("Arial", 33.75F, FontStyle.Regular);
            ForeColor = Color.Black;
            BackColor = Color.White;
            SF.Alignment = StringAlignment.Center;
            SF.LineAlignment = StringAlignment.Center;

            AdjustTextBoxInput();
            this.Controls.Add(tbInput);
        }
        private void AdjustTextBoxInput()
        {
            TextBox tb = new TextBox();
            tbInput.Name = "InputBox";
            tbInput.BorderStyle = BorderStyle.None;
            tbInput.BackColor = BackColor;
            tbInput.ForeColor = ForeColor;
            tbInput.Font = Font;
            int offset = TextRenderer.MeasureText(TextPreview, FontTextPreview).Height / 2;

            tbInput.Location = new Point(5, Height / 2 - offset);
            tbInput.Size = new Size(Width - 10, tb.Height);

        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-05-20
@gerkenGN

public class AutoCompleteTextBox : TextBoxRound
    {
        public AutoCompleteTextBox()
        {
            var allowedStatorTypes = new AutoCompleteStringCollection();
            // вставляете свои строки, дополняете конечно логикой для получения тут данных например из базы
            var allstate = new[] { "Test", "nTest" }.OrderBy(x => x).Select(x => x).Distinct().ToList();


            foreach (var item in allstate)
            {
                allowedStatorTypes.Add(item);
            }


            tbInput.AutoCompleteMode = AutoCompleteMode.Suggest;
            tbInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tbInput.AutoCompleteCustomSource = allowedStatorTypes;
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question