Do not use on any
shared computer
August 29, 2008 08:06pm pdt
 
[x]
Attachment Details

C# Textbox focus at startup

I am just trying to get the bottom textbox to get focus and select text so the user knows where to begin.  For some reason nothing i've tried selects the text and i've gone through several solutions that other people have found work, to no avail.

Thanks in advance.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
using System;
using System.Drawing;
using System.Windows.Forms;
 
public delegate void onStartDelegate();
 
class GUI : Form
{
    public TextBox myBox = new TextBox();
    public RichTextBox myTextArea = new RichTextBox();
    public event onStartDelegate onGUIStart;
 
    public GUI()
    {
        /* Define form layout */
        this.Height = 500;
        this.Width = 600;
        this.Text = "Bills Program Alpha 1.0";
        this.MaximizeBox = false;
        this.CenterToScreen();
        this.MaximumSize = new Size(600, 500);
        this.MinimumSize = new Size(600, 500);
        this.BackColor = Color.DarkGoldenrod;
 
        /* Create textbox */
        myBox.Parent = this;
        myBox.Size = new Size(ClientSize.Width + 5, 10);
        myBox.Location = new Point(-2, (ClientSize.Height - myBox.Height));
        myBox.BackColor = Color.Black;
        myBox.ForeColor = Color.LightGreen;
        myBox.Font = new Font("Courier New", 12);
        myBox.MaxLength = 30;
        myBox.TabStop = true;
        myBox.TabIndex = 0;
        myBox.Text = "Enter commands here";
        
        /* Create text area */
        myTextArea.Parent = this;
        myTextArea.Size = new Size(ClientSize.Width + 2, (ClientSize.Height - myBox.Height) + 4);
        myTextArea.Multiline = true;
        myTextArea.BackColor = Color.Black;
        myTextArea.ForeColor = Color.LightGreen;
        myTextArea.ReadOnly = true;
        myTextArea.Font = new Font("Verdana", 15);
        myTextArea.Text = "this is some testing text";
        myTextArea.ScrollBars = RichTextBoxScrollBars.Horizontal;
        myTextArea.TabStop = false;
 
        onGUIStart += new onStartDelegate(focusText);
 
        onGUIStart();
 
    }
 
    public void focusText()
    {
        myBox.Focus();
        myBox.Select();
    }
    
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new GUI());
    }
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: Nitinol
Solution Provided By: topdog770
Participating Experts: 2
Solution Grade: A
Views: 241
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by topdog770

Rank: Guru

Accepted Solution by topdog770:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Nitinol
Author Comment by Nitinol:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by topdog770

Rank: Guru

Expert Comment by topdog770:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by xjpmauricio
In my experience with microsoft ajax extensions i've noticed that many things do not work as they supposed to so i end up using some "hacks" to make things work. I've ran into this "focus issue" in the past and what i done to solve it was to use direct javascript to set the focus on some object:

idOfObject.focus();
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628