|
-
Jun 17th, 2008, 04:45 AM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 17th, 2008, 05:35 AM
#2
Hyperactive Member
Re: [2005] Return to where I've just come from
first of try using messagebox.show.
vb Code:
If MessageBox.Show("Wanna exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
'Exit here
Else
'Dont exit
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:
Dim lastTxt As TextBox
Private Sub TextBoxes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus, nexttextbox etc.
lastTxt = CType(sender, TextBox)
End Sub
If MessageBox.Show("Wanna exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
'Exit here
Else
'Dont exit
lastTxt.Focus()
End If
there are properly better ways of doing this but this will work
-
Jun 17th, 2008, 09:56 PM
#3
Thread Starter
Hyperactive Member
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
-
Jun 17th, 2008, 10:53 PM
#4
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.
-
Jun 18th, 2008, 04:03 AM
#5
Thread Starter
Hyperactive Member
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
-
Jun 18th, 2008, 06:26 PM
#6
Thread Starter
Hyperactive Member
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
-
Jun 18th, 2008, 07:27 PM
#7
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.
-
Jun 18th, 2008, 08:30 PM
#8
Thread Starter
Hyperactive Member
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
-
Jun 18th, 2008, 08:35 PM
#9
Re: [2005] Return to where I've just come from
First up, what's this for?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.
-
Jun 18th, 2008, 10:36 PM
#10
Thread Starter
Hyperactive Member
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.
-
Jun 18th, 2008, 10:42 PM
#11
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
-
Jun 18th, 2008, 11:45 PM
#12
Thread Starter
Hyperactive Member
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
-
Jun 19th, 2008, 10:30 AM
#13
Frenzied Member
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.
-
Jun 19th, 2008, 06:35 PM
#14
Re: [RESOLVED] [2005] Return to where I've just come from
 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.
-
Jun 19th, 2008, 06:50 PM
#15
Frenzied Member
Re: [RESOLVED] [2005] Return to where I've just come from
as simple as this I guess
vb Code:
Dim lastTxt As New TextBox
instead of just
-
Jun 19th, 2008, 06:58 PM
#16
Re: [RESOLVED] [2005] Return to where I've just come from
 Originally Posted by CoachBarker
as simple as this I guess
vb Code:
Dim lastTxt As New TextBox
instead of just
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?
-
Jan 18th, 2009, 08:06 AM
#17
Frenzied Member
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.
-
Jan 18th, 2009, 05:06 PM
#18
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.
-
Jan 18th, 2009, 07:22 PM
#19
Frenzied Member
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.
-
Jan 18th, 2009, 08:05 PM
#20
Re: [RESOLVED] [2005] Return to where I've just come from
 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:
Public Class Form1
Private lastActiveControl As Control
Private Sub Controls_Enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.Enter, _
DateTimePicker1.Enter, _
ComboBox1.Enter
Me.lastActiveControl = DirectCast(sender, Control)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.lastActiveControl.Select()
End Sub
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.
-
Jan 18th, 2009, 08:47 PM
#21
Frenzied Member
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.
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
|