Notepad database in VB

放肆的年华 提交于 2019-12-13 05:24:41

问题


I am completely new to VB.net and have only been learning in for a few weeks I am doing a project where i need to make an EPOS systems using notepad as a data base. I am able to make the values of the buttons appear in the list box, however I have numerous buttons all with different values but only the first value in the text box is appearing each time a different button is pressed. E.G When Heineken button pressed "Heineken €5.00" is displayed when Guiness button pressed "Heineken €5.00" is displayed

Any help is greatly appreciated!

Imports System.IO Public Class Form1

Private Sub btnHeineken_Click(sender As Object, e As EventArgs) Handles btnHeineken.Click
    Dim sr As IO.StreamReader = IO.File.OpenText("DATABASE.txt")
    'File DATABASE.TXT is the the debug folder

    Dim name As String

    Dim stock, price As Double
    name = sr.ReadLine

    stock = CDbl(sr.ReadLine)

    price = CDbl(sr.ReadLine)

    lstBox.Items.Add(name & "" & FormatCurrency(price))
    name = sr.ReadLine



End Sub

Private Sub BtnGuiness_Click(sender As Object, e As EventArgs) Handles BtnGuiness.Click
    Dim sr As IO.StreamReader = IO.File.OpenText("DATABASE.txt")
    'File DATABASE.TXT is the the debug folder

    Dim name As String

    Dim stock, price As Double
    name = sr.ReadLine

    stock = CDbl(sr.ReadLine)

    price = CDbl(sr.ReadLine)

    lstBox.Items.Add(name & "" & FormatCurrency(price))
    name = sr.ReadLine
End Sub

DATBASE.txt

Heineken
5.00
20
Guiness
4.50
50
Bulmers
5.00
25

回答1:


Both your methods have exactly the same code. Thus, they do exactly the same thing: They show the contents of the first entry in your text file.

If you want your methods to do different things, you need to put different code in them.

Unfortunately, putting arbitrary code in your methods won't make them do what you want. It looks like you already discovered that. So the next step is to take a more structured approach:

  1. Decide what your button click should do. It looks like you already did that: You want to display "Guiness €4.50" when the "Guiness" button is clicked.

  2. Next, think about how your program can do that. Apparently, that's where you are stuck. You have a text file with a list of entries, how do you get the one you want?

  3. Translate the result of step 2 (the "algorithm") in code.

You tried to do step 3 before step 2. That won't work, and that's the reason why your code doesn't work.

I suggest that you think really hard about step 2 (How do I find data in a text file? How would I do it if I had the file printed out in front of me and were searching for the data personally?), come up with an algorithm and then return here and ask a new question if you need help translating it to code.



来源:https://stackoverflow.com/questions/28670892/notepad-database-in-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!