473,396 Members | 2,021 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,396 software developers and data experts.

check textbox focus

I have 2 textboxes and one button and I don't know why my code doesn't work.
All I'm trying to do is write a number 1 into the textbox with the focus. so
if the user puts the cursor in textBox1 and pushes the button the 1 should go
into that box. If the cursor is in the other one the 1 should go there. It
seems simple but doesn't work. Can anyone give me any hints? Here is the code:

private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.Focused == true)
textBox1.Text = "1";
if (textBox2.Focused == true)
textBox2.Text = "1";
}
Nov 17 '05 #1
5 19187
Salam

Your code will not work correctly because when you click a Text Box, Focus
event is not fired, and as a result focus is false.

When you click on the TextBox, the Text Box Enter event is fired, here you
can set the focus of the text box by calling its Focus() Function. In this
way, ur code will work correctly
--
ALI RAZA SHAIKH
MCAD.net

www.programmersparadise.cjb.net
alirazashaikh.blogspot.com
"orahm" <or***@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
I have 2 textboxes and one button and I don't know why my code doesn't
work.
All I'm trying to do is write a number 1 into the textbox with the focus.
so
if the user puts the cursor in textBox1 and pushes the button the 1 should
go
into that box. If the cursor is in the other one the 1 should go there. It
seems simple but doesn't work. Can anyone give me any hints? Here is the
code:

private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.Focused == true)
textBox1.Text = "1";
if (textBox2.Focused == true)
textBox2.Text = "1";
}

Nov 17 '05 #2
Hi Orahm,
maybee i am wrong, but i would say that as soon as you klick on button1, the
button has the focus, So no one of your textboxes can have focus at that
moment.

To verify, add to your code:
if (button1.Focused == true) textbox1.Text = "b";
and try again. What happens?

greetings from germany
Chris

Nov 17 '05 #3
Hi Orahm,
i forgot to say...
you could use the .Leave-Event (is fired when focus leaves an element) to
let the textboxes write their number to some little helper variable. So the
button click can use the helper variable to determine which textbox had last
the focus.

Grettings from germany
Chris

Nov 17 '05 #4
Hi, thanks for replying. Both of your assumptions are correct. the focus is
always false because the button has it. Which event should I use to write the
one in the "last chosen" text box? I couldn't find an enter event?

"Christian Stueben" wrote:
Hi Orahm,
maybee i am wrong, but i would say that as soon as you klick on button1, the
button has the focus, So no one of your textboxes can have focus at that
moment.

To verify, add to your code:
if (button1.Focused == true) textbox1.Text = "b";
and try again. What happens?

greetings from germany
Chris

Nov 17 '05 #5
Thanks a lot the leave event did the trick

"orahm" wrote:
I have 2 textboxes and one button and I don't know why my code doesn't work.
All I'm trying to do is write a number 1 into the textbox with the focus. so
if the user puts the cursor in textBox1 and pushes the button the 1 should go
into that box. If the cursor is in the other one the 1 should go there. It
seems simple but doesn't work. Can anyone give me any hints? Here is the code:

private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.Focused == true)
textBox1.Text = "1";
if (textBox2.Focused == true)
textBox2.Text = "1";
}

Nov 17 '05 #6

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

Similar topics

4
by: Mad Scientist Jr | last post by:
I have a textbox that i am adding to (in codebehind of ASP.NET) and need to ensure that the focus is scrolled to the bottom of the textbox each time the page refreshes, and then set focus to a 2nd...
0
by: Jonas L | last post by:
Hi, I need to create a textbox which acts as a normal textbox but with the following extra requirements: 1) In-focus color, when the textbox gets focus the backcolor property of the textbox...
2
by: Jesper | last post by:
Last week I posted this for the first time. However, I didn't get any useable solution to my problem. I'm very keen on getting this problem solved, so please excuse me for trying again. I'll try to...
0
by: Mad Scientist Jr | last post by:
I have a textbox that i am adding to (in codebehind of ASP.NET) and need to ensure that the focus is scrolled to the bottom of the textbox each time the page refreshes, and then set focus to a 2nd...
2
by: Yoshitha | last post by:
In my webform i have 2 textbox controls and one button control. in 1st textbox control i'll enter email id when it lost its focus i've tocheck whether the entered email id exist in...
5
by: Tosch | last post by:
I have a usercontrol with a label, a textbox, a treeview, a grid and a couple of checkboxes. The usercontrol is hosted on a form together with a cancel and a accept button. This form is used to...
3
by: Henry Jones | last post by:
VS 2005 On a form I have a tabcontrol with 5 tabs. Each tab has three textboxes on it. When I click on each tab, I would like to have the focus set to the first textbox. On the tabenter...
4
geo039
by: geo039 | last post by:
I tried to write a simple application that takes user input by text and time selected by date time picker. It displays the appt description in one list box and the time in another list box. I wrote...
1
by: John Wright | last post by:
I am reading data from a serial port using the DataReceived event from the COM control. I need to check which text box has the focus on the form so I can call the right function. As you can see...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.