Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have started develop for UNIX-system and have used MonoDevelop/Gtk#. That's why I have some trouble now. I want to create window with textbox and button's(numeric keypad) in which user's can input personal number to textbox by button's or keyboard. I use Gtk.Entry as textbox. This is a simple task, but when user click to button's my Entry is lost focus and user cann't use keyboard for input next numeric's, that's why I manually set focus using GrabFocus() method. My code look like that:
C#
//code by gtk-gui
public partial class MainWindow
{
	private global::Gtk.Table table5;
	private global::Gtk.Entry personalNumber_entry;
	private global::Gtk.Table table6;
	private global::Gtk.Button eight_button;
	private global::Gtk.Label label8;
	private global::Gtk.Button five_button;
	private global::Gtk.Label label5;
	private global::Gtk.Button four_button;
	private global::Gtk.Label label4;
	private global::Gtk.Button nine_button;
	private global::Gtk.Label label9;
	private global::Gtk.Button one_button;
	private global::Gtk.Label label1;
	private global::Gtk.Button seven_button;
	private global::Gtk.Label label7;
	private global::Gtk.Button six_button;
	private global::Gtk.Label label6;
	private global::Gtk.Button three_button;
	private global::Gtk.Label label3;
	private global::Gtk.Button two_button;
	private global::Gtk.Label label2;
	private global::Gtk.Button zero_button;
	private global::Gtk.Label label10;
	
	protected virtual void Build ()
	{
		global::Stetic.Gui.Initialize (this);
		// Widget MainWindow
		this.Name = "MainWindow";
		this.Title = global::Mono.Unix.Catalog.GetString ("MainWindow");
		this.WindowPosition = ((global::Gtk.WindowPosition)(4));
		this.FocusOnMap = false;
		// Container child MainWindow.Gtk.Container+ContainerChild
		this.table5 = new global::Gtk.Table (((uint)(3)), ((uint)(3)), false);
		this.table5.Name = "table5";
		this.table5.RowSpacing = ((uint)(6));
		this.table5.ColumnSpacing = ((uint)(6));
		// Container child table5.Gtk.Table+TableChild
		this.personalNumber_entry = new global::Gtk.Entry ();
		this.personalNumber_entry.CanDefault = true;
		this.personalNumber_entry.CanFocus = true;
		this.personalNumber_entry.Name = "personalNumber_entry";
		this.personalNumber_entry.IsEditable = true;
		this.personalNumber_entry.ActivatesDefault = true;
		this.personalNumber_entry.InvisibleChar = '•';
		this.table5.Add (this.personalNumber_entry);
		global::Gtk.Table.TableChild w1 = ((global::Gtk.Table.TableChild)(this.table5 [this.personalNumber_entry]));
		w1.LeftAttach = ((uint)(1));
		w1.RightAttach = ((uint)(2));
		w1.XOptions = ((global::Gtk.AttachOptions)(4));
		w1.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table5.Gtk.Table+TableChild
		this.table6 = new global::Gtk.Table (((uint)(4)), ((uint)(3)), false);
		this.table6.Name = "table6";
		this.table6.RowSpacing = ((uint)(6));
		this.table6.ColumnSpacing = ((uint)(6));
		// Container child table6.Gtk.Table+TableChild
		this.eight_button = new global::Gtk.Button ();
		this.eight_button.Name = "eight_button";
		// Container child eight_button.Gtk.Container+ContainerChild
		this.label8 = new global::Gtk.Label ();
		this.label8.Name = "label8";
		this.label8.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>8</span>");
		this.label8.UseMarkup = true;
		this.eight_button.Add (this.label8);
		this.eight_button.Label = null;
		this.table6.Add (this.eight_button);
		global::Gtk.Table.TableChild w3 = ((global::Gtk.Table.TableChild)(this.table6 [this.eight_button]));
		w3.TopAttach = ((uint)(2));
		w3.BottomAttach = ((uint)(3));
		w3.LeftAttach = ((uint)(1));
		w3.RightAttach = ((uint)(2));
		w3.XOptions = ((global::Gtk.AttachOptions)(4));
		w3.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.five_button = new global::Gtk.Button ();
		this.five_button.Name = "five_button";
		// Container child five_button.Gtk.Container+ContainerChild
		this.label5 = new global::Gtk.Label ();
		this.label5.Name = "label5";
		this.label5.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>5</span>");
		this.label5.UseMarkup = true;
		this.five_button.Add (this.label5);
		this.five_button.Label = null;
		this.table6.Add (this.five_button);
		global::Gtk.Table.TableChild w5 = ((global::Gtk.Table.TableChild)(this.table6 [this.five_button]));
		w5.TopAttach = ((uint)(1));
		w5.BottomAttach = ((uint)(2));
		w5.LeftAttach = ((uint)(1));
		w5.RightAttach = ((uint)(2));
		w5.XOptions = ((global::Gtk.AttachOptions)(4));
		w5.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.four_button = new global::Gtk.Button ();
		this.four_button.Name = "four_button";
		// Container child four_button.Gtk.Container+ContainerChild
		this.label4 = new global::Gtk.Label ();
		this.label4.Name = "label4";
		this.label4.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>4</span>");
		this.label4.UseMarkup = true;
		this.four_button.Add (this.label4);
		this.four_button.Label = null;
		this.table6.Add (this.four_button);
		global::Gtk.Table.TableChild w7 = ((global::Gtk.Table.TableChild)(this.table6 [this.four_button]));
		w7.TopAttach = ((uint)(1));
		w7.BottomAttach = ((uint)(2));
		w7.XOptions = ((global::Gtk.AttachOptions)(4));
		w7.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.nine_button = new global::Gtk.Button ();
		this.nine_button.Name = "nine_button";
		// Container child nine_button.Gtk.Container+ContainerChild
		this.label9 = new global::Gtk.Label ();
		this.label9.Name = "label9";
		this.label9.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>9</span>");
		this.label9.UseMarkup = true;
		this.nine_button.Add (this.label9);
		this.nine_button.Label = null;
		this.table6.Add (this.nine_button);
		global::Gtk.Table.TableChild w9 = ((global::Gtk.Table.TableChild)(this.table6 [this.nine_button]));
		w9.TopAttach = ((uint)(2));
		w9.BottomAttach = ((uint)(3));
		w9.LeftAttach = ((uint)(2));
		w9.RightAttach = ((uint)(3));
		w9.XOptions = ((global::Gtk.AttachOptions)(4));
		w9.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.one_button = new global::Gtk.Button ();
		this.one_button.Name = "one_button";
		// Container child one_button.Gtk.Container+ContainerChild
		this.label1 = new global::Gtk.Label ();
		this.label1.Name = "label1";
		this.label1.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>1</span>");
		this.label1.UseMarkup = true;
		this.one_button.Add (this.label1);
		this.one_button.Label = null;
		this.table6.Add (this.one_button);
		global::Gtk.Table.TableChild w11 = ((global::Gtk.Table.TableChild)(this.table6 [this.one_button]));
		w11.XOptions = ((global::Gtk.AttachOptions)(4));
		w11.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.seven_button = new global::Gtk.Button ();
		this.seven_button.Name = "seven_button";
		// Container child seven_button.Gtk.Container+ContainerChild
		this.label7 = new global::Gtk.Label ();
		this.label7.Name = "label7";
		this.label7.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>7</span>");
		this.label7.UseMarkup = true;
		this.seven_button.Add (this.label7);
		this.seven_button.Label = null;
		this.table6.Add (this.seven_button);
		global::Gtk.Table.TableChild w13 = ((global::Gtk.Table.TableChild)(this.table6 [this.seven_button]));
		w13.TopAttach = ((uint)(2));
		w13.BottomAttach = ((uint)(3));
		w13.XOptions = ((global::Gtk.AttachOptions)(4));
		w13.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.six_button = new global::Gtk.Button ();
		this.six_button.Name = "six_button";
		// Container child six_button.Gtk.Container+ContainerChild
		this.label6 = new global::Gtk.Label ();
		this.label6.Name = "label6";
		this.label6.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>6</span>");
		this.label6.UseMarkup = true;
		this.six_button.Add (this.label6);
		this.six_button.Label = null;
		this.table6.Add (this.six_button);
		global::Gtk.Table.TableChild w15 = ((global::Gtk.Table.TableChild)(this.table6 [this.six_button]));
		w15.TopAttach = ((uint)(1));
		w15.BottomAttach = ((uint)(2));
		w15.LeftAttach = ((uint)(2));
		w15.RightAttach = ((uint)(3));
		w15.XOptions = ((global::Gtk.AttachOptions)(4));
		w15.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.three_button = new global::Gtk.Button ();
		this.three_button.Name = "three_button";
		// Container child three_button.Gtk.Container+ContainerChild
		this.label3 = new global::Gtk.Label ();
		this.label3.Name = "label3";
		this.label3.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>3</span>");
		this.label3.UseMarkup = true;
		this.three_button.Add (this.label3);
		this.three_button.Label = null;
		this.table6.Add (this.three_button);
		global::Gtk.Table.TableChild w17 = ((global::Gtk.Table.TableChild)(this.table6 [this.three_button]));
		w17.LeftAttach = ((uint)(2));
		w17.RightAttach = ((uint)(3));
		w17.XOptions = ((global::Gtk.AttachOptions)(4));
		w17.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.two_button = new global::Gtk.Button ();
		this.two_button.Name = "two_button";
		// Container child two_button.Gtk.Container+ContainerChild
		this.label2 = new global::Gtk.Label ();
		this.label2.Name = "label2";
		this.label2.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>2</span>");
		this.label2.UseMarkup = true;
		this.two_button.Add (this.label2);
		this.two_button.Label = null;
		this.table6.Add (this.two_button);
		global::Gtk.Table.TableChild w19 = ((global::Gtk.Table.TableChild)(this.table6 [this.two_button]));
		w19.LeftAttach = ((uint)(1));
		w19.RightAttach = ((uint)(2));
		w19.XOptions = ((global::Gtk.AttachOptions)(4));
		w19.YOptions = ((global::Gtk.AttachOptions)(4));
		// Container child table6.Gtk.Table+TableChild
		this.zero_button = new global::Gtk.Button ();
		this.zero_button.CanFocus = true;
		this.zero_button.Name = "zero_button";
		// Container child zero_button.Gtk.Container+ContainerChild
		this.label10 = new global::Gtk.Label ();
		this.label10.Name = "label10";
		this.label10.LabelProp = global::Mono.Unix.Catalog.GetString ("<span size='20000'>0</span>");
		this.label10.UseMarkup = true;
		this.zero_button.Add (this.label10);
		this.zero_button.Label = null;
		this.table6.Add (this.zero_button);
		global::Gtk.Table.TableChild w21 = ((global::Gtk.Table.TableChild)(this.table6 [this.zero_button]));
		w21.TopAttach = ((uint)(3));
		w21.BottomAttach = ((uint)(4));
		w21.LeftAttach = ((uint)(1));
		w21.RightAttach = ((uint)(2));
		w21.XOptions = ((global::Gtk.AttachOptions)(4));
		w21.YOptions = ((global::Gtk.AttachOptions)(4));
		this.table5.Add (this.table6);
		global::Gtk.Table.TableChild w22 = ((global::Gtk.Table.TableChild)(this.table5 [this.table6]));
		w22.TopAttach = ((uint)(1));
		w22.BottomAttach = ((uint)(2));
		w22.LeftAttach = ((uint)(1));
		w22.RightAttach = ((uint)(2));
		w22.XOptions = ((global::Gtk.AttachOptions)(1));
		w22.YOptions = ((global::Gtk.AttachOptions)(1));
		this.Add (this.table5);
		if ((this.Child != null)) {
			this.Child.ShowAll ();
		}
		this.DefaultWidth = 828;
		this.DefaultHeight = 342;
		this.personalNumber_entry.HasDefault = true;
		this.Show ();
		this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
		this.two_button.Clicked += new global::System.EventHandler (this.OnTwoButton_Click);
		this.three_button.Clicked += new global::System.EventHandler (this.OnThreeButton_Click);
		this.one_button.Clicked += new global::System.EventHandler (this.OnOneButton_Click);
	}
}


C#
//MainWindow module
public partial class MainWindow: Gtk.Window
{	
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
    private void OnOneButton_Click (object sender, EventArgs e)
	{
		personalNumber_entry.Text= personalNumber_entry.Text.Insert(personalNumber_entry.CursorPosition, "1");
		
		personalNumber_entry.GrabFocus ();
    }
	
	private void OnTwoButton_Click (object sender, EventArgs e)
	{
		personalNumber_entry.Text = personalNumber_entry.Text.Insert (personalNumber_entry.CursorPosition, "2");
		
		personalNumber_entry.GrabFocus ();
    }
	
	private void OnThreeButton_Click (object sender, EventArgs e)
	{
		personalNumber_entry.Text = personalNumber_entry.Text.Insert (personalNumber_entry.CursorPosition, "3");
		
		personalNumber_entry.GrabFocus ();
    }				
}

But when focus sets to personal Number_entry all including text is selecting! How can I freeze a focus to Entry object and how can I unselect text in Entry?
And can you tell me, how can I set fontsize for Entry?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900