Create a new Entry widget with the following function.
Entry entry1 = new Entry();The next function alters the text which is currently within the Entry widget.
entry1.Text = string text;The property Text sets the contents of the Entry widget, replacing the current contents. Note that the class Entry implements the Editable interface which contains some more functions for manipulating the contents.
The contents of the Entry can be retrieved by retrieving the value of the following property. This is useful in the events described below.
string text = entry1.Text;
If we don't want the contents of the Entry to be changed by someone typing into it, we can change its editable state.
entry1.Editable = bool editable;The function above allows us to toggle the editable state of the Entry widget by passing in a "true" or "false" value for the editable argument.
If we are using the Entry where we don't want the text entered to be visible, for example when a password is being entered, we can use the following function, which also takes a boolean flag.
entry1.Visibility = bool visible;A region of the text may be set as selected by using the following function. This would most often be used after setting some default text in an Entry, making it easy for the user to remove it.
entry1.SelectRegion(int start, int end );If we want to catch when the user has entered text, we can connect to the activate or changed event. Activate is raised when the user hits the enter key within the Entry widget. Changed is raised when the text changes at all, e.g., for every character entered or removed.
The following code is an example of using an Entry widget.
// MyTextEntry.cs - GTK# Tutorial example // // Authors: Andrés Sáyago // asath@ColombiaLinux.org // // (C) 2003 Andrés Sáyago // Colombia // namespace GtkSharpTutorial { using Gtk; using GtkSharp; using System; public class textEntrySample { // The entry_toggle_* functions use it static Entry entry; static void enter_callback(object obj, EventArgs args) { string entry_text = ((Entry) obj).Text; Console.WriteLine("Entry contents: " + entry_text); } static void entry_toggle_editable(object obj, EventArgs args) { entry.Editable = ((CheckButton) obj).Active; } static void entry_toggle_visibility(object obj, EventArgs args) { entry.Visible = ((CheckButton) obj).Active; } static void window_destroy(object obj, DeleteEventArgs args) { Application.Quit(); } static void button_close(object obj, EventArgs args) { Application.Quit(); } // Function for the Widgets building static void create_text_entry() { Window window; VBox vbox; HBox hbox; Button button; CheckButton check; int tmp_pos; // Create a new window window = new Window (WindowType.Toplevel); window.SetDefaultSize(200, 100); window.Title = "GTK Entry"; window.DeleteEvent += new DeleteEventHandler(window_destroy); vbox = new VBox(false, 0); window.Add(vbox); vbox.Show(); // Box to enter text entry = new Entry(); entry.MaxLength = 50; // When the Enter key is pressed, print a message to the console entry.Activated += new EventHandler(enter_callback); entry.Text = "hello"; tmp_pos = entry.Text.Length; // Demostration of the 'InsertText' function (-1 is at end) entry.InsertText(" world", -1, out tmp_pos); // Text selected entry.SelectRegion(0, entry.Text.Length); vbox.PackStart(entry, true, true, 0); entry.Show(); hbox = new HBox(false, 0); vbox.Add(hbox); hbox.Show(); check = new CheckButton("Editable"); hbox.PackStart(check, true, true, 0); check.Toggled += new EventHandler(entry_toggle_editable); check.Active = true; check.Show(); check = new CheckButton("Visible"); hbox.PackStart(check, true, true, 0); check.Toggled += new EventHandler(entry_toggle_visibility); check.Active = true; check.Show(); button = Button.NewFromStock(Gtk.Stock.Close); button.Clicked += new EventHandler(button_close); vbox.PackStart(button, true, true, 0); button.CanDefault = true; button.GrabDefault(); button.Show(); window.Show(); } // Main function public static void Main(string[] args) { Application.Init (); create_text_entry(); Application.Run (); } } }