Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# how to let textbox always keep in autocomplete?(Windows Form)

C# how to let textbox always keep in autocomplete?(Windows Form)

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akira32
    wrote on last edited by
    #1

    I use below code to reach the autocomplete in a textbox. But the chance is once, it cannot keep in autocomplete status. When I finish an autocomplete work, and then I cannot key in another autocomplete word. Does somebody know how to let textbox always keep in autocomplete?

    private void Form1_Load(object sender, EventArgs e)
    {
    // Create the list to use as the custom source.
    var source = new AutoCompleteStringCollection();
    source.AddRange(new string[]
    {
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
    });

    		// Create and initialize the text box.
    		var textBox = new TextBox
    		{
    			AutoCompleteCustomSource = source,
    			AutoCompleteMode =
    				AutoCompleteMode.SuggestAppend,
    			AutoCompleteSource =
    				AutoCompleteSource.CustomSource,
    			Location = new Point(20, 20),
    			Width = ClientRectangle.Width - 40,
    			Visible = true
    		};
    
    		// Add the text box to the form.
    		Controls.Add(textBox);
    
    	}
    
    CHill60C 1 Reply Last reply
    0
    • A akira32

      I use below code to reach the autocomplete in a textbox. But the chance is once, it cannot keep in autocomplete status. When I finish an autocomplete work, and then I cannot key in another autocomplete word. Does somebody know how to let textbox always keep in autocomplete?

      private void Form1_Load(object sender, EventArgs e)
      {
      // Create the list to use as the custom source.
      var source = new AutoCompleteStringCollection();
      source.AddRange(new string[]
      {
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
      });

      		// Create and initialize the text box.
      		var textBox = new TextBox
      		{
      			AutoCompleteCustomSource = source,
      			AutoCompleteMode =
      				AutoCompleteMode.SuggestAppend,
      			AutoCompleteSource =
      				AutoCompleteSource.CustomSource,
      			Location = new Point(20, 20),
      			Width = ClientRectangle.Width - 40,
      			Visible = true
      		};
      
      		// Add the text box to the form.
      		Controls.Add(textBox);
      
      	}
      
      CHill60C Offline
      CHill60C Offline
      CHill60
      wrote on last edited by
      #2

      I think your problem is that the text isn't highlighted when you go back into the textbox, so any characters you start to type are appended to what is already there - hence auto-complete does not work. The solution is to highlight all of the text as you enter the textbox: Add this line just before you add textbox to the Controls collection

              textBox.Enter += new System.EventHandler(this.textBox\_GotFocus);
      

      and add this method to the form

      private void textBox_GotFocus(object sender, EventArgs e)
      {
      ((TextBox)sender).SelectAll();
      }

      Unfortunately when you use the mouse to enter the field the caret is positioned wherever the mouse pointer is so the MouseEnter method doesn't work in quite the same way as we'd like. To get over that you can use the MouseClick event:

      textBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox_MouseClick);

      and add this method

      private void textBox_MouseClick(object sender, MouseEventArgs e)
      {
      ((TextBox)sender).SelectAll();
      }

      A 1 Reply Last reply
      0
      • CHill60C CHill60

        I think your problem is that the text isn't highlighted when you go back into the textbox, so any characters you start to type are appended to what is already there - hence auto-complete does not work. The solution is to highlight all of the text as you enter the textbox: Add this line just before you add textbox to the Controls collection

                textBox.Enter += new System.EventHandler(this.textBox\_GotFocus);
        

        and add this method to the form

        private void textBox_GotFocus(object sender, EventArgs e)
        {
        ((TextBox)sender).SelectAll();
        }

        Unfortunately when you use the mouse to enter the field the caret is positioned wherever the mouse pointer is so the MouseEnter method doesn't work in quite the same way as we'd like. To get over that you can use the MouseClick event:

        textBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox_MouseClick);

        and add this method

        private void textBox_MouseClick(object sender, MouseEventArgs e)
        {
        ((TextBox)sender).SelectAll();
        }

        A Offline
        A Offline
        akira32
        wrote on last edited by
        #3

        Hello CHill60! Thanks your answer. But my problem is that I cannot input the second word with Autocomplete list item. For example, when I input "May Apr", it should be disapeear "May April" for me to select "April". Now it just can work in frist word "May".

        CHill60C 1 Reply Last reply
        0
        • A akira32

          Hello CHill60! Thanks your answer. But my problem is that I cannot input the second word with Autocomplete list item. For example, when I input "May Apr", it should be disapeear "May April" for me to select "April". Now it just can work in frist word "May".

          CHill60C Offline
          CHill60C Offline
          CHill60
          wrote on last edited by
          #4

          That's because May April is not part of the autocomplete list - you'll either have to ensure that all combinations are in the autocomplete list, or write a user control to do that - I'll have another look

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups