Jump to content
  • Advertisement
Sign in to follow this  
inmysights

[.net] VB.net Left_button

This topic is 3129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a button that I have labeled left and when I click it I want it to go backwards through my array. What happens is it goes forward all the way to 10 and then pops my message box. any ideas what I am doing wrong? Thank you Private Sub LeftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftButton.Click 'read name age and DOB from file Dim FieldString() As String 'must be string array from delimited fields 'countbackwards through the array For BackInteger As Integer = 10 To 1 Step -1 If Not NADTextFieldParser.EndOfData Then FieldString = NADTextFieldParser.ReadFields() 'first field NameTextBox.Text = FieldString(0) 'second box AgeTextBox.Text = FieldString(1) 'third box DOBTextBox.Text = FieldString(2) '4th box GroupBuffBox.Text = FieldString(3) Else MessageBox.Show("At the first record in the file.", "End of Data") End If Next BackInteger End Sub

Share this post


Link to post
Share on other sites
Advertisement
NADTextFieldParser.ReadFields isn't paying any attention to BackInteger (why should it?) - it is just reading fields out from the file in its usual first-to-last line order.

You might want to read all the lines into a list first, then iterate over it from end to start, something like this:

Dim Fields As New ArrayList()

While Not NADTextFieldParser.EndOfData
Fields.Add(NADTextFieldParser.ReadFields())
End While

For I As Integer = Fields.Count - 1 To 0 Step -1
NameTextBox.Text = Fields(I)(0)
AgeTextBox.Text = Fields(I)(1)
DOBTextBox.Text = Fields(I)(2)
GroupBuffBox.Text = Fields(I)(3)
MessageBox.Show("Record displayed.")
Next I

MessageBox.Show("All records displayed.")

Share this post


Link to post
Share on other sites
Thank you for replying.

I added the code and tried to see if it worked. the text box pops and never goes backward.

I have the text file loading on the form load and just using it that way. The forward button works fine.

I will add it maybe it will help. No clue why I can just tell it to go back one array sub

Here is my right button code:

Private Sub RightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightButton.Click
'read name age and DOB from file
Dim FieldString() As String 'must be string array from delimited fields
If Not NADTextFieldParser.EndOfData Then
FieldString = NADTextFieldParser.ReadFields()
'first field
NameTextBox.Text = FieldString(0)
'second box
AgeTextBox.Text = FieldString(1)
'third box
DOBTextBox.Text = FieldString(2)
'4th box
GroupBuffBox.Text = FieldString(3)
Else
MessageBox.Show("No more records to display.", "End of Data")
End If
End Sub

Share this post


Link to post
Share on other sites
Ah, I see, you want to be able to browse left and right through your file's records.

In that case, you'll need to load the records into a list on form creation, and also initially set a 'location' variable to 0.


Private Fields As ArrayList()
Private Location As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Fields = New ArrayList()
Location = 0

' get your reference to your NADTextFieldParser instance here

While Not NADTextFieldParser.EndOfData
Fields.Add(NADTextFieldParser.ReadFields())
End While
End Sub



Add a routine for loading the current record (as specified by the 'location' variable) into the form:

Private Sub DisplayRecord()
Dim Field As String()
Field = CType(Fields(Location), String())

NameTextBox.Text = Field(0)
AgeTextBox.Text = Field(1)
DOBTextBox.Text = Field(2)
GroupBuffBox.Text = Field(3)
End Sub



Then, for left and right button click event handlers, you can just check to see whether you could move the 'location' variable further left or right, and if so, adjust it and load the new data into the form.


Private Sub LeftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftButton.Click
If Location > 0 Then
Location -= 1
DisplayRecord()
Else
MessageBox.Show("This is the first record!")
End If
End Sub

Private Sub RightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightButton.Click
If Location < Fields.Count - 1 Then
Location += 1
DisplayRecord()
Else
MessageBox.Show("This is the last record!")
End If
End Sub

Share this post


Link to post
Share on other sites
I wanna thank you!

that worked perfectly!

I did have to change
Private Fields As ArrayList()
to
Private Fields As New ArrayList()

I was going about it all wrong, thank you so much!

Share this post


Link to post
Share on other sites
Quote:
Original post by inmysights
Private Fields As ArrayList()
to
Private Fields As New ArrayList()

Ah yeah.. it has been a while since I've written VB(.NET) :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!