Results 1 to 8 of 8

Thread: GotFocus

  1. #1

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437

    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.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Use the Sender Object to figure out who is the one calling you, so
    VB Code:
    1. Sender.Backcolor = color.Red

  3. #3

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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 ...

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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)

  5. #5

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Jeez, I did not know you could do Ctypin'g on the left side of =

    tx...C

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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:
    1. Dim Tb As TextBox
    2.         Dim Nome As String = "TxtImporto"
    3.         Dim punta As Byte = 1 ' La prima è la textbox1
    4.         Dim Totale As Double = 0
    5.         Dim Ctr As Control
    6.  
    7.         Do While punta < 9
    8.  
    9.             For Each Ctr In Me.Controls
    10.                 If TypeOf Ctr Is TextBox Then
    11.                     ' Se qui, è una TextBox
    12.  
    13.                     Tb = DirectCast(Ctr, TextBox)
    14.  
    15.                     If Tb.Name = Nome & punta.ToString Then
    16.                         'Se qui è la textbox col nome voluto (TextBoxn dove n=punta)
    17.                         If Not IsNumeric(Tb.Text) Then
    18.                             Tb.Text = "0,00"
    19.                         End If
    20.  
    21.                         Totale += Double.Parse(Tb.Text)
    22.  
    23.                     End If
    24.  
    25.                 End If
    26.             Next
    27.             punta += 1
    28.  
    29.         Loop
    30.  
    31. '.......below it continues, but is not very interesting......

    Live long and prosper (Mr. Spock)

  7. #7
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i've got this since i don't have vs.net 2k3. i use textpad.
    VB Code:
    1. option strict on
    2. imports system
    3. imports system.drawing
    4. imports system.windows.forms
    5. public class sample
    6.    inherits form
    7.    
    8.    sub new()
    9.       mybase.new()
    10.       initializecomponent()
    11.    end sub  
    12.    
    13.    public withevents t1 as new textbox()
    14.    public withevents t2 as new textbox()
    15.    public withevents t3 as new textbox()
    16.    sub initializecomponent()
    17.       t1.setbounds(100,100,100,20)
    18.       t2.setbounds(100,125,100,20)
    19.       t3.setbounds(100,150,100,20)
    20.      
    21.       me.controls.addrange(new control(){t1,t2,t3})
    22.      
    23.       for each c as control in me.controls
    24.          if typeof c is textbox then
    25.             addhandler c.gotfocus, addressof t
    26.             addhandler c.leave, addressof tt
    27.          end if
    28.       next
    29.    end sub
    30.    
    31.    sub t(sender as object,e as eventargs)
    32.       ctype(sender,textbox).backcolor=color.red
    33.    end sub
    34.    
    35.    sub tt(sender as object,e as eventargs)
    36.       ctype(sender,textbox).backcolor=color.white
    37.    end sub
    38.    
    39.    shared sub main()
    40.       application.run(new sample())
    41.    end sub
    42. end class
    i do a vbc file.vb /r:system.dl,system.windows.forms.dll,system.drawing.dll

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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:
    1. Dim ctr As Control
    2.         For Each ctr In Controls
    3.             If TypeOf ctr Is TextBox Then
    4.                 ctr.BackColor = Color.Red
    5.             End If
    6.  
    7.         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
  •  



Click Here to Expand Forum to Full Width