Results 1 to 5 of 5

Thread: sub or function?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    I am trying to teach myself VB, so if it sound like I've no idea what I am talkin about....thats why.

    I have four text box's and four buttons next to them.
    I have one txtMain box. when the user types something into txtMain and then clicks any button the text is moved from txtMain to the corresponding text box for the button.
    (cmdOne moves txtMain.text to txtOne.text..ect.)
    it also clears the last text box so there is text only in one box at a time.
    I use a variable and for each button it looks first in the main text box and if there is something there it puts that string into a variable. then it puts that variable into the txtOne,txtTwo..ect.
    (this part works, I get an A++ for class)
    now what I want to do now is use a sub or funtion to set the variable then call (or however it is done) the function with the click of the various buttons.
    this is what the code for one button looks like. I had to copy this for all four buttons. How can I use a sub/function
    to make less code?

    Private Sub cmdOne_Click()

    If txtMain = "" Then 'check the txtMain box
    txtOne.Text = strMain
    Else
    strMain = txtMain.Text 'load variable strMain
    txtOne.Text = strMain 'load txtOne box

    End If 'clear txtbox's
    txtMain.Text = ""
    txtTwo.Text = ""
    txtThree.Text = ""
    txtFour.Text = ""
    End Sub

    Thanks to anyone that helps. (if you can even understand what I just said...
    pnj
    pnj

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Code:
    Private Sub fillTextBox (strText as String, txtBox as TextBox)
     
        'clear all of the text boxes
        txtMain.Text = ""
        txtOne.Text = "" 
        txtTwo.Text = "" 
        txtThree.Text = "" 
        txtFour.Text = "" 
    
        if strText = "" then
          txtBox.Text = strMain
        else
          strMain = strText      
          txtBox.Text = strText
        End if
    
    End Sub
    
    private sub command1_click()
    
        'pass the data in the txtMain, and the textBox to be changed
        filltextBox txtMain.Text, txtOne
    
    End Sub
    
    private sub command2_click()
    
        filltextBox txtMain.Text, txtTwo
    
    End Sub
    Iain, thats with an i by the way!

  3. #3
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    some type of help ...

    ok so all u want to do is have it in a function or sub so u can just say "call mywhatever" instead of having to copy that code to each button right? ... if so then do this:

    go to the code section of vb n then go to the menus of vb n click on Tools | Add Procedure ...

    then a box will come up n give ur procedure a name ... after that make the type (sub) n the scope (public) ... i'm not that good with subs n functions so that's y i'm telling u to put them as i'm telling u ...

    well after u do that n click on "ok" then u will see a new procedure be added:

    Public Sub whatever()

    End Sub

    now just place ur code in between those 2 lines ...
    n in ur command buttons just type "call whatever"

    change "whatever" to the name u want ...

  4. #4
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122
    Hi,

    you're trying to learn something? OK! First of all you have to learn that copy and paste of code is baaaaad!

    Try this:

    Make your four text boxes a control array and you command buttons too with corresponding index numbers.

    Then you use the follwoing code:

    Code:
    Option Explicit 'Has to be there allways!
    Dim strText As String ' Your text variable
    
    Private Sub Command1_Click(Index As Integer)
    Dim txt As TextBox
        
    If txtMain = "" Then
        txtMain = strText
    Else
        strText = txtMain
    End If
        
    'Clear text boxes
    For Each txt In Text1
        txt.Text = ""
    Next
    
    'set the one box
    Text1(Index) = strText
    
    End Sub
    Good luck and happy learning!
    Roger

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537

    Thanks all.

    wow! thanks for the quick response everybody.
    I have no idea what any of that code means...
    Hey Roger, I know cut/paste is bad but for now it is all I have. I cut/paste then analize the heck out of what I cut/paste then try and recreate the same thing with out cut paste...so maybe it(the code) sticks in my head.
    any ways.......
    thanks everyone.
    pnj
    pnj

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