How to get the last active control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amnonevo@yahoo.com

    #1

    How to get the last active control

    Any equivalent to VB's PreviousControl in VB.NET?

  • Cor Ligthert [MVP]

    #2
    Re: How to get the last active control

    Hi,

    Probably there is a much better way, when not, than you can maybe try this
    sample I created.

    \\\\Needs by instance a form with two textoboxes and surely one button and
    pasting this code in
    Private PreviousControl As Control
    Private Sub Form1_Load(ByVa l sender As Object, _
    ByVal e As System.EventArg s) Handles MyBase.Load
    doSet(Me)
    End Sub
    Private Sub doSet(ByVal parentCtr As Control)
    Dim ctr As Control
    For Each ctr In parentCtr.Contr ols
    AddHandler ctr.Leave, AddressOf meLeave
    doSet(ctr)
    Next
    End Sub
    Private Sub meLeave(ByVal sender As Object, _
    ByVal e As System.EventArg s)
    PreviousControl = DirectCast(send er, Control)
    End Sub
    Private Sub Button1_Click(B yVal sender As System.Object, _
    ByVal e As System.EventArg s) Handles Button1.Click
    MessageBox.Show (PreviousContro l.Text)
    End Sub
    ///

    I hope this helps,

    Cor


    Comment

    • amnon

      #3
      Re: How to get the last active control

      Cor,

      Thanks for the code. It does help. Just one comment: it seems to me that
      the call to doSet(ctr) in the For Each loop in Sub doSet is redundant.
      Don't you think so?



      --
      Sent via .NET Newsgroups

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: How to get the last active control

        Amnon,
        [color=blue]
        >Just one comment: it seems to me that the call
        >to doSet(ctr) in the For Each loop in Sub doSet is redundant.
        > Don't you think so?[/color]

        No normally it is not.
        It is a recursive method.
        It gets as well all the controls that are in another control.

        However if you are sure that you have placed your controls only direct on
        your form. Than it is redundant.

        I hope this gives the idea?

        Cor


        Comment

        • amnon

          #5
          Re: How to get the last active control

          Yes, Thanks. This answers my question.

          --
          Sent via .NET Newsgroups

          Comment

          Working...