Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i write code to allow the txtbox to setfocus when the form start 3

Status
Not open for further replies.
Jan 20, 2007
237
US
Hi Guys,
I know when creating a form manually, i can put in the form active event the following to allow the txtbox to setfocus when the form open, as follow
Thisform.txtTextBox.setfocus

ok now i am writing code to create a form, a txtbox, label, grid etc but don't how to write code to make the txtbox to setfocus, when the form open
can anyone indicate Please ?
see below what i have done
Code:
Local oForm
oForm = Createobject('myForm')
oForm.Show(2)  
Read Events

Define Class myForm As Form
	Height = 250
	Width = 300
	AutoCenter = .T.
	Caption = 'Search Vendor Names'
	ShowTips = .T.

   showWindow = 2

Add Object label1 As Label With ;
		top = 15,;
		left = 8,;
		caption = 'Enter Po# ',;
		Backstyle = 0

	Add Object Text1 As TextBox With ;
		top = 15,;
		left = 125,;
		Height = 23,;
		Width = 80,;
		value = '',;		
		ToolTipText = 'Start typing to search matching records,'+; 		
		' Double-click to clear search	'
Procedure Text1.DblClick
	This.Value = ''
	This.InteractiveChange()
	Endproc
and so on
Just need to know, how to write code, to allow the textbox to setfocus, when the form open
Thanks a lot
 
Can you guys correct me here ?
I added this as below
Code:
PROCEDURE myForm.activate 
	 WITH This	
	  .Text1.setfocus()
	 Endwith
     Endproc

then, i am getting this error "Unknown member myForm", can you explain me, what do i have wrong, need to learn from the mistakes
Thanks
 
Keep in mind that certain form elements/objects don't exist until other specific things occur.

While there are a number of places within the form (methods) in which you can initialize the objects, I generally use the Form.Init method.
By that time, all of the Form Objects have all been defined and can be referenced and/or initialized.

Good Luck,
JRB-Bldr
 
PROCEDURE myForm.activate

That's where you are going wrong. If the above line is part of your class definition, then you just need to write [tt]PROCEDURE.Activate[/tt]. In other words, remove the reference to MyForm.

Mike





__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I would put it in the forms init method, not the activate one.

Otherwise every time you switch back to the form, from a messagebox for example, you will end up giving your textbox the focus.

If you are half way down a form, doing validation - that is going to get frustrating.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
I see I made a small typographical error in my last post. I should have said [tt]PROCEDURE Activate[/tt], not [tt]PROCEDURE.Activate[/tt]. In other words, put a space between the two words, not a dot.

That said, Griff has made a very good point. The code should go [tt]PROCEDURE Init[/tt] rather than [tt]PROCEDURE Activate[/tt].

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
You can simply set the tabindex of the control you want to get initial focus to 1.

Bye, Olaf.

 
Hi Griff , Mike and Olaf
Yes it works ok on the Procedure Init thanks a lot for helping and teaching, the best place to learn
 
Setting focus to a control in the form's Init() has the side effect of making the form visible immediately. For a visually defined form, this means that the NOSHOW clause in a DO FORM formName.SCX NOSHOW statement won't be honored.

If your only intention is to make sure the textbox receives focus when the form is shown, then a simple Tabindex set to 1, as suggested, would be enough.

Code:
#DEFINE SET_FOCUS_BY_CODE	.T.

LOCAL AForm AS FormX

m.AForm = CREATEOBJECT("FormX")

#IF SET_FOCUS_BY_CODE
MESSAGEBOX("An error will be raised, because we are trying to set the modality of a form that is already visible")
#ELSE
MESSAGEBOX("The form is initialized, but not yet visible (you can do whatever you need to before the user sees it)")
#ENDIF

m.AForm.Show(1)

DEFINE CLASS FormX AS Form

	ADD OBJECT Text1 AS Textbox WITH Top = 10, Left = 10

#IF SET_FOCUS_BY_CODE

	ADD OBJECT Text2 AS Textbox WITH Top = 40, Left = 10

	PROCEDURE Init
	
		This.Text2.SetFocus()
	
	ENDPROC

#ELSE

	ADD OBJECT Text2 AS Textbox WITH Top = 40, Left = 10, Tabindex = 1

#ENDIF

ENDDEFINE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top