Locked Incrementing by one

  • Thursday, April 12, 2012 1:14 PM
     
     

    Trying to teach myself some Visual Basic... and not sure what to do....

    I have:

     Private Sub copyButton_Click() Handles copyButton.Click
            System.IO.File.Copy(OpenFileDialog1.FileName, SaveFileDialog1.FileName)
            System.IO.File.Copy(OpenFileDialog1.FileName, SaveFileDialog2.FileName)
    End Sub

    which place two copies of a file into two specific folders. What I also what do to is add a incremental function on click. I found a couple of pieces of codes online, but they seem to contradict each other. What I want to do is when I click the copyButton, I want to increment a TextBox by one.

    Nate

All Replies

  • Thursday, April 12, 2012 1:33 PM
     
     Answered

    The posted code works? I think so.

    You can not increment a textbox because what is "xyz" plus one? :) If you replace it by a NumericUpDown control you don't have to care about invalid input and you can just write

       numupdowncontrol.value +=1

    for incrementing.


    Armin

    • Marked As Answer by igator210 Thursday, April 12, 2012 1:46 PM
    •  
  • Thursday, April 12, 2012 1:45 PM
     
     

    I'm so glad I found this forum.  I knew you couldn't increment 'text' box but i didn't think about using the NumericUpDown control. (There are a lot of controls I'm still learning all their purposes). The code I found online for using a text box talking about redefining the box and using integers and strings.

    Nate

  • Thursday, April 12, 2012 9:00 PM
     
     
    If you had a particular reason for using a textbox (and it doesn't seem that you do) then the code you found online would be used for making sure that the textbox can never contain "xyz" or anything else that would not make sense if you tried to add 1 to it.  If that is done properly it is very complex code.  Since the NUD already has all that code included as part of the control, it is the better choice.
  • Friday, April 13, 2012 5:25 PM
     
     

    I did found a code that works with text boxes inside a button

    TextBox1.Text = Val(TextBox1.Text) + 1


    • Edited by igator210 Friday, April 13, 2012 5:25 PM grammar
    •  
  • Friday, April 13, 2012 5:58 PM
     
      Has Code

    I did found a code that works with text boxes inside a button

    TextBox1.Text = Val(TextBox1.Text) + 1


    The majority of the .Net Framework class library consists of programming language independent classes that offer all you need in a consistent way. I call it the core Framework functionality.

    In addition, for lowering the inhibition threshold for VB6 programmers and also for simplifying the upgrade process, classes have been added that look and work like they did in VB6 (and before). One of them is the Val() function. I recommend not to use them anymore because...:

    • Most of them do not offer any additional functionality compared to the core Framework functions.
    • Many of them just call core Framework functions, so you can simply call them directly.
    • The VB specific functions are not always consistent with the core Framework functions. For example, VB often uses a 1-based index whereas it's 0-based everywhere else.
    • They have their own special behavior as it has been designed many years ago. The core Framework functions are much more recent and developed.

    So, in your case, I'd write...:

          Dim value As Integer
    
          If Integer.TryParse(TextBox1.Text, value) Then
             value += 1
             TextBox1.Text = value.ToString
          Else
             MessageBox.Show("Not a valid integer value.")
          End If
    


    Armin