|
-
Jul 1st, 2004, 11:17 AM
#1
Thread Starter
Hyperactive Member
GotFocus
Is there a way to override the gotfocus event for all textboxes on the form in one method. I can write one and then attach Handles for gotfocus for all textboxes, but inside that I still have to check
if text1 then do this ...
if text2 then do this ...
while they will all be doing the same thing .. eg; change in backcolor.
is there a way so I do not have to check the textboxes. And if future if somebody else adds another textbox, it will be taken care of automaticallty without touching the code.
-
Jul 1st, 2004, 11:20 AM
#2
Use the Sender Object to figure out who is the one calling you, so
VB Code:
Sender.Backcolor = color.Red
-
Jul 1st, 2004, 11:33 AM
#3
Thread Starter
Hyperactive Member
For this to work, I will have to turn OFF option strict on all Files because when its on it does not allow late binding this way. I do not want to do this. Is there any other way ...
-
Jul 1st, 2004, 11:51 AM
#4
Hyperactive Member
Something like
If TypeOf Sender IS TextBox Then
ctype(sender,textbox).BackColor=color.red
end if
or you can use directcast
Dim tb as textbox
If TypeOf Sender IS TextBox Then
tb=directcast(sender,textbox)
end if
I'm writing from memory, you have to check the exact syntax!
Good job
Live long and prosper (Mr. Spock)
-
Jul 1st, 2004, 12:19 PM
#5
Thread Starter
Hyperactive Member
Jeez, I did not know you could do Ctypin'g on the left side of =
tx...C
-
Jul 2nd, 2004, 03:31 AM
#6
Hyperactive Member
And you can't, in fact!
Biological memory is not very affordable, expecially because I always use directcast. On the computer I was using I could not to have a check....
This is working code:
VB Code:
Dim Tb As TextBox
Dim Nome As String = "TxtImporto"
Dim punta As Byte = 1 ' La prima è la textbox1
Dim Totale As Double = 0
Dim Ctr As Control
Do While punta < 9
For Each Ctr In Me.Controls
If TypeOf Ctr Is TextBox Then
' Se qui, è una TextBox
Tb = DirectCast(Ctr, TextBox)
If Tb.Name = Nome & punta.ToString Then
'Se qui è la textbox col nome voluto (TextBoxn dove n=punta)
If Not IsNumeric(Tb.Text) Then
Tb.Text = "0,00"
End If
Totale += Double.Parse(Tb.Text)
End If
End If
Next
punta += 1
Loop
'.......below it continues, but is not very interesting......
Live long and prosper (Mr. Spock)
-
Jul 2nd, 2004, 11:14 PM
#7
Fanatic Member
i've got this since i don't have vs.net 2k3. i use textpad.
VB Code:
option strict on
imports system
imports system.drawing
imports system.windows.forms
public class sample
inherits form
sub new()
mybase.new()
initializecomponent()
end sub
public withevents t1 as new textbox()
public withevents t2 as new textbox()
public withevents t3 as new textbox()
sub initializecomponent()
t1.setbounds(100,100,100,20)
t2.setbounds(100,125,100,20)
t3.setbounds(100,150,100,20)
me.controls.addrange(new control(){t1,t2,t3})
for each c as control in me.controls
if typeof c is textbox then
addhandler c.gotfocus, addressof t
addhandler c.leave, addressof tt
end if
next
end sub
sub t(sender as object,e as eventargs)
ctype(sender,textbox).backcolor=color.red
end sub
sub tt(sender as object,e as eventargs)
ctype(sender,textbox).backcolor=color.white
end sub
shared sub main()
application.run(new sample())
end sub
end class
i do a vbc file.vb /r:system.dl,system.windows.forms.dll,system.drawing.dll
-
Jul 3rd, 2004, 07:16 AM
#8
PowerPoster
Originally posted by spoiledkid
For this to work, I will have to turn OFF option strict on all Files because when its on it does not allow late binding this way. I do not want to do this. Is there any other way ...
I'm not quite sure what you are trying to do but even with Option Strict On you CAN do:
VB Code:
Dim ctr As Control
For Each ctr In Controls
If TypeOf ctr Is TextBox Then
ctr.BackColor = Color.Red
End If
Next
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|