Array and Listbox

Im new to programming please help me.
I

Comments

  • seancampbellseancampbell Pennsylvania, USA
    What exactly do you need?

    So basically you want to create a program that opens a Text file, inputs the text into a Multiline Texbox, make changes, click Update and have those changes be saved? Or are you doing something else?

    Please elaborate on what you are trying to do and I will be glad to assist.
  • : What exactly do you need?
    :
    : So basically you want to create a program that opens a Text file,
    : inputs the text into a Multiline Texbox, make changes, click Update
    : and have those changes be saved? Or are you doing something else?
    :
    : Please elaborate on what you are trying to do and I will be glad to
    : assist.

    Basically i want to design a membership Name and Telephone List form using the following command buttons.

    Edit
    Add New;
    Update;
    Delete;
    Cancel;
    Save;
    Clear, and
    Exit

    The names and phone numbers of members are stored in a sequential text file called MEMBERS.TXT which i have created.

    When the program is loaded, names and telephone numbers should be read from the file and stored in an array or arrays in memory and then, from memory, the names and telephone numbers are displayed in alphabetical names sequence in a list box.


    I used two textboxs to hold the Name and the other to hold the Number

    When a name in the listbox is selected (clicked) the name and corresponding phone number should be should be displayed in the textboxes but i cant manage to do this.

    To change any fields, the user first uses the Edit button to enable the textboxes to be changed.

    The user makes the amendments to the textboxes and uses the update button to effect the change in memory. I need your help on this too.

    To delete an item, Delete is used. But i dont know how to do this using sequantial Files. Thanks for your help.




  • seancampbellseancampbell Pennsylvania, USA
    Is this for a project at school? If you are doing this for a school project I will be more then willing to help you arrive at an answer by answering questions, but I will not write the code for you it won't help you learn.

    I did create a mock program do perform exactly what you want it to do, with 2 text boxes and a list box full of each member on file. I am more than willing to share it with you, but it uses a different method of writing files then you have set up in yours, and it also uses a bit more advanced programming, so if your looking to learn for school it might not be the way to go...

    Let me know your intentions, I am more than willing to walk you through writing one step by step if you want to learn

  • I'll be more than happy for you walk me though your moch program. I really want to learn and not just to finish the project and one again thanks.
  • seancampbellseancampbell Pennsylvania, USA
    Well here goes:

    Typically when I am assigned a new project, I break it down into what parts I am going to need to complete it. For your problem statement I determined that I would need 2 Labels (to designate which text box is which) 2 Text boxes (for entering MemberName and MemberNumber) a ListBox (I like to use Listviews myself, but I am using a listbox since that was what you said you were using) and finally 8 buttons, one for each operation you wanted to do.

    Now that we have drawn those objects onto our form, and before we write any code, lets put some proper names on the Controls so we don't get confused as the project gains more complexity (this program is rather simple, if I didn't use good naming schemes, my 2 year project at work would really be difficult to work on):

    when naming controls, try to designate what control it is with the name by adding three lower case letters to the begining. I named the objects as followed:
    txtMemberName, txtMemberNumber, lblMemeberName, lblMemberNumber, lstNames, btnEdit, btnAdd, btnUpdate, btnDelete, btnCancel, btnSave, btnClear, btnExit.

    Now that we have our form drawn up, lets take a crack at coding functionality.

    So I wrote a few custom Subroutines (if you didn't know how to do this, type in Sub Bob() and hit enter [should auto put in the end sub] and now you can call the code in the Bob subroutine from anywhere else on the form (Form1_Load etc). This comes in handy when you have a few lines of code that will get repeated a lot such as the code to repopulate the listBox.

    Read the commenting under each function for the specifics, but here is a breif description of each function I wrote:

    ----
    grabData(fileName as String) - This function is used to grab the names and numbers out of the fileName supplyed. If that filename does not exist the program will crash. I created a file with 2 lines in it, first line was name, second line was number and named it MEMBERS.TXT and saved it to the bin folder of my project to avoid the crash. If you want to fix the crash, you need to handle for no data in the fillListBox function.

    Note on grabData - If you notice, I use the ReDim Preserve keywords on my two arrays (at the top they are declared as MemberName(0) and MemberNumber(0) which normally meens they can only hold 1 value in cell (0). Well since I wanted this program to be able to handle any number of members, the ReDim Preserve Array(Array.Length) adds on a new cell. Heres how:

    When I declare an array i do this Dim Arr(10) as integer, I can now store values in Arr(0), Arr(1) ... Arr(10). So the Length of Arr is 11. So if I say ReDim Arr(Arr.Length) I am really saying ReDim Arr(11) which increases the size by 1.

    Normally when you do a reDim it erases the values that used to be in the array, but when you include the Preserve keyword, it saves them.

    ----
    writeVarsToFile(MName() as String, MNumber() as String, fileName as string) - This function writes sequentially writes each MemberName and MemberNumber from the two arrays supplied to it to the fileName you pass it.

    ----
    fillListBox(MName() as String, LB as ListBox) - This function fills the ListBox you pass it with every name in the passed MName array. Assuming that there is a MemberNumber for each MemberName, we will use ListBox.SelectedIndex in the end to know which item in the array we have selected.

    ----
    removeMember(index as integer) - This function removes a member from the array. Note: You have to save the file with the save button to update the text file for any changes. If you notice, to remove an item, I just shift every value up one by saying Array(x) = Array(x+1) at the end, I use ReDim Preserve to shrink the array length down by 1 and remove the now duplicated value at the end

    ----
    addMember(MName as String, MNumber as String) - This function simply adds a new cell to the array and puts Mname and Mnumber in.

    ----
    The rest of the code is event driven.

    On Load I read the text file and populate the Listbox with the data now in memory (I am not handling for the file not existing, you should add that)

    When the listbox calls SelectedIndexChanged (when you select something new) the Textboxes up top are updated to display the information underneath the arrays at that selected index (since we sync up the MemberName array with the listBox items, SelectedIndex 0 = Array(0).

    When the user clicks Add New, the text boxes are enabled and cleared. You can add in a new name and number and click the add New button again to add it to the list. If you look at the code under the add new button, I set a boolean called AddNewMode to true on the first click, and set it back to false on the second click.

    When a user selects a new item on the listbox, it sets AddNewMode and EditMode to false incase you were in the middle of making changes and decided to stop.


    I hope this program helps (code included in next post). I didn't finish all the buttons code because I am not sure exactly what each button needs to do, but you can probably just modify this code to get it to do everything you need.

    If you have anymore questions, or don't understand any parts of this code, just reply back I will gladly and indepthly explain any part of it to help you learn.

    Sean Campbell - Programmer / Musician / Life Philosopher
    check out my Music site http://www.jamjunky.com/firesickle
  • seancampbellseancampbell Pennsylvania, USA
    [code]

    Dim MemberName(0) As String
    Dim MemberNumber(0) As String
    Dim EditMode As Boolean = False
    Dim AddNewMode As Boolean = False
    Dim appPath As String 'needed for application Startup folder

    Sub grabData(ByVal FileName As String)

    'This subroutine grabs Name and Number information
    'from the supplyed fileName and inserts them into Dynamic
    'Arrays

    Dim FF As Integer = FreeFile()
    'FreeFile provides an unused Integer to use
    'for a FileOpen command

    Try
    'Try keep program from crashing whenever an error occurs in the
    'contained code. Underneath Catch I put in some MsgBox code to display
    'any errors that occur.

    FileOpen(FF, FileName, OpenMode.Input)
    'This opens the File FileName in input mode (which allows me to read
    'each line of text out of the file into strings)

    Do While Not EOF(FF)
    'This loop runs until we reach the end of FileNum FF (same filenum as fileopen)
    'The Input statements grab a whole line as an object
    'and stick
    Input(FF, MemberName(MemberName.Length - 1))
    Input(FF, MemberNumber(MemberNumber.Length - 1))

    If Not EOF(FF) Then
    ReDim Preserve MemberName(MemberName.Length)
    ReDim Preserve MemberNumber(MemberNumber.Length)
    'I will be sure to explain the reDims in my post
    'this(part Is tricky)
    End If
    Loop

    FileClose(FF)

    Catch ex As Exception
    MsgBox(ex.Message)
    End Try

    End Sub

    Sub writeVarsToFile(ByVal MName() As String, ByVal MNum() As String, ByVal FileName As String)

    'This subroutine writes all values in MName and MNum into the specified file
    'as text.
    'It will insert MName(0) on the first line MNum(0) on the next
    ' MName(1) on the next ETC.

    Dim FF As Integer = FreeFile()

    Try

    FileOpen(FF, FileName, OpenMode.Output)

    Dim i As Integer = 0
    'We will use i to designate where we are at in the Arrays

    Do While i < MemberName.Length
    PrintLine(FF, MemberName(i))
    PrintLine(FF, MemberNumber(i))
    'PrintLine writes the variable(s) to file (without quotes)
    ' and inserts a LineFeed after it
    i += 1 'incriment i by 1
    Loop

    FileClose(FF)

    Catch ex As Exception
    MsgBox(ex.Message)
    End Try

    End Sub

    Sub fillListBox(ByVal MName() As String, ByVal LB As ListBox)
    Dim i As Integer
    LB.Items.Clear()
    For i = 0 To MName.Length - 1
    LB.Items.Add(MName(i))
    Next
    End Sub

    Sub removeMember(ByVal index As Integer)

    Dim i As Integer
    For i = index To MemberName.Length - 2
    'Remove data by shifting everything up one index
    MemberName(i) = MemberName(i + 1)
    MemberNumber(i) = MemberNumber(i + 1)
    Next
    'take off empty array cell
    'Preserve keyword keeps old values from being erased
    ReDim Preserve MemberName(MemberName.Length - 2)
    ReDim Preserve MemberNumber(MemberNumber.Length - 2)

    fillListBox(MemberName, lstNames) 'refresh data in listBox

    End Sub

    Sub addMember(ByVal MName As String, ByVal MNumber As String)
    'Add on a new cell to end of array
    ReDim Preserve MemberName(MemberName.Length)
    ReDim Preserve MemberNumber(MemberNumber.Length)
    'Add in the next values
    MemberName(MemberName.Length - 1) = MName
    MemberNumber(MemberNumber.Length - 1) = MNumber

    fillListBox(MemberName, lstNames) 'refresh data in listbox

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'These two functions set appPath to be the fully qualified
    'path name of where the application is running at

    'If you are running the program in debug mode, this path
    'is the Bin folder inside of your project folder

    'On my machine this var is C:My VS ProjsProjectNameBin
    appPath = Application.StartupPath
    If appPath.Substring(appPath.Length - 1) <> "" Then appPath &= ""
    ' String &= "Text" is an append function. &= adds the Concatinates onto the end.

    'On Load we want to initialize the Form's Dataset
    grabData(appPath & "MEMBERS.TXT")
    fillListBox(MemberName, Me.lstNames) 'Pass the function Membername array


    'These will be enabled in AddNew mode and in EditMode
    txtMemberName.Enabled = False
    txtMemberNumber.Enabled = False

    End Sub

    Private Sub lstNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNames.SelectedIndexChanged
    'This function is called whenever the user changes what is selected in listbox
    If lstNames.SelectedIndex < MemberName.Length Then
    'This function makes sure that the item selected in the listbox
    'is not a higher index then what items the MemberName array has
    'This case should not be common, but it is best to include it to keep
    'the program from crashing if it did
    txtMemberName.Text = MemberName(lstNames.SelectedIndex)
    txtMemberNumber.Text = MemberNumber(lstNames.SelectedIndex)

    'When the user selects something on the lstBox, set modes = false
    'and disable textboxes
    txtMemberName.Enabled = False
    txtMemberNumber.Enabled = False
    AddNewMode = False
    EditMode = False
    End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    If AddNewMode = False Then
    txtMemberName.Text = ""
    txtMemberNumber.Text = ""
    txtMemberName.Enabled = True
    txtMemberNumber.Enabled = True
    txtMemberName.Focus()
    AddNewMode = True
    Else
    addMember(txtMemberName.Text, txtMemberNumber.Text)
    txtMemberName.Enabled = False
    txtMemberNumber.Enabled = False
    AddNewMode = False
    End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    If EditMode = False Then
    txtMemberName.Enabled = True
    txtMemberNumber.Enabled = True
    txtMemberName.Focus()
    EditMode = True
    Else
    If lstNames.SelectedIndex < MemberName.Length Then
    MemberName(lstNames.SelectedIndex) = txtMemberName.Text
    MemberNumber(lstNames.SelectedIndex) = txtMemberNumber.Text
    End If
    txtMemberName.Enabled = False
    txtMemberNumber.Enabled = False
    EditMode = False
    End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    writeVarsToFile(MemberName, MemberNumber, appPath & "MEMBERS.TXT")
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    If lstNames.SelectedIndex < MemberName.Length Then
    removeMember(lstNames.SelectedIndex)
    End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    txtMemberName.Enabled = False
    txtMemberNumber.Enabled = False
    AddNewMode = False
    EditMode = False
    lstNames.SelectedIndex = 0
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    End 'just End closes the Application
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    End Sub
    [/code]
  • did you get it done need the same help.
  • Hi

    I see you did this program quite some time ago but im hoping you can help me. I took pieces of your code and added it to mine where my code wasnt working but now everytime i try to run my program it gives me an error msg stating that it couldnt find my folder members.txt. Do you possibly know what could be wrong and how to fix it?

    Thanks
    Kirsten

  • seancampbellseancampbell Pennsylvania, USA
    Hi Kirsten,

    I would love to help you figure out what is wrong with your code, but you'll have to copy and paste it into a post for me to read it and see whats wrong.

    I'd suggest starting a new topic since this one is from May 2007 and has a load of replys already.

    Look forward to helping,
    Firesickle.com
Sign In or Register to comment.

Howdy, Stranger!

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

Categories