Can anyone figure this one out?

I wrote a dynamic array to resize based on user input, then transfer the input to a textbox. i used "Redim preserve" to keep the contents of the textbox from resetting each time more entries are made. The problem is that every time the input box is displayed to enter new items the textbox is still reset. And everytime i close the form out the contents of the textbox are lost. What am I doing wrong? Here is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PatientArea() As String
Dim Areas As Short
Dim i As Short
Dim result As String
Areas = InputBox("How many Patient Care Areas would you like to add?", "Patient Care Area")
If Areas > 0 Then ReDim Preserve PatientArea(Areas - 1)
For i = 0 To UBound(PatientArea)
PatientArea(i) = InputBox("Enter Patient Care Area(s)", "Patient Care")
result = result & PatientArea(i) & vbCrLf
Next

TextBox1.Text = result


End Sub

Comments

  • : I wrote a dynamic array to resize based on user input, then transfer the input to a textbox. i used "Redim preserve" to keep the contents of the textbox from resetting each time more entries are made. The problem is that every time the input box is displayed to enter new items the textbox is still reset. And everytime i close the form out the contents of the textbox are lost. What am I doing wrong? Here is my code:
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    : Dim PatientArea() As String
    : Dim Areas As Short
    : Dim i As Short
    : Dim result As String
    : Areas = InputBox("How many Patient Care Areas would you like to add?", "Patient Care Area")
    : If Areas > 0 Then ReDim Preserve PatientArea(Areas - 1)
    : For i = 0 To UBound(PatientArea)
    : PatientArea(i) = InputBox("Enter Patient Care Area(s)", "Patient Care")
    : result = result & PatientArea(i) & vbCrLf
    : Next
    :
    : TextBox1.Text = result
    :
    :
    : End Sub
    :
    If i understand what you are trying to do, you want to redim the array to the number that the person enters and then have each of those listed in the textbox...If that's not right, then don't read the rest :)
    Frist, everytime you call the procedure, you are reinitializing the array with the first line. You chould put the line
    Dim PatientArea() As String
    somewhere outside of the procedure, like in the form_load or InitializeComponents() procedures. That way, it will only be called once, and it won't reset everytime.
    Second, I assume you want to keep the original values in the Array when you redim without the user reinputting it in your inputbox. So before your need to create an Integer with the UBound of the array before you redim it. Something like:
    dim iOldUBound as Integer = UBound(PatientArea)
    Now that you have that, you can make the for statement that appends the new values with something like:
    dim x as integer
    for x = (iOldUbound+1) to UBound(PatientArea-1)
    PatientArea(x) = InputBox("Enter Patient Care Area(s)", "Patient Care")
    result = result & PatientArea(i) & vbCrLf
    Next

    finally, you can put the results in the TextBox:
    TextBox1.Text = result

    To be honest though, it seems like a difficult way to do it. What are you trying to do? there may be an easier way, like using a List Box.

    Also, when you close the form, all the information will not be kept, becaue the form is removed from memory, if you want to keep the information, you will need to store it on the hard drive or in the registry before you close the form.

    Hope this was helpful.

  • Thanks for your reply! Actually in the middle of troubleshooting the array I decided to switch to a listbox. Now the code is simply:

    Dim result as string ()

    result = inputbox ("Enter an Area","Add/Remove Areas")

    listbox1.items.add(result)

    end sub

    This does what I want it to, but its a little more simple. My goal is to give the user the ability to add/remove areas (like an application manager). Then I need to figure out the best way for the listbox value to be retained each time the application is closed. It also needs to transfer its value to a listbox on form1 (the main interface). I thought about adding the code to the form_load event but was not sure if this would work. I guess I could use trial and error, but any help you could give me would save tons of time! Thanks dude.
  • : Thanks for your reply! Actually in the middle of troubleshooting the array I decided to switch to a listbox. Now the code is simply:
    :
    : Dim result as string ()
    :
    : result = inputbox ("Enter an Area","Add/Remove Areas")
    :
    : listbox1.items.add(result)
    :
    : end sub
    :
    : This does what I want it to, but its a little more simple. My goal is to give the user the ability to add/remove areas (like an application manager). Then I need to figure out the best way for the listbox value to be retained each time the application is closed. It also needs to transfer its value to a listbox on form1 (the main interface). I thought about adding the code to the form_load event but was not sure if this would work. I guess I could use trial and error, but any help you could give me would save tons of time! Thanks dude.
    :

    Yeah, the List box will do a lot better for your purpose. From what I understand, you want the user to go to this form and choose an item from the list box, click ok, and then that selected Item be placed on the Main form. You could easily do this by using the .showDialog() method as opposed to .show(). the showDialog() method will open the form as a modal dialog box(basically means that it must be closed or hidden before the user can continue). This way, you can do something like this on the main form:

    Private Sub goToPicker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goToPciker.Click
    Dim oForm As New Picker()
    oForm.ShowDialog(Me)

    If oForm.isOk Then
    TextBox1.Text = oForm.ListBox1.SelectedItem
    End If
    oForm.Dispose()

    End Sub

    basically what is does it makes a new form from the Picker Class(the form you created) then shows it as a modal form with the main form(me) being the master of it. Then when the user closes it, you check to see if the isOK variable(this would be created on the modal form as a public variable, i.e. public isOK as boolean) was set to true. If it is, then you set the textbox on the main form to contain the same value as the selected Item of the list box on the Picker form.

    Now, on the modal form side, all you really need to do at design time is make an OK button and Cancel button. When each other them are clicked set the isOk variable to either true or false, and then hide the form.

    About saving the selected information when the application is closed, you need to add those values to a file that will be saved on the harddrive. This will take a ton of explaining. I would suggest finding something online or in a VB.NET book about reading and writing to files.

  • Thanks for the advice. I will probably write the contents of the listbox(form4) to a .txt file, then import the file to a listbox(form2.)
    This does what I want it too. My original thought was to do this but I was unsure about security. Do you know of the best way to secure data in a .txt file (to prevent accidental deletion, ect.)?
  • : Thanks for the advice. I will probably write the contents of the listbox(form4) to a .txt file, then import the file to a listbox(form2.)
    : This does what I want it too. My original thought was to do this but I was unsure about security. Do you know of the best way to secure data in a .txt file (to prevent accidental deletion, ect.)?
    :

    I know that cb.net has encryption classes, but I have never used them, that would be out of my league :) You can however set the files attributes to read-only and archive to protect it, and then when you need to modify it, turn off those attributes. I also change the file from .txt to something new like .lbf. This way Windows won't recognize it as a text file, and people will be even less likely to modify it.
  • Great Idea! I'll change the file .ext. You've been helpful, if you ever need any VBA excel tips let me know. Later.
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