473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Method Focus()


Hi,

I want to set the focus to a specific textbox. When using somthing like
textbox1.Focus() it doesn't do anything. Am I missing something here?

Regards,

Christian
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
10 8563
Hi,
It should work. Are you by any chance traping the 'Enter' focus event?
which in turn is doing something to leave focus?

Cheers
Benny
Christian Pické wrote:
Hi,

I want to set the focus to a specific textbox. When using somthing like
textbox1.Focus() it doesn't do anything. Am I missing something here?

Regards,

Christian
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #2
Christian,

Can you give an example where the Focus method of the TextBox class does
not work?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Christian Pické" <in**@soft-service.net> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...

Hi,

I want to set the focus to a specific textbox. When using somthing like
textbox1.Focus() it doesn't do anything. Am I missing something here?

Regards,

Christian
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
I've also many times encountered this problem!

I've set Focus on some control, and breakpoint in the next line said
otherwise!
Couldn't explain it though!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OV**************@TK2MSFTNGP12.phx.gbl...
Christian,

Can you give an example where the Focus method of the TextBox class does not work?

Nov 16 '05 #4
Goran,

I still don't understand. What do you mean you breakpoint in the next
line? If you are setting breakpoints after a control has the focus set, and
then you switch to a debugger, then the focus is most definitely lost. This
is behavior I would expect.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Goran Genter" <go**********@fer.hr> wrote in message
news:c9**********@bagan.srce.hr...
I've also many times encountered this problem!

I've set Focus on some control, and breakpoint in the next line said
otherwise!
Couldn't explain it though!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:OV**************@TK2MSFTNGP12.phx.gbl...
Christian,

Can you give an example where the Focus method of the TextBox class

does
not work?


Nov 16 '05 #5
Christian,

Is it possible that you validatate the controls and the validation fails? In
this case it is possible that the control that fails the validation retains
the focus.

--

Stoitcho Goutsev (100) [C# MVP]
"Christian Pické" <in**@soft-service.net> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...

Hi,

I want to set the focus to a specific textbox. When using somthing like
textbox1.Focus() it doesn't do anything. Am I missing something here?

Regards,

Christian
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uN**************@TK2MSFTNGP12.phx.gbl...
Goran,

I still don't understand. What do you mean you breakpoint in the next
line? If you are setting breakpoints after a control has the focus set, and then you switch to a debugger, then the focus is most definitely lost. This is behavior I would expect.


OK! You're right with this one! My bad!
But I've looked into debugger only because that control wasn't focused after
I called focus method!
And I came across that problem in few projects!
Nov 16 '05 #7

Sorry, none of the above is happening. I am just opening a Windows Form
FormDiscount and have this code:

...
public FormDiscount()
{
InitializeComponent();
textboxReason.Focus();
}
...

When running the program, it's not textboxReason that gets the focus but
the control that has taborder number 1.

I did some simulation here because I encountered the problem on an other
form where some key-trapping is done, so I tried it on FormDiscount
which is a very simple form with 2 labels, 2 textboxes en 2 buttons, but
it still doesn't work!

Christian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8
Hi Christian,

Insted this code use the following
public FormDiscount()
{
InitializeComponent();
this.ActiveControl = textboxReason;
}

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Christian Pické" <in**@soft-service.net> wrote in message
news:uU****************@TK2MSFTNGP10.phx.gbl...

Sorry, none of the above is happening. I am just opening a Windows Form
FormDiscount and have this code:

..
public FormDiscount()
{
InitializeComponent();
textboxReason.Focus();
}
..

When running the program, it's not textboxReason that gets the focus but
the control that has taborder number 1.

I did some simulation here because I encountered the problem on an other
form where some key-trapping is done, so I tried it on FormDiscount
which is a very simple form with 2 labels, 2 textboxes en 2 buttons, but
it still doesn't work!

Christian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #9
OK, this one works. Thanks a lot!

But it still doesn't take away my frustration about a method that simply
doesn't work the way it's described (gets AND sets the focus).

Christian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10
Hi Christian,

The problem is that in the constructor CanFocus property returns *false*. It
returns false because the control is not visible yet. Normaly frameworks (at
least some of them ) don't allow focusing invisible windows. So, Focus
method checks CanFocus property and because it is *false* it doesn't do
anything. If it was true the Focus method would call SetFocus API and if
everything goes ok, would set ActiveControl.
However windows allows focusing invisible windows if you try the following
it works.

[DllImport("user32.dll")]
private extern static IntPtr SetFocus(IntPtr hwnd);
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
SetFocus(textBox1.Handle);
}
--

Stoitcho Goutsev (100) [C# MVP]
"Christian Pické" <in**@soft-service.net> wrote in message
news:OD*************@TK2MSFTNGP10.phx.gbl...
OK, this one works. Thanks a lot!

But it still doesn't take away my frustration about a method that simply
doesn't work the way it's described (gets AND sets the focus).

Christian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phl | last post by:
hi, I can use the following code to set focus to standard html elements but if I try to do the same for a custom element, it seems to complain that it does not support a focus method. Does...
31
by: Benno Bös | last post by:
If I use the following construct in the frame "main" for a link to an extern site: <A HREF="http://www.any.xy" TARGET="extern"> the Browser is creating the window "extern", loading the page...
3
by: bradwiseathome | last post by:
I focus on controls in a web form like this: <script language="javascript"> var focusControl = null; if (document.getElementById) focusControl = document.getElementById('txtArea1'); else if...
3
by: VA | last post by:
t=document.getElementById('mytable') is a HTML table with some input fields in its cells Why doesnt t.getElementsByTagName('tr').firstChild.focus; put the focus on that text field? It...
8
by: selen | last post by:
How can I focus the user to textbox. Thank you......
1
by: Praveena.Alapati | last post by:
Hi, There is a tab control on a page with multiple tabs and handful of controls in each tab. The way it's rendered in html (like grid etc..) is in table format in tablerows and cells. On...
9
by: Zytan | last post by:
http://msdn2.microsoft.com/en-us/system.windows.forms.control.focus(VS.80).aspx this page says: "Focus is a low-level method intended primarily for custom control authors. Instead, application...
4
by: Roger | last post by:
Hi, I am confused about the differences between this.window.focus(), window.focus(), and this.focus(). I want to use the calls in a <body onload="..."tag. What are the differences between...
2
by: Oriane | last post by:
Hi there, What could explain that although I set the focus on a TextBox with the Focus () method in my site, this textbox has the focus only the first time the page is constructed ? The page is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.