Results 1 to 21 of 21

Thread: [RESOLVED] [2005] Return to where I've just come from

  1. #1

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Resolved [RESOLVED] [2005] Return to where I've just come from

    Hello All,

    That's a mouthful!!

    On my form I have several textboxes and an Exit button. If the user hits the exit button but after a Yes/No exit message the user decides not to exit, how can I return to the textbox I was in prior to the call?

    I've tried Return, Iv'e tried Form1.Focus(), all I get is Form1 again and the cursor is nowhere to be seen, until I click on a textbox. My Exit code is below.

    Code:
        Public Sub ExitProgram()
    
            Dim response As Integer
    
            response = MsgBox("End Program Now", vbYesNo + vbQuestion)
            If response = vbYes Then
                End
            ElseIf response = vbNo Then
                cancel = 1
            End If
            
        End Sub
    Rgds,
    Tarablue

  2. #2
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2005] Return to where I've just come from

    first of try using messagebox.show.

    vb Code:
    1. If MessageBox.Show("Wanna exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    2.             'Exit here
    3.         Else
    4.             'Dont exit
    5.         End If

    to get focus back to the textbox you could declare a textbox object.

    in the textbox lostfocus event, set the lasttxt to the sender.



    vb Code:
    1. Dim lastTxt As TextBox
    2.  
    3. Private Sub TextBoxes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus, nexttextbox etc.
    4.         lastTxt = CType(sender, TextBox)
    5.     End Sub
    6.  
    7. If MessageBox.Show("Wanna exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    8.             'Exit here
    9.         Else
    10.             'Dont exit
    11.             lastTxt.Focus()
    12.         End If

    there are properly better ways of doing this but this will work

  3. #3

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: [2005] Return to where I've just come from

    Hello gnaver,

    Sorry mate just couldn't get it to work. I placed Dim lastTxt As TextBox at the top of my code and changed Dim to Public. I then added Private Sub TextBoxes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus, nexttextbox etc. lastTxt = CType(sender, TextBox) End Sub.

    I assumed that nexttextbox etc was for all the other TextBoxes on my form but if I leave your code as written I just get an error over etc requesting a period in it's place.

    Can you expand on your code a little.

    Best Rgds,
    Tarablue

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

    Re: [2005] Return to where I've just come from

    He means that if you have multiple TextBoxes then you should include the appropriate event of ALL of them in the Handles clause. You don't have to do this manually though. Let the IDE do the work for you, as you always should with event handlers. In the designer select all your TextBoxes. Open the Properties window and click the Events button. Now double-click the appropriate event and voila!

    Also, note that you should be handling the Leave event, not LostFocus.
    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
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: [2005] Return to where I've just come from

    Hello jmcilhinney,

    That's an area I've never been into before, shows you how much I know and use the power of VB.net.

    OK, how can you call this feature when there are TextBoxes that are created at 'Run-Time' by the program when certain conditions are met? At the start of my program I have 7 TextBoxes but can have upto 17 in total based on user input.

    Best Rgds,
    Tarablue

  6. #6

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: [2005] Return to where I've just come from

    Hello gnaver & jmcilhinney,

    Got It!!!

    I think what was going wrong is that I had my Exit Program as a separate module, once I got rid of that and included the code within my ButtonClick event all was cleared.

    I still have the problem of additional TextBoxes created at 'Run-Time', any thoughts on that would be appreciated.

    Best Rgds,
    Tarablue

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

    Re: [2005] Return to where I've just come from

    When you create a control at run time you use the AddHandler statement to attach and existing method to an event of the object. When the event is raised you can get a reference to the object that raised it from the 'sender' parameter.
    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

  8. #8

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: [2005] Return to where I've just come from

    Hello jmcilhinney,

    The following code is what I use to create additional boxes during 'Run-Time'.
    Code:
        Private Sub NumericalTextBox2_enter(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles NumericalTextBox1.Leave
    
            Dim tb1 As New NumericalTextBox
            Dim tb2 As New NumericalTextBox
    
            Me.NumericalTextBox2.Focus()
            Me.NumericalTextBox2.SelectAll()
    
            TextLeft = 6
            TextRight = 7
    
            M = Val(NumericalTextBox1.Text)
            N = M + 1
            A = 26
    
            If N > 3 Then
    
                For i As Integer = 1 To M - 2
                    TextNumberLeft = TextLeft + i
                    With tb1
                        .Name = "NumericalTextBox" & TextNumberLeft
                        .TextAlign = HorizontalAlignment.Right
                        .TabStop = True
                        .TabIndex = TextNumberLeft
                        .Size = New Size(50, 20)
                        .Location = New Point(12, (133 + (i * A)))
                        .Focus()
                        .SelectAll()
                    End With
                    Me.Controls.Add(tb1)
                    TextLeft = TextLeft + 1
    
                    TextNumberRight = TextRight + i
                    With tb2
                        .Name = "NumericalTextBox" & TextNumberRight
                        .TextAlign = HorizontalAlignment.Right
                        .TabStop = True
                        .TabIndex = TextNumberRight
                        .Size = New Size(50, 20)
                        .Location = New Point(68, (133 + (i * A)))
                        .Focus()
                        .SelectAll()
                    End With
                    Me.Controls.Add(tb2)
                    TextRight = TextRight + 1
                Next
    
            Else : Exit Sub
            End If
        End Sub
    Now I'm looking at How to: Write Event Handlers in MSDN and am a bit lost on all of this.

    Best Rgds,
    Tarablue

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

    Re: [2005] Return to where I've just come from

    First up, what's this for?
    vb.net Code:
    1. Else : Exit Sub
    What exactly does that achieve?

    As to your question, you should already have written the event handler. I've already told you how to let the IDE do it. If you have no controls in the designer then add one, create the event handler, then delete the control. You then use the AddHandler statement to attach the event handler to the desired event.
    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

  10. #10

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: [2005] Return to where I've just come from

    hello jmcilhinney,

    1/ What's this for - I have no idea. It's pointless and must have been left over from some other idea I had. Now deleted.

    On my form I have 7 NumericalTextBoxes and I went into the Event area, as you mentioned before, and created the following event handler
    Code:
        Private Sub TextBoxes_Leave(ByVal sender As Object, _
                ByVal e As System.EventArgs) Handles NumericalTextBox7.Enter, _
                NumericalTextBox6.Enter, NumericalTextBox5.Enter, NumericalTextBox4.Enter, _
                NumericalTextBox3.Enter, NumericalTextBox2.Enter, NumericalTextBox1.Enter
    
            lastTxt = CType(sender, NumericalTextBox)
    
        End Sub
    I think then that I should be placing the AddHandler in the previous coding I put up, but I can't get it. I even tried to place this in the above code, still no luck.

    Best Rgds,
    Tarablue
    Last edited by Tarablue; Jun 18th, 2008 at 10:48 PM.

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

    Re: [2005] Return to where I've just come from

    So you've got some TextBoxes created in the designer and some created at run time, correct? If so then you will use that same method to handle the events of all of them. When you create a TextBox at run time you use the AddHandler statement to attach that method to the new TextBox's event of interest.

    have you read the MSDN documentation for the AddHandler statement? If not then do so. Next, show us your attempt and we'll see what's wrong.

    Also, it's not going to break anything but your method is named "TextBoxes_Leave" and it's handling Enter events
    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

  12. #12

    Thread Starter
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: [2005] Return to where I've just come from

    Hello jmcilhinney,

    EUREKA Got the little bugger!!

    A simple 'One-Line' statement;
    Code:
                    AddHandler tb1.Enter, AddressOf TextBoxes_Enter
    Obviously it's finding where to put it that can cause some aggravation.

    Thanks for the pointer.

    Best Rgds,
    Tarablue

  13. #13
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [RESOLVED] [2005] Return to where I've just come from

    I thought this post interesting because it showed how to an event handler for multiple text boxes. The problem I ran into when trying it was that if I did not enter anything in the textboxes and select NO, i get an error

    Attachment 64937

    how can you account for this?
    Last edited by CoachBarker; Apr 15th, 2009 at 09:30 AM.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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

    Re: [RESOLVED] [2005] Return to where I've just come from

    Quote Originally Posted by CoachBarker
    I thought this post interesting because it showed how to an event handler for multiple text boxes. The problem I ran into when trying it was that if I did not enter anything in the textboxes and select NO, i get an error

    Attachment 64937

    how can you account for this?
    Obviously lastTxt is Nothing, so you've never actually assigned anything to that variable. This is what debugging is for: to find the cause of issues like this.
    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

  15. #15
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [RESOLVED] [2005] Return to where I've just come from

    as simple as this I guess

    vb Code:
    1. Dim lastTxt As New TextBox

    instead of just

    vb Code:
    1. Dim lastTxt As TextBox
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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

    Re: [RESOLVED] [2005] Return to where I've just come from

    Quote Originally Posted by CoachBarker
    as simple as this I guess

    vb Code:
    1. Dim lastTxt As New TextBox

    instead of just

    vb Code:
    1. Dim lastTxt As TextBox
    No, not as simple as that. The point of that variable is to refer to the last TextBox that had focus. What good is assigning a New TextBox to it that hasn't even been added to the form? That isn't the last TextBox that had focus, is it?
    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

  17. #17
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [RESOLVED] [2005] Return to where I've just come from

    Not sure if anyone will respond, but I have the same problem with a Pause and Continue Button. When I pause I want to catch the last acti control and when I continue I want to return to the same control. I could do this in the shown in this post, but I am using TextBoxes, MaskedTextBoxes and DateTimePickers. I would assume I would have to cast tham all somehow but not sure how to do it.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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

    Re: [RESOLVED] [2005] Return to where I've just come from

    Just declare a Control variable and on the Enter event of each control except your Pause Button, assign that control to the variable. You can use a single method to handle all events and just cast the sender as type Control.
    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

  19. #19
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [RESOLVED] [2005] Return to where I've just come from

    That is what I have been trying to do, I will keep trying.

    Git it. Like this? If so I just have to change it to C# and put it in my project when I get to work tomorrow.

    Code:
    Public Class frmWhatControl
        Dim lastControl As Control
    
        Private Sub DateTimePicker2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.Enter, TextBox4.Enter, MaskedTextBox2.Enter, MaskedTextBox1.Enter, DateTimePicker2.Enter, DateTimePicker1.Enter
            With DirectCast(sender, Control)
                .BackColor = Color.DarkBlue
                .ForeColor = Color.White
                .Font = New Font(.Font, FontStyle.Bold)
            End With
            Me.lastControl = DirectCast(sender, Control)
        End Sub
    
        Private Sub DateTimePicker2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.Leave, TextBox4.Leave, MaskedTextBox2.Leave, MaskedTextBox1.Leave, DateTimePicker2.Leave, DateTimePicker1.Leave
            With DirectCast(sender, Control)
                .BackColor = SystemColors.Window
                .ForeColor = SystemColors.ControlText
                .Font = New Font(.Font, FontStyle.Regular)
            End With
          
        End Sub
    
        Private Sub disableControls()
            TextBox4.Enabled = False
            TextBox5.Enabled = False
            MaskedTextBox1.Enabled = False
            MaskedTextBox2.Enabled = False
            DateTimePicker1.Enabled = False
            DateTimePicker2.Enabled = False
        End Sub
    
        Private Sub enableControls()
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            MaskedTextBox1.Enabled = True
            MaskedTextBox2.Enabled = True
            DateTimePicker1.Enabled = True
            DateTimePicker2.Enabled = True
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            disableControls()
            If Not Me.lastControl Is Nothing Then
                MessageBox.Show(Me.lastControl.Name)
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            enableControls()
            Me.lastControl.Select()
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim Main As New frmMain
            Main.Show()
            Me.Close()
        End Sub
    Last edited by CoachBarker; Jan 18th, 2009 at 07:59 PM.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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

    Re: [RESOLVED] [2005] Return to where I've just come from

    Quote Originally Posted by CoachBarker
    That is what I have been trying to do, I will keep trying.

    Git it. Like this? If so I just have to change it to C# and put it in my project when I get to work tomorrow.

    Code:
    Public Class frmWhatControl
        Dim lastControl As Control
    
        Private Sub DateTimePicker2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.Enter, TextBox4.Enter, MaskedTextBox2.Enter, MaskedTextBox1.Enter, DateTimePicker2.Enter, DateTimePicker1.Enter
            With DirectCast(sender, Control)
                .BackColor = Color.DarkBlue
                .ForeColor = Color.White
                .Font = New Font(.Font, FontStyle.Bold)
            End With
            Me.lastControl = DirectCast(sender, Control)
        End Sub
    
        Private Sub DateTimePicker2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.Leave, TextBox4.Leave, MaskedTextBox2.Leave, MaskedTextBox1.Leave, DateTimePicker2.Leave, DateTimePicker1.Leave
            With DirectCast(sender, Control)
                .BackColor = SystemColors.Window
                .ForeColor = SystemColors.ControlText
                .Font = New Font(.Font, FontStyle.Regular)
            End With
          
        End Sub
    
        Private Sub disableControls()
            TextBox4.Enabled = False
            TextBox5.Enabled = False
            MaskedTextBox1.Enabled = False
            MaskedTextBox2.Enabled = False
            DateTimePicker1.Enabled = False
            DateTimePicker2.Enabled = False
        End Sub
    
        Private Sub enableControls()
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            MaskedTextBox1.Enabled = True
            MaskedTextBox2.Enabled = True
            DateTimePicker1.Enabled = True
            DateTimePicker2.Enabled = True
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            disableControls()
            If Not Me.lastControl Is Nothing Then
                MessageBox.Show(Me.lastControl.Name)
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            enableControls()
            Me.lastControl.Select()
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim Main As New frmMain
            Main.Show()
            Me.Close()
        End Sub
    It's best not to edit your posts so long after creating them unless it's to correct a mistake. If you want to add something then add a new post. I was all set to reply and I hit the Quote button and I see that what I'm quoting is not what I read. Anyway, here's what I was going to post:

    I can do this:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private lastActiveControl As Control
    4.  
    5.     Private Sub Controls_Enter(ByVal sender As System.Object, _
    6.                                ByVal e As System.EventArgs) Handles TextBox1.Enter, _
    7.                                                                     DateTimePicker1.Enter, _
    8.                                                                     ComboBox1.Enter
    9.         Me.lastActiveControl = DirectCast(sender, Control)
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         Me.lastActiveControl.Select()
    14.     End Sub
    15.  
    16. End Class
    and then click the Button and whatever control was active before I clicked will be active afterwards. It even works if I tab to the Button. Your situation will be a little more involved but not much.
    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

  21. #21
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [RESOLVED] [2005] Return to where I've just come from

    My apologies, I got pretty excited that it worked afterplaying around with it all week end. Thanks for the nudge in the right direction.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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