Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am still trying to lean C# and I am playing with Global Variables or should I say static variables.
I went to this link to learn how to set Focus on a textbox it kind of works.
How to: Set Focus in a TextBox Control - WPF .NET Framework | Microsoft Learn[^]

Here is my code frmStart is first form & frmAlert is shown as a dialog
Not the most elegant Error Trapping I am just testing how things work
The Question Why does textbox.Focus fail ?

What I have tried:

public frmStart()
 {
     InitializeComponent();
     //ActiveControl = tbEV;//This Works
     tbEV.Focus();// This Fails
 }
 private void frmStart_Load(object sender, EventArgs e)
 {
     //tbEV.Text = gv.I.ToString();
     tbEV.Text = gv.gvalertType;
     //ActiveControl = tbEV;//This Works
     tbEV.Focus();// This Fails
 }

 private void btnSD_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(tbEV.Text))
     {
         //tbMessage.Text = "Enter a Integer Value";
         gv.gvalertType = "1";
         frmAlert fA = new frmAlert();
         fA.ShowDialog();
         tbEV.Focus();//This Works
         //ActiveControl = tbEV;//This Works
         //return;
     }

     if (Int32.TryParse(tbEV.Text, out var outParse))// This test that a Integer was entered
     {

     }
     else
     {
         tbMessage.Text = "Enter a Value";
         //ActiveControl = tbEV;
         return;
     }

     doWhat = 1;
     Hide();
     frmShowData fSD = new frmShowData();
     //gv.I = 35;
     gv.I = Int32.Parse(tbEV.Text);
     fSD.Show();
 }


This is what I am playing with not relavent to the question just FYI
static class gv
{
    public static int I;
    public static string gvalertType;
}
Posted
Comments
Jo_vb.net 29-Nov-23 14:08pm    
You write // This fails
Do you get an error message?

I think during Initialize or Load the TB gets the focus - but when the next control is initialized / loaded the focus may change.
Choroid 29-Nov-23 14:27pm    
No Error Message When I had the ActiveControl expressed this way this.ActiveControl = tbEV;
with the return statement used upon closing the frmStart I would get a error at the
this.ActiveControl = tbEV; So I removed the this statement and the return was not needed
I agree too much happening will try to comment out the If Else routine Thanks
Dave Kreskowiak 29-Nov-23 14:32pm    
What's the error message.

Also, your code screams Windows Form, but the link you posted is for WPF. The two application types are not the same and do not support the same methods and properties on objects.

Which application type are you building? Windows Forms or WPF?
Choroid 30-Nov-23 15:02pm    
Windows Forms AND I should have learned by now that MS web pages are for every dialect Thanks for the answer I changed the tabindex to zero and tbEV.Focus(); now works when the form is revisited or gains focus returning from the frmDialog pop up message

1 solution

Assuming Windows Forms:

The .Focus() method does not work in the Form_Load handler because, well, there's no control yet as far as the UI is concerned. In the Form_Load handler, the form is LOADED, NOT SHOWN YET.

If you're going to set the initial textbox to have focus, the normal way of doing this would be to set the textbox TabStop property to True (already set by default) and set the TabIndex to 0.

If you need to do it in code, you have to call .Focus() on whatever control you want to have the input focus DURING or AFTER the Form_Shown event of the Form.
C#
private void Form1_Shown(object sender, EventArgs e)
{
    textBox3.Focus();
}
 
Share this answer
 
Comments
Maxim Kartavenkov 30-Nov-23 4:32am    
5.

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