8 Replies - 398 Views - Last Post: 14 February 2013 - 08:20 AM Rate Topic: -----

#1 konradwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-February 13

Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 09:53 AM

Hi Folks

I am trying to have 10 buttons (0 -9) to enter into a label for someone to enter minutes

So as they type in 1 it shows 01

type 15 it shows 15 but then type 2 and it becomes 52

always dropping the first character

I appreciate any thoughts.. I am a beginner

I have tried this:

  Private Sub btnNumber0_Click(sender As Object, e As EventArgs) Handles btnNumber9.Click, btnNumber8.Click, btnNumber7.Click, btnNumber6.Click, btnNumber5.Click, btnNumber4.Click, btnNumber3.Click, btnNumber2.Click, btnNumber1.Click, btnNumber0.Click
        txtSTart_TipBox.Visible = False

        Dim addMinutes As String  ' holder for new minutes
        Dim currMinutes As Integer = txtMainFrm_ButtonTimerEntry.Text
        addMinutes = CType(sender, Button).Text
      
        If currMinutes = 0 Then
            txtMainFrm_ButtonTimerEntry.Text = addMinutes
        ElseIf currMinutes > 9 AndAlso currMinutes < 100 Then

            txtMainFrm_ButtonTimerEntry.Text = "0" & addMinutes
        End If

    End Sub




and this


Private Sub btnNumber0_Click(sender As Object, e As EventArgs) Handles btnNumber9.Click, btnNumber8.Click, btnNumber7.Click, btnNumber6.Click, btnNumber5.Click, btnNumber4.Click, btnNumber3.Click, btnNumber2.Click, btnNumber1.Click, btnNumber0.Click

        Dim addMinutes As Integer = CType(sender, Button).Text
        Dim curMinutes As String = lblMeetingTimerMinutes.Text
        If curMinutes = 0 Then
            lblMeetingTimerMinutes.Text = addMinutes
        ElseIf curMinutes > 0 Then
            lblMeetingTimerMinutes.Text = (curMinutes & addMinutes)


        End If

    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Whats wrong with my code - Numeric Buttons entry

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9206
  • View blog
  • Posts: 34,589
  • Joined: 12-June 08

Re: Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 10:03 AM

I would go about it using the 'pad left' string function to pad any string with 0's for three places... then use a substring of that result and get the right two most characters..

Any new character typed would just be appended to the two character string and the process repeats itself!
Was This Post Helpful? 1
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 282
  • Posts: 892
  • Joined: 01-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 12:05 PM

Hi konradwalsh,
There are a few different ways you can get this done. modi123`s idea should work fine. However, you can also put everything together into one string and then use a substring of that. It just depends on which way you feel like doing it.

Example :
    Private Sub Button_0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_0.Click, Button_1.Click, Button_2.Click, Button_3.Click, Button_4.Click, Button_5.Click, Button_6.Click, Button_7.Click, Button_8.Click, Button_9.Click
        Dim num As String = CType(sender, Button).Text
        num = "0" & Label_Minutes.Text & num
        Label_Minutes.Text = num.Substring(num.Length - 2)
    End Sub


Was This Post Helpful? 1
  • +
  • -

#4 konradwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 02:59 PM

[quote name='IronRazer' date='13 February 2013 - 12:05 PM' timestamp='1360782354' post='1802182']
Hi konradwalsh,
There are a few different ways you can get this done. modi123`s idea should work fine. However, you can also put everything together into one string and then use a substring of that. It just depends on which way you feel like doing it.



thank you very much.. i was sort over complicating it... thanks to both of you for your help
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 282
  • Posts: 892
  • Joined: 01-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 03:19 PM

You are welcome. I am glad it helped.
Was This Post Helpful? 0
  • +
  • -

#6 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 04:55 PM

How about this:

Convert to a integer and it will never had a 0 at the start.

Public Class Form1

    Dim number_input As String = ""

    Private Sub number_Click(sender As System.Object, e As System.EventArgs) Handles cmb_0.Click, cmb_9.Click, cmb_8.Click, cmb_7.Click, cmb_6.Click, cmb_5.Click, cmb_4.Click, cmb_3.Click, cmb_2.Click, cmb_1.Click
        Try
            number_input = CType(sender, Button).Text
            txt_minutes.Text = CInt(txt_minutes.Text & number_input)
        Catch ex As Exception
            txt_minutes.Text = "Error"
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        txt_minutes.Text = "0"
    End Sub

    Private Sub cmb_clear_Click(sender As System.Object, e As System.EventArgs) Handles cmb_clear.Click
        txt_minutes.Text = "0"
    End Sub


Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 282
  • Posts: 892
  • Joined: 01-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 13 February 2013 - 08:20 PM

View Posttycos, on 13 February 2013 - 06:55 PM, said:

How about this:

Convert to a integer and it will never had a 0 at the start.


That is because an integer is a number value and will never begin with a 0. If you where going to tell me you had 10 bucks you would not say you had zero10 bucks. A string represents a collection of 1 or more printable characters in any order. You can put the string "0" in front of the string of the button`s text (lets say you pressed 3) but, when you converted the string "03" back into an integer which is a (number value) the leading 0 is dropped leaving you with a number value of 3. Then the number 3 is being assigned to the (txt_minutes) text. The only reason you would need to convert the string to an integer is if you wanted to perform math functions with the number value of the text.

Notice nothing is being converted to an integer:
    Private Sub number_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_0.Click, cmb_9.Click, cmb_8.Click, cmb_7.Click, cmb_6.Click, cmb_5.Click, cmb_4.Click, cmb_3.Click, cmb_2.Click, cmb_1.Click
        Try
            number_input = CType(sender, Button).Text
            txt_minutes.Text = txt_minutes.Text.Substring(txt_minutes.Text.Length - 1) & number_input
        Catch ex As Exception
            txt_minutes.Text = "Error"
        End Try
    End Sub



PS. If you have further questions please start a new thread.
Was This Post Helpful? 1
  • +
  • -

#8 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 14 February 2013 - 07:20 AM

You are entering number of minutes, I assumed some math would be done on it?
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 282
  • Posts: 892
  • Joined: 01-February 13

Re: Whats wrong with my code - Numeric Buttons entry

Posted 14 February 2013 - 08:20 AM

View Posttycos, on 14 February 2013 - 09:20 AM, said:

You are entering number of minutes, I assumed some math would be done on it?


I would guess at some point that will be done by getting the text and converting it to an integer just before doing math operations to it but, if you read his first post he explains that he wants a leading 0 to be shown when he presses the first number button. He also only wanted to display the last 2 numbers in the text. Your example did not do ether. If you have further comments then PM me so this thread is not full of useless info to OP looking at it for help. Thank You.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1