8 Replies - 556 Views - Last Post: 23 January 2013 - 02:59 PM Rate Topic: -----

#1 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:04 PM

Hello everybody,

I have many textboxes in my form
In the lostfocus event of every textbox I store its name in a string variable called LastUsedTextBox

I also have many other controls on my form
I have a button

If the focus is on any other control which is not textbox then I want to have focus on textbox whose name was previously stored in the LastUsedTextbox (String) by clicking on the button

Does any one knows the answer.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Focus on a textbox whose name is stored in a variable in vb.net

#2 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:16 PM

You mean something like:

Me.Controls(LastTextUsed).Focus()


Where LastTextUsed might hold the string value "TextBox1"

This post has been edited by CharlieMay: 23 January 2013 - 01:16 PM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:20 PM

Loop through the list of MyForm.Controls
Find the one with that name
Set the focus to it.

But if it were me trying to force the user to walk through those textboxes, I would use the .Enabled property.

Start with all the controls set to .Enabled = false

Then you can enable as they properly complete one box. When all is good, you can enable the [SUBMIT] button or whatever you have. That primitive animation of the control changing gets the user attention so they realize what they should do next.
Was This Post Helpful? 1
  • +
  • -

#4 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:26 PM

Yes Exactly that
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,570
  • Joined: 12-December 12

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:28 PM

This is unclear to me. At what point do you want the focus to suddenly move from one control to another?

If it is when the form is first opened (within your application) then you can set the focus to the last-used textbox, but you would need to store the name of this last-control in My.Settings.

Edited: Sorry, the previous posts have made it a little clearer. Ignore me :)

This post has been edited by andrewsw: 23 January 2013 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:30 PM

this can be done in the windows forms but if i want to write this line a class library or a custom control project then what should I write?

I tried this but I failed
mybase.controls(LastUsedTextBox).Focus()

Any other solutions
Was This Post Helpful? 0
  • +
  • -

#7 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:36 PM

View Postandrewsw, on 24 January 2013 - 01:58 AM, said:

This is unclear to me. At what point do you want the focus to suddenly move from one control to another?

If it is when the form is first opened (within your application) then you can set the focus to the last-used textbox, but you would need to store the name of this last-control in My.Settings.

If you want to do this as the user switches between different forms, then most uses would find this a very annoying feature.

Perhaps you should clarify.


This question was an example to simplify the problem
Currently I am working on a custom control which is inheritated from textbox and have a property called AssociatedDataGridView
So I am searching in AssociatedDataGridView what user writes in the textbox
But I got a problem here
I want if the user clicks on the AssociatedDataGridView the focus should be returned to the textbox
because this Textbox and DatagridView should work like a single control
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,570
  • Joined: 12-December 12

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 01:39 PM

Quote

Loop through the list of MyForm.Controls
Find the one with that name
Set the focus to it.


Shouldn't need to loop(?):

Me.Controls(LastUsed).Focus()

Was This Post Helpful? 0
  • +
  • -

#9 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Re: Focus on a textbox whose name is stored in a variable in vb.net

Posted 23 January 2013 - 02:59 PM

no
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1