Results 1 to 7 of 7

Thread: [RESOLVED] Last Activate control

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Resolved [RESOLVED] Last Activate control

    MSDN brings nothing up, so here was my attempt at a work around.

    Code:
        Private Sub txtTime1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTime1.Leave, txtTime2.Leave, txtTime3.Leave, _
        txtTime4.Leave, txtTime5.Leave, txtTime6.Leave, txtTime7.Leave
    Code:
            Dim LastActiveTimeControl As String = Me.ActiveControl.Name
            Dim n As Integer = CInt(LastActiveTimeControl.Substring(LastActiveTimeControl.Length - 1))
    
            n += -1
    
            Dim newTime As String = String.Format("txtTime{0}", n)
    Because event leave calls another sub i need the last active control. I have the textbox name txtTime{0} but obviously string is not a control.

    So Two questions:

    1) can a string value be attached to an existing control if i know it's name?
    Dim attControl as Control = newTime

    2) All this could be avoided in event leave if i could get the left controls name. I have tried sender.tostring etc but nothing returns the textboxs name

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Last Activate control

    Think about what you're doing. You're getting the Name from the control and then storing it, then you're trying to use that to get the control again later. Why not just store the control in the first place?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Last Activate control

    Thats when question 2 comes in. This would obviously been the prefered method for me. But i cant figure out

    Code:
        Private Sub txtTime1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTime1.Leave, txtTime2.Leave, txtTime3.Leave, _
        txtTime4.Leave, txtTime5.Leave, txtTime6.Leave, txtTime7.Leave
    my.settings.LastActiveControl = ' here
    end sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Last Activate control

    Why are you using My.Settings for a start? You should just be using a member variable.

    As for what goes there, it's the 'sender'. The 'sender' is ALWAYS the object that raised the event. If you want to assign it to a variable of type Control or TextBox then you have to cast it as type Control or type TextBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Last Activate control

    That was just a quick insertion my.settings.

    Maybe i should of included my first attempt.....

    Code:
        Private Sub txtTime1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTime1.Leave, txtTime2.Leave, txtTime3.Leave, _
        txtTime4.Leave, txtTime5.Leave, txtTime6.Leave, txtTime7.Leave
            Dim ControlSender As Control = CType(sender, TextBox)
            Debug.WriteLine(ControlSender.ToString)
        End Sub
    output : WindowsApplication1.SelectedText, Text: textinfield

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Last Activate control

    There's no use assigning it to local variable because the variable ceases to exist at the end of the method. As I already said last post:
    You should just be using a member variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Last Activate control

    resolved ty john

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