Results 1 to 21 of 21

Thread: how to add in textbox?[resolve]

  1. #1

    Thread Starter
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20

    how to add in textbox?[resolve]

    hi its me again asking stupid question.
    just forgive me...i would like to ask this question hanging in my head...

    how to add in the textbox?
    for example when the user input 8+ 2 and press "enter" it gives a value of 10.

    a sort of simple calculator..

    thanks in advance..
    Last edited by LoNeR; Jun 19th, 2004 at 02:44 AM.

  2. #2
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460
    Tetxbox1.Text = 8+2

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Libero
    Tetxbox1.Text = 8+2
    Sorry but that is dangerous. It only works if Option Strict is Off.

    Use

    Textbox1.Text =str(8+2)
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I think you need something like that:

    Code:
     Dim i As Byte
            Dim str As String = TextBox2.Text
            Dim car As Char
            While i < str.Length
                car = str.Chars(i)
                If Char.IsSymbol(car) Then
                    Dim str1 As String = str.Substring(0, i)
                    Dim str2 As String = str.Substring(i + 1)
                    Dim dec1 As Decimal = Decimal.Parse(str1)
                    Dim dec2 As Decimal = Decimal.Parse(str2)
                    Dim res As Decimal
                    Select Case car
                        Case Is = "+"
                            res = dec1 + dec2
                        Case Is = "-"
                            res = dec1 - dec2
                        Case Is = "/"
                            res = dec1 / dec2
                        Case Is = "*"
                            res = dec1 * dec2
                    End Select
                    str = res.ToString
                    Exit While
                End If
                i += 1
            End While
            TextBox2.Text = str
        End Sub
    It's not fully tested and you have to be careful !!
    With more time, it's possible do better, obviously
    Good job
    Live long and prosper (Mr. Spock)

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    VB Code:
    1. Dim ans as integer     'or long, double, etc
    2.  
    3. ans = 8 + 2
    4.  
    5. Textbox1.Text = Cstr(ans)

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi alextyx,



    WOW! Do you use sledgehammers to crush olives over there
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Hi Taxes, happy to see you again
    Really, probably I miss the request, it's always the same problem with language I think. You have not idea in how many threads I don't dare to enter, bacuase I'm not sure about the request.
    Anyway, I think Loner wants to put its enter in a textbox and then have the result in another or in the same textbox, but also writing its entire formula in a textbox, too!!!
    So you can't start from 8+2 but from the string "8+2" (Textbox2.text in my example) . Meet the difference and let me know your impression
    If, once again, I missed the request.....then

    Loner, which was your request?
    Live long and prosper (Mr. Spock)

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi alextyx,

    You may have a point. In which case:

    textbox1.text=str(val(textbox2.text)+val(textbox3.text))
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Perhaps taxes, but I understood Loner wanted to write its formula, using a single textbox like the display of a calculator!
    This is the problem!
    If you use two textboxes for your input, then you have to use a button to define which kind of operation you want to perfom: adding, multipling, ecc.... isn't it?
    I think the problem was:

    I have in my textbox1.text (I use textbox2, to be original )the string "8+2" or "12,3*4" or...so on. Give me the result!

    Obviously if I'm the only who thought so, among many english people, probably I was wrong, but my approach is for that kind of problem.
    Live long and prosper (Mr. Spock)

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi alextyx,

    Yes. Valid comment and quite right. The problem you then have is the "Rules of Arithmetic".

    I think I will just use a calculator
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I think using a calculator is the right choice, too.
    I only tried to give an answer at Loner's question, or what I thought It was its question! But...if Loner confirm my line, dear Taxes, you will have to study italian, to be punished!
    Bye
    Live long and prosper (Mr. Spock)

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by alextyx
    I think using a calculator is the right choice, too.
    I only tried to give an answer at Loner's question, or what I thought It was its question! But...if Loner confirm my line, dear Taxes, you will have to study italian, to be punished!
    Bye
    That's not a bad idea. My favourite singer is Paverotti (I've even named my Rottweiller after him ) and I can't understand a word he sings.

    Wait a minute... do you mean the Latin I learned at school is no good over there now
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    First lesson: PavArotti not PavErotti!
    About latin....uhm...I don't know if it's useful know, perhaps it depends on the last Latin Service Pack you installed. If you kept your language system upgraded until the 16th century's releases, it should be good enough!
    bye
    Live long and prosper (Mr. Spock)

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by alextyx
    First lesson: PavArotti not PavErotti!
    About latin....uhm...I don't know if it's useful know, perhaps it depends on the last Latin Service Pack you installed. If you kept your language system upgraded until the 16th century's releases, it should be good enough!
    bye
    That's what I call my dog; PAV because he is a rotti
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  15. #15

    Thread Starter
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20
    halu guys thanks for the great idea and suggs..
    Perhaps taxes, but I understood Loner wanted to write its formula, using a single textbox like the display of a calculator!
    This is the problem!
    yes your right alex...i want the user to input any no. in the single textbox add,mul,div or sub... and when he press the enter key it gives a sufficient answer...
    simple calculator...

    tanx guys..

  16. #16
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    perhaps this.
    VB Code:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.       If e.KeyCode = Keys.Enter Then
    3.          Dim re As New Regex("\d+\s*(\*|\/|\+|\-)\s*\d+")
    4.          If Not (re.Match(TextBox1.Text)).Success Then
    5.             MessageBox.Show("invalid")
    6.          Else
    7.             Dim arr() As String = Regex.Split(TextBox1.Text, "\s*[*,/,+,-]\s*")
    8.             If (Regex.Match(TextBox1.Text, "\s*\+\s*")).Success Then
    9.                TextBox1.Text = Integer.Parse(arr(0)) + Integer.Parse(arr(1))
    10.             ElseIf (Regex.Match(TextBox1.Text, "\s*\-\s*")).Success Then
    11.                TextBox1.Text = Integer.Parse(arr(0)) - Integer.Parse(arr(1))
    12.             ElseIf (Regex.Match(TextBox1.Text, "\s*\/\s*")).Success Then
    13.                TextBox1.Text = Integer.Parse(arr(0)) / Integer.Parse(arr(1))
    14.             ElseIf (Regex.Match(TextBox1.Text, "\s*\*\s*")).Success Then
    15.                TextBox1.Text = Integer.Parse(arr(0)) * Integer.Parse(arr(1))
    16.             End If
    17.          End If
    18.       End If
    19.    End Sub
    i don't know if this is the efficient one.

  17. #17

    Thread Starter
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20
    tanx monk...it really helps..


    more power...

    BTW what is this???
    Usa ako ka tawo. Bakakon nga tawo.
    Di ka kauyon nako pero usahay
    ako ang tao nga imong mauyonan. Ngano?
    Diman gud ko mosulti sa tinuod.


  18. #18
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    it means.
    i'm a person, a dishonest one.
    you won't like me but sometimes
    you like me most. why?
    cause i don't tell the truth exactly.
    it's a bisayan phrase. my native tongue.

  19. #19

    Thread Starter
    Registered User LoNeR's Avatar
    Join Date
    Jun 2004
    Location
    Heliom Prime
    Posts
    20
    Originally posted by brown monkey
    it means.

    it's a bisayan phrase. my native tongue.
    thanks monk...

    im just curious and a stupid one..

  20. #20
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Taaaaaxeeeessss....yu-hu.....did you buy the dictionary?
    Good morning to all you, dear friends. Excuse me (and Taxes). In this thread we have jocked a little. Interesting code B.M., it seems better than mine. I have to study the use of regex!
    Live long and prosper (Mr. Spock)

  21. #21
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    bisaya dictionary? btw, thanx alextyx. well, i'm proud to be a bisaya [filipino]

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