Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can I make a prog loop on a button click?

Jazmine123Jazmine123 Member Posts: 3
Im takin my first visual basic class and im stuck on how to finish an assignment. I need my program to open a file when i press the start button. And read the first name off the file and display it in a pic box. Then I need to be able to type into two text boxes and have it save that data into a new file when i push the button again. It also needs to read the second line in the file and display the name in the picbox so i can update the file with the text boxes again. It needs to do this until EOF. Can anyone give me a hint? Ive been banging my head on the desk since yesterday tryin everything I can think of. Thank you so much!

jazmine1234@yahoo.com

Comments

  • MalcolmMalcolm Member Posts: 828
    [b][red]This message was edited by Malcolm at 2002-10-29 12:36:20[/red][/b][hr]
    : Im takin my first visual basic class and im stuck on how to finish an assignment. I need my program to open a file when i press the start button. And read the first name off the file and display it in a pic box. Then I need to be able to type into two text boxes and have it save that data into a new file when i push the button again. It also needs to read the second line in the file and display the name in the picbox so i can update the file with the text boxes again. It needs to do this until EOF. Can anyone give me a hint? Ive been banging my head on the desk since yesterday tryin everything I can think of. Thank you so much!
    :
    : jazmine1234@yahoo.com
    :


    I'm not actually sure on how to write the text to a picture box, but you could get the info like this:

    Put this in the declarations section of your form:

    [code]Private ItemCount As Long
    Private TextWritten As String[/code]

    Then In The click Event of your Command button put this code:

    [code]Dim RemData As String, ICount As Long
    Open FileName For Input As #1
    Do While EOF(1) = False
    Input #1, RemData
    If ICount = ItemCount Then
    TextWritten = RemData
    'Write the contents of RemData to the picture box here
    ItemCount = ItemCount + 1
    Close #1
    Exit Do
    Else
    ICount = ICount + 1
    End If
    Next
    Close #1[/code]

    That hasn't been tested, but should be enough to get you started

    Then to save data to the file.

    [code]Open FileName For Append #1 'You should open it for Output if you want to start a new file.
    Write #1, Text1.Text
    Write #1, Text2.Text
    Write #1, TextWritten 'Writes the current contents of the picture box to the file.
    Close #1[/code]


  • Jazmine123Jazmine123 Member Posts: 3
    I dont think thats quite what Im looking for..

    I have this so far:

    Open "C:TEMPAVERAGE.TXT" For Input As #1
    Open "C:TEMPTEMP.TXT" For Output As #2

    Do While Not EOF(1)
    Input #1, name, Bats, Hits
    picName.Print name
    txtBat.SetFocus

    Bats = Bats + Val(txtBat.Text)
    Hits = Hits + Val(txtHits.Text)
    Write #2, name, Bats, Hits

    Count = Count + 1
    Loop
    Close

    I left out some non important stuff but I think you can see the general thing.

    The average.txt file looks something like
    "Bob",0,0
    "John",0,0
    etc
    where the first zero is the bats and the second is the hits. My prog is posed to update those zeros.

    I think I need some sort of nested loop, but I dont know if I can make it loop when i click the button. Its supposed to store the data and pull up the next entry when you click the button.

    Im so confused :-)

  • MalcolmMalcolm Member Posts: 828
    [b][red]This message was edited by Malcolm at 2002-10-29 12:56:55[/red][/b][hr]
    : I dont think thats quite what Im looking for..
    :
    : I have this so far:
    :
    : Open "C:TEMPAVERAGE.TXT" For Input As #1
    : Open "C:TEMPTEMP.TXT" For Output As #2
    :
    : Do While Not EOF(1)
    : Input #1, name, Bats, Hits
    : picName.Print name
    : txtBat.SetFocus
    :
    : Bats = Bats + Val(txtBat.Text)
    : Hits = Hits + Val(txtHits.Text)
    : Write #2, name, Bats, Hits
    :
    : Count = Count + 1
    : Loop
    : Close
    :
    : I left out some non important stuff but I think you can see the general thing.
    :
    : The average.txt file looks something like
    : "Bob",0,0
    : "John",0,0
    : etc
    : where the first zero is the bats and the second is the hits. My prog is posed to update those zeros.
    :
    : I think I need some sort of nested loop, but I dont know if I can make it loop when i click the button. Its supposed to store the data and pull up the next entry when you click the button.
    :
    : Im so confused :-)
    :
    :

    You just need to make some changes to the code I gave. Put the same two variabes I posted in the declaration section of your form. Then code it like this:

    [code]Dim name As String, bats As Long, hits As Long, ICount As Long
    Open "C:TEMPAVERAGE.TXT" For Input As #1
    Open "C:TEMPTEMP.TXT" For Output As #2
    Do While EOF(1) = False
    Input #1, name, Bats, Hits
    If ICount = ItemCount Then
    TextWritten = name
    picName.Print name
    txtBat.SetFocus
    Bats = Bats + Val(txtBat.Text)
    Hits = Hits + Val(txtHits.Text)
    Write #2, name, Bats, Hits
    ItemCount = ItemCount + 1
    Close #2
    Close #1
    Exit Do
    Else
    ICount = ICount + 1
    End If
    Loop
    Close #2
    Close #1

    [/code]

    I still haven't tested that



  • Jazmine123Jazmine123 Member Posts: 3

    : You just need to make some changes to the code I gave. Put the same two variabes I posted in the declaration section of your form. Then code it like this:
    :
    : [code]Dim name As String, bats As Long, hits As Long, ICount As Long
    : Open "C:TEMPAVERAGE.TXT" For Input As #1
    : Open "C:TEMPTEMP.TXT" For Output As #2
    : Do While EOF(1) = False
    : Input #1, name, Bats, Hits
    : If ICount = ItemCount Then
    : TextWritten = name
    : picName.Print name
    : txtBat.SetFocus
    : Bats = Bats + Val(txtBat.Text)
    : Hits = Hits + Val(txtHits.Text)
    : Write #2, name, Bats, Hits
    : ItemCount = ItemCount + 1
    : Close #2
    : Close #1
    : Exit Do
    : Else
    : ICount = ICount + 1
    : End If
    : Loop
    : Close #2
    : Close #1
    :
    : [/code]
    :
    : I still haven't tested that
    :
    :
    :
    Will this update the data for each individual record? I dont see how it will and I dont really understand how to do it to try it. When the file is open before the program it reads
    "Jennifer",0,0
    "Bo",0,0
    after i run the program i want it to read
    "Jennifer",5,3
    "Bo",2,0
    or something to that affect.
    They way i have it now it puts the numbers on everyones all at once like
    "Jennifer",5,3
    "Bo",5,3

    Ive never used Exit Do.. im assuming it ends the do not starts it over, correct? Im feeling rather dull today.
    But I dont see where i get to input the data for the next record. And I dont see where it starts the next record on the button click.
    Maybe I just need a nap.

    Thank you for your help

  • MalcolmMalcolm Member Posts: 828
    :
    : : You just need to make some changes to the code I gave. Put the same two variabes I posted in the declaration section of your form. Then code it like this:
    : :
    : : [code]Dim name As String, bats As Long, hits As Long, ICount As Long
    : : Open "C:TEMPAVERAGE.TXT" For Input As #1
    : : Open "C:TEMPTEMP.TXT" For Output As #2
    : : Do While EOF(1) = False
    : : Input #1, name, Bats, Hits
    : : If ICount = ItemCount Then
    : : TextWritten = name
    : : picName.Print name
    : : txtBat.SetFocus
    : : Bats = Bats + Val(txtBat.Text)
    : : Hits = Hits + Val(txtHits.Text)
    : : Write #2, name, Bats, Hits
    : : ItemCount = ItemCount + 1
    : : Close #2
    : : Close #1
    : : Exit Do
    : : Else
    : : ICount = ICount + 1
    : : End If
    : : Loop
    : : Close #2
    : : Close #1
    : :
    : : [/code]
    : :
    : : I still haven't tested that
    : :
    : :
    : :
    : Will this update the data for each individual record? I dont see how it will and I dont really understand how to do it to try it. When the file is open before the program it reads
    : "Jennifer",0,0
    : "Bo",0,0
    : after i run the program i want it to read
    : "Jennifer",5,3
    : "Bo",2,0
    : or something to that affect.
    : They way i have it now it puts the numbers on everyones all at once like
    : "Jennifer",5,3
    : "Bo",5,3
    :
    : Ive never used Exit Do.. im assuming it ends the do not starts it over, correct? Im feeling rather dull today.
    : But I dont see where i get to input the data for the next record. And I dont see where it starts the next record on the button click.
    : Maybe I just need a nap.
    :
    : Thank you for your help
    :
    :

    Remember to include the two variables I said to put in the declarations section of the form. Then each time you press the command button it will update the next record in the file.

    You are correct in regards to Exit Do Statement.

    The code gets the data from the two textboxes. you'll have a chance to change the contents of the textboxes inbetween button clicks.

    Try the code with a couple of backup files (so that you don't loose data out of the original file), in debug mode. I think you'll see what I'm trying to do this way.



  • Andre YoungAndre Young USAMember Posts: 0

    _______ \ http://forcoder.org \ free ebooks and video tutorials about < R, Objective-C, Assembly, Ruby, Visual Basic, MATLAB, Java, Scratch, Swift, Python, C, JavaScript, Go, Visual Basic .NET, Perl, C#, PHP, PL/SQL, C++, Delphi Apex, COBOL, ML, Erlang, Rust, FoxPro, Alice, Fortran, Scala, SAS, Scheme, Julia, Logo, ABAP, LabVIEW, Hack, Crystal, Clojure, Transact-SQL, Awk, Prolog, F#, Kotlin, Ada, Lua, Lisp, D, Dart, VBScript, Bash /> ____

Sign In or Register to comment.