Finish Words In Textbox

Does anybody have any idea on how to make it so when you type in a textbox I could have an array of words and it will complete the word if its in the list? I am not sure what it is called but any ideas?

Well if you use .NET controls, it would be binding data to the textbox.

How do you do that? look here it’s for a combo box but it,s the same thing or close to it for a textbox and you can here for an example for a textbox.

hope this helps.

Language my friend… Assuming you are using csharp.

Rather than use an array you can use a dictionary which already has built in methods to allow you to limit and return the keys fitting what is being typed.

Dictionary is also handy as it lends itself nicely to ingame consoles because you can set the key = the keyword you want to use and the value = to the function will call with it.

Anywho i hope this shot in the dark answered your question if you give more context like language you are using or application i can be more helpful

Read more on dictionaries Dictionary<TKey,TValue> Class (System.Collections.Generic) | Microsoft Learn

I suppose you’re talking about GUI.TextField and not Windows Forms Textbox… right?

Here is a quick easy way you can use as start (it automatically completes the word if found, when you type a new character and the new string length is greater than the previous, so it will not autocomplete when you delete chars or use cursor arrows):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test : MonoBehaviour {
	
	List<string> words = new List<string>();
	string myText = "";
	
	void Start () {
		words.Add("testString");
		words.Add("otherString");
	}
	
	void OnGUI () {
		string oldString = myText;
		myText = GUI.TextField(new Rect(10, 10, 200, 20), myText);
		if (!string.IsNullOrEmpty(myText)  myText.Length > oldString.Length) {
			List<string> found = words.FindAll( w => w.StartsWith(myText) );
			if (found.Count > 0)
				myText = found[0];
		}
	}
}

PS: the autocomplete search is case-sensitive here, to make it case-insensitive just add “System.StringComparison.CurrentCultureIgnoreCase” as second parameter to “StartsWith”.