CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Feb 2010
Posts
11

Hello,
I got a text file with that kind of data

ALARM= ALARM1
ZONE= ZONE6
DESCRIPTION= NEED TO REPLACE

ALARM= ALARM5
ZONE= ZONE5
DESCRIPTION= OK FOR 2 YEAR

ALARM= ALARM9
ZONE= ZONE7
DESCRIPTION= NEED MAINTANENCE

When textfile contain "ALARM=" i can load alarm name to listbox with the code below and display
ALARM1
ALARM5
ALARM9 on the listbox

'+++++++++++++++++++++++++

Private Sub Command1_Click()
nFileNum = FreeFile
Open "C:\3.txt" For Input As nFileNum
lLineCount = 1
Do While Not EOF(nFileNum)
Line Input #nFileNum, sNextLine
sNextLine = sNextLine & vbCrLf
sText = sNextLine

If InStr(sText, "ALARM=") = 1 Then

List1.AddItem (Mid(sText, 7, (Len(sText) - 7)))

End If

Loop

Close nFileNum

End Sub

'++++++++++++++++++

What i want now is this function

Private Sub List1_Click()
text1.text= ?????????
End Sub

I mean ,when i click ALARM1 on list item i want to display
ALARM= ALARM1
ZONE= ZONE6
DESCRIPTION= NEED TO REPLACE

When i click ALARM9 on list item i want to display
ALARM= ALARM9
ZONE= ZONE7
DESCRIPTION= NEED MAINTANENCE

And so on, there is one space line between these group.

I have attach my sample project. That will be great if anyone can find the solution.

Many thanks

2. Member
Join Date
Apr 2009
Posts
394

## Re: ABOUT STRING READING FROM textfile

hmmm... from your code I bet the entries into the list box are not quite what you are expecting...
Code:
If Left(sNextLine, 6) = "ALARM=" Then
Redim Preserve MyArray(List1.ListCount) As String
MyArray(List1.ListCount) = sNextLine
Else
MyArray(List1.ListCount) = MyArray(List1.ListCount) & sNextLine
End If
Then...
Code:
Private Sub List1_Click()
Dim ForLoopCounter As Integer
For ForLoopCounter = 0 To List1.ListCount - 1
If List1.Selected(ForLoopCounter) = True Then
Text1.Text = MyArray(ForLoopCounter + 1)
Exit For
End If
Next ForLoopCounter
End Sub

Good Luck

3. Junior Member
Join Date
Feb 2010
Posts
11

## Re: ABOUT STRING READING FROM textfile

Many thanks for you reply, i was trying to compile your code and when button click i got

"compiler error: variable not defined " , highlight to this code "ReDim Preserve MyArray(List1.ListCount) As String"

Do you know what it could be problem?

Thanks

4. ## Re: ABOUT STRING READING FROM textfile

You need to modify YOUR code to his, or HIS code to yours. Can't mix variable names. It just double-spaces lines, but you can put YOUR If string inside of the loop. (Hint)

Here's two ways:

Code:
Option Explicit

Dim x As Integer, st As String, y As Integer
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
Open App.Path & "\to do.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ----------------- two ways to skin a cat --------------
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -------------------------------------------------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
Dim words() As String
For x = 0 To UBound(str)
words = Split(str(x)) ' Split into WORDS
For y = 0 To UBound(words)
st = st & str(x) & vbCrLf & vbCrLf ' one line for each word
Next y
Next x
MsgBox st
End Sub

5. Junior Member
Join Date
Feb 2010
Posts
11

## Re: ABOUT STRING READING FROM textfile

Thank for your info, the code you are providing is to display all lines from file to messagebox , as i mention on this thread and in my code sample i already got loading require items ( "ALARM1", "ALARM5" , "ALARM9" )on listbox.
What i want is, When i click "ALARM9" on listbox i want to display on textbox

ALARM= ALARM9
ZONE= ZONE7
DESCRIPTION= NEED MAINTANENCE """only"""

When i click "ALARM1" on listbox i want to display on textbox
ALARM= ALARM1
ZONE= ZONE6
DESCRIPTION= NEED TO REPLACE """only"""

When i click "ALARM5" on listbox i want to display on textbox
ALARM= ALARM5
ZONE= ZONE5
DESCRIPTION= OK FOR 2 YEAR """only"""

Thanks
Last edited by davidkhan; February 21st, 2010 at 08:34 AM.

6. ## Re: ABOUT STRING READING FROM textfile

I thought you'd get that part. If you want to display three things at once, put them on the same line! Separate them with a character that won't be used. / , * ~ are usually OK

7. Junior Member
Join Date
Feb 2010
Posts
11

## Re: ABOUT STRING READING FROM textfile

Sorry i am not cleared what you mean , can you explain more about it?

Thanks

8. ## Re: ABOUT STRING READING FROM textfile

You have this:

ALARM= ALARM1
ZONE= ZONE6
DESCRIPTION= NEED TO REPLACE

ALARM= ALARM5
ZONE= ZONE5
DESCRIPTION= OK FOR 2 YEAR

ALARM= ALARM9
ZONE= ZONE7
DESCRIPTION= NEED MAINTANENCE

If it looked like THIS (first):

ALARM= ALARM1/ZONE= ZONE6/DESCRIPTION= NEED TO REPLACE
ALARM= ALARM5/ZONE= ZONE5/DESCRIPTION= OK FOR 2 YEAR
ALARM= ALARM9/ZONE= ZONE7/DESCRIPTION= NEED MAINTANENCE
It'd be easy to parse, but if you omit the labels, you'd have this:

ALARM1/ZONE6/NEED TO REPLACE
ALARM5/ZONE5/OK FOR 2 YEAR
ALARM9/ZONE7/NEED MAINTANENCE
Which is what it should look like. Just parse CrLf, then "/"

9. Junior Member
Join Date
Feb 2010
Posts
11

## Re: ABOUT STRING READING FROM textfile

Thanks for your info, it is not possible to change because the file is about 30MB, i am using to with VB.net/CSHARP , it is OK, but now i want to combine with some more useful source code in VB and that is why try to translate in VB6. If you interest , i can sent you this VB.net project.

Thanks

10. ## Re: ABOUT STRING READING FROM textfile

I don't understand that. Use VB6 -or- VB.net and try again

11. ## Re: ABOUT STRING READING FROM textfile

I think I know, what you want. You want to store the additional info t an Alarm somewhere.
Why not use a collection. You declare a collection for the additional data and then you load like this:
Code:
Private Dim MoreData as Collection
Private Sub LoadFile(FileName As String)
dim a$, b$, c$List1.Clear Set MoreData = New Collection Open FileName For Input As #1 While Not EOF(1) Line Input #1, a$
If InStr(a$, "ALARM=") Then List1.AddItem Trim(Mid$(a$, 7)) Line Input #1, a$: b$=Trim(Mid$(a$,6)) Line Input #1, a$: c$=Trim(mid$(a$,13)) MoreData.Add b$ + "/" + c\$
End If
Wend
Close #1
End Sub
when an ALARM line is encountered, the Alarm is added to the listbox.
Then 2 more lines are read in, the values concatenated with "/" and added to the collection.
You have to watch out when retrieving the data, though. ListBox elements start with 0, Collections start with 1.
That is, List1.Listitem(0) will contain "ALARM1", but the according data is in
MoreData(1), which is "ZONE6/NEED TO REPLACE"

12. Junior Member
Join Date
Feb 2010
Posts
11

## Re: ABOUT STRING READING FROM textfile

Thanks for your info, I think i havn't seen the code to load on the Textbox corresponding to listbox, is that code loading to listbox items?

Anyway i have attach my sample BINARY(EXE) in .NET and sample data as well, so you will see exactly what the function look like.

Many thanks

13. Super Moderator Power Poster
Join Date
Jul 2008
Location
WV
Posts
4,975

## Re: ABOUT STRING READING FROM textfile

Shouldn't you be posting this is the .Net section if your program is .Net advise you get in VB6 will be different and may not work.

14. ## Re: ABOUT STRING READING FROM textfile

something you are doing :
Code:
 Private Sub listBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim num As Integer = Convert.ToInt32(Me.listBox1.SelectedIndex.ToString)
Dim strArray As String() = File.ReadAllLines(".//data1.csv")
Dim item As String = String.Empty
Dim str2 As String
For Each str2 In strArray
If ((str2.IndexOf("Package:") > -1) AndAlso (item.Length > 0)) Then
item = (str2 & ChrW(10))
Else
item = (item & str2 & ChrW(10))
End If
Next
Me.richTextBox1.Text = Me.entries.Item(num)
End Sub
and it seems as if you're doing something else when a toolstrip is reached (but I did not see it in your exe) :

Code:
Process.Start("WGET\wget.exe", ("http://gb.archive.ubuntu.com/ubuntu/" & Me.textBox1.Text))
Now, repeat : what are you looking for? Because even if I am amble to see the code you did not posted, I am not able to understand your needing

15. Junior Member
Join Date
Feb 2010
Posts
11

## Re: ABOUT STRING READING FROM textfile

Thanks for your reply , i already post complete code in VB.NET and at the top of forum when i start this thread, but people complain and i edit for that. The reason i post binary + sample is to undersatand people exactly what i mean. Even now i might need to delete this EXE as some people even private mail me and complain that this is not VB.NET forum.

This reflector generate code is exactly the same as original code

The only main thing i want is not when a toolstrip is reached , when mouse doubleclick, reading that csv data "BLOCK BY BLOCK to reach empty line of that block" and display on textbox, (that means not line counting , not ..........., data might contain 3 line, 4 line 100 lines in one BLOCK as like in the sample file i sent) , that is all i need .
the other function i don't need that.

Thanks
Last edited by davidkhan; February 25th, 2010 at 03:44 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

This is a Codeguru.com survey!