Ok, here's what I've got.

In the screenshot, you'll see three things- a start time, end time and total time. So the goal is this-

A user puts in a current start time by hitting F1 at the start of shift. They tab across the end time to the "time field", then go across the row.

Then they enter their case number, then hit F1 again, and it will enter the current time in the start box on the next row, as well as putting that time in the end box in the row above...

And keep doing this down the whole form.

I hope this makes sense. If anyone can think of a better way to do this, I'm all ears. I need help with the code.

Recommended Answers

All 9 Replies

I forgot to mention I'm using VB 2010.

See if this helps.

'// Prerequisites: 3 TextBoxes.
'// TextBox1 = current start time, TextBox2 = case number, TextBox3 = current time in the start box on the next row
Public Class Form1

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            Dim tempStr As String = TimeOfDay
            tempStr = tempStr.Substring(0, tempStr.Length - 6) '// get only hours and minutes.
            If TextBox1.Focused Then TextBox1.Text = tempStr
            If TextBox2.Focused AndAlso TextBox2.Tag = "enter Key pressed." Then TextBox3.Text = tempStr
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True '// important when using keys pressed and having other controls on the Form.
    End Sub

    Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
        If e.KeyCode = Keys.Enter Then
            TextBox2.Tag = "enter Key pressed." '// set the tag to some value.
        End If
    End Sub

    Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
        TextBox2.Tag = "" '// reset tag if lost focus.
    End Sub
End Class

Thanks for the chunk of code. I implimented it and this is what I got.

First off, my text boxes are laid out as followes:

Start1,End1,Time1,CallType1,Description1,Case1
Start2,End2,Time2,CallType2,Description2,Case2
Start3,End3,Time3,CallType3,Description3,Case3

and so on down to 23.

So I edited your code as follows to match my text box naming scheme-

'// Prerequisites: 3 TextBoxes.
    '// TextBox1 = current start time, TextBox2 = case number, TextBox3 = current time in the start box on the next row

        Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
            If e.KeyCode = Keys.F1 Then
                Dim tempStr As String = TimeOfDay
                tempStr = tempStr.Substring(0, tempStr.Length - 6) '// get only hours and minutes.
            If Start1.Focused Then Start1.Text = tempStr
            If Case1.Focused AndAlso Case1.Tag = "enter Key pressed." Then Start2.Text = tempStr
            End If
        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.KeyPreview = True '// important when using keys pressed and having other controls on the Form.
        End Sub

    Private Sub Case1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Case1.KeyUp
        If e.KeyCode = Keys.Enter Then
            Case1.Tag = "enter Key pressed." '// set the tag to some value.
        End If
    End Sub

F1 key does what it should. So when it goes to the next line, how do I take that new start time and fill in the preceding end time? Do I make sense?

For example

When the case number box is active (say for example Case1), and I hit F1 to enter a new start time (on say Start2), now does that new time get entered onto End1 also?

This code sample will add appropriate Events for each textbox defined and also add the "Start" time for a new row and "End" time for the current row.

Public Class Form1

    Dim activeCtl As TextBox '// control that is currently in focus.

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            Dim tempStr As String = TimeOfDay
            tempStr = tempStr.Substring(0, tempStr.Length - 6) '// get only hours and minutes.

            If activeCtl.Name.StartsWith("Start") Then
                activeCtl.Text = tempStr '// add Start time.
                Exit Sub '// skip remaining code.
            End If

            '// add "Start" time for next line of textboxes and add "End" time for the current line of textboxes.
            If activeCtl.Name.StartsWith("Case") AndAlso activeCtl.Tag = "enter Key pressed." Then
                Dim ctlNumber As String = activeCtl.Name.Substring(4, activeCtl.Name.Length - 4) '// get number of textbox.
                For Each ctlStart As Control In TabControl1.TabPages(0).Controls '// loop thru controls.
                    If TypeOf (ctlStart) Is TextBox And ctlStart.Name = "Start" & ctlNumber + 1 Then '// if textbox and next in line.
                        ctlStart.Text = tempStr '// add "Start" time.
                        For Each ctlEnd As Control In TabControl1.TabPages(0).Controls '// loop thru controls to locate the preceeding End time.
                            If TypeOf (ctlEnd) Is TextBox And ctlEnd.Name = "End" & ctlNumber Then ctlEnd.Text = tempStr '// add "End" time.
                            Exit For '// end loop.
                        Next
                        Exit For '// end loop.
                    End If
                Next
            End If
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True '// important when using keys pressed and having other controls on the Form.

        For Each ctl As Control In TabControl1.TabPages(0).Controls '// loop thru all controls.
            If TypeOf (ctl) Is TextBox And ctl.Name.StartsWith("Case") Then '// find "Case" named controls.
                AddHandler ctl.KeyUp, AddressOf Case_KeyUp '// add KeyUp event for control.
                AddHandler ctl.GotFocus, AddressOf Ctl_GotFocus '// add GotFocus event for control.
            End If
            If TypeOf (ctl) Is TextBox And ctl.Name.StartsWith("Start") Then '// find "Start" named controls.
                AddHandler ctl.GotFocus, AddressOf Ctl_GotFocus '// add GotFocus event for control.
            End If
        Next
    End Sub

    Private Sub Case_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            sender.Tag = "enter Key pressed." '// set the tag to some value.
        End If
    End Sub

    Private Sub Ctl_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        activeCtl = sender '// determine which control is in focus.
    End Sub
End Class

Btw, thanks for adding the way your controls are setup, it helped. :)

Thanks codeorder.

It's not filling in the End time on the preceding row after entering the time on the current row.

By the way- thanks for the help you've given so far. I'm pretty new to this and am struggling to say the least.

I put the entire project up on my site at http://www.robrtaylor.com/DailyLog/DailyLogMaster.zip. If you looked at it, that would be cool, if not, I understand the hesitation in downloading someone's project files from the net and the security risks, etc.

Either way, I'm going to do some reading up on this and see what I can learn from it.

Rob

I ended up downloading the project file and am currently overlooking the Daily Log Form.
You may delete the project from your website if preferred.

I do appreciate the concern about downloading strange files/projects since I do feel the same way. However, I did scan with AVG, both the zip and unzipped folder, and looks promising. If I cannot trust anything, I probably would not trust myself, not AVG. :D

I will send a private message when done overlooking the code.
Nice website btw.:)

Nice website btw.:)

Thanks. I do some websites on the side, I'm a full time deputy sheriff assigned to patrol The whole point of this program I'm trying to write is that we currently use a spreadsheet to log our activities. I hate it. Other deputies hate it. It serves it's purpose ie: admin can get their stats and all, but I kept thinking there has to be a better way to collect this data throughout the shift. The spreadsheet is large and cumbersome. But- this log prints to the spreadsheet because that's what the admin likes. My goal is to make it a bit easier for us guys on patrol to quickly enter this stuff while on the go.

I've removed the file from my server for now. Thanks for your time codeorder- if I ever catch you speeding through my county I'll cut you a break! ;)

codeorder, did you get my PM? I'm not sure if it went through, or if it went through 5 times...

:)
Just tunning up my sport car to test it in your county.:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.