Click to See Complete Forum and Search --> : VB Bug ???


December 9th, 1999, 10:39 AM
Hi all,

Here's my problem


Given

A Form containing

A/ a textbox (txt_Name = Name of a company)
B/ a command button (cmd_Save = Save button)
C/ a lostfocus event for this textbox (txt_Name)

private Sub LostFocus_txt_Name()
' Checks if company name is filled in
' & then enables the command buttton
cmd_Save.Enabled = false
If (len(Trim(txt_Name)) = 0) then
Exit Sub
End If
cmd_Save.Enabled = true
End Sub



Problem

You have to click 2 times on the command button to activate the
Click event on this command button !!!

Is there a solution to this problem ???

Thanks.

Jordan

December 9th, 1999, 01:24 PM
If you are using VB6 it's much better to use Validate event:

private Sub txtName_Validate(Cancel as Boolean)
If txtName.Text = "" then Cancel = true
End Sub



Actually you can use LostFocus for this particular case, but when you place at least one more control which value you need to validate when it's loosing focus you'll have hightmare.
Vlad

Chris Eastwood
December 9th, 1999, 05:58 PM
It's not a bug - just a 'bad' (?) way of doing it. How about setting the enabled property in the 'change' event of the text1 textbox :


private Sub txtName_Change()
If len(Trim$(txtName.Text)) > 0 then
cmdSave.Enabled = true
else
cmdSave.Enabled = false
End If
'
' or if you want to be really clever
'
' cmdSave.Enabled = (len(Trim$(txtName.Text)) > 0)

End Sub




Either of the two methods above will work (- with all versions of VB since v3 I think)

Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Sudharshaan
December 28th, 1999, 04:46 AM
hey use a global variable here goes
public i as integer

form_load()
I=0
end sub

command1_click()
if i =0 then
i=I+1
exit sub
else
* perform wot ever operations u want *
i=0 again
end if
end sub

try this code it may work!

Sudharshaan