|
-
Jun 20th, 2009, 09:20 AM
#1
Thread Starter
New Member
[RESOLVED] Open .txt in RichTextBox
Hi All, new member here..
Basically, I have .txt file with data as below:
moref:800
name:nox-A
uuid:564d3ae6-5c80-79bb-daee-60b0215df8ee
ipaddr:192.168.1.12
moref:810
name:nox-B
uuid:564d29e7-d264-b779-870c-f402936b41dc
ipaddr:192.168.1.14
moref:832
name:nox-C
uuid:564d943d-0801-7f3d-0297-f428999e3619
ipaddr:192.168.1.16
I want to open this .txt in RichTextBox with some filtering via combobox. Let say, If I choose "name" from combobox, richtextbox only display :
nox-A
nox-B
nox-C
If ipaddr via combobox :
192.168.1.12
192.168.1.14
192.168.1.16
I know how to open via richtextbox but donno how to filter it. Hope someone can guide me on how to achieve this.
Thanks
-
Jun 20th, 2009, 09:34 AM
#2
Re: Open .txt in RichTextBox
Welcome to the forums.
I am assuming the file is not huge? If so, I think you may want to do the following:
1. Read your file and store it in a UDT (user-defined type) structure array
2. Add the IPs to the combobox and use the combo's .ItemData property to cross-reference its items to the UDT array.
Tasks include creating a UDT array, opening file, reading it, populating the UDT from the file, appending IPs to the combobox and using its .ItemData member. Are you comfortable with these? If not, please indicate what you don't know how to do.
There are other methods that can achieve what you want, but I think what I mentinoed is the easiest. If you file is huge, you may want to consider other methods.
P.S. Your UDT might look like:
Code:
Private Type USERSTRUCT
MoreF As Long
Name As String
UUID As String
IPAddr As String
End Type
Dim theUsers() As USERSTRUCT
-
Jun 20th, 2009, 12:19 PM
#3
Thread Starter
New Member
Re: Open .txt in RichTextBox
To be frank, I'm more on bash scripting and my knowledge in VB is 3 out of 10. I'm only knew how to modify ready made source code. To create & use VB function, module & class it's little bit difficult for me to do it. Plus vb programming just to make my daily work more easier.
So, the answer of your question, "No" I dont know how to do it but are willing to learn.
Back to the topic, it's small flat file with 3KB in size.
Thanks
-
Jun 20th, 2009, 12:31 PM
#4
Re: Open .txt in RichTextBox
i would use 2 richtextboxes one hidden, load the hidden one from the text file. Display results in the second one: using selstart, find and sellength
Heres an example:
http://www.planet-source-code.com/vb...xtCodeId=69098
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Jun 20th, 2009, 02:10 PM
#5
Thread Starter
New Member
Re: Open .txt in RichTextBox
Okay, without knowing much, I give a try to :
- Create UDT
- Open & read the file
vb Code:
'UDT aka user define type Private Type ESX moref As Long name As String uuid As String ipaddr As String End Type Dim vms() As ESX Dim allvmsESX(50) As ESX 'Open & Read File Private Sub Form_Load() Dim strFile As String Dim nFile As Integer, strOut As String strFile = "C:\example.txt" nFile = FreeFile Open strFile For Input As #nFile Do Until EOF(nFile) Input #nFile, strOut Loop Close #nFile End Sub
So, now how to populate & what next? any idea?
-
Jun 20th, 2009, 02:46 PM
#6
Re: Open .txt in RichTextBox
Code:
'UDT aka user define type
Private Type ESX
moref As Long
name As String
uuid As String
ipaddr As String
End Type
Dim vms() As ESX
Dim allvmsESX(50) As ESX
'Open & Read File
Private Sub Form_Load()
Dim strFile As String
Dim nFile As Integer, strOut As String
Dim iPos as Integer, Index As Integer
strFile = "C:\example.txt"
' add error checking in case file doesn't exist
nFile = FreeFile
Open strFile For Input As #nFile
Do Until EOF(nFile)
Line Input #nFile, strOut
If Len(strOut) Then
' Assumption: File format is exactly like the following (no spaces in line)
' moref:800
' name:nox-A
' uuid:564d3ae6-5c80-79bb-daee-60b0215df8ee
' ipaddr:192.168.1.12
iPos = InStr(strOut, ":")
If iPos Then
Select Case LCase$(Left$(strOut, iPos-1))
Case "moref"
If Index = 0 Then
ReDim vms(0)
Else
ReDim Preserve vms(Index)
End If
' should add error checking here too. What if value is non-numeric?
vms(Index).moref = CLng(mid$(strOut,I+1))
Index = Index + 1
Case "name"
vms(Index-1).name = mid$(strOut,I+1)
Case "uuid"
vms(Index-1).uuid = mid$(strOut,I+1)
Case "ipaddr"
vms(Index-1).ipaddr = mid$(strOut,I+1)
Case else
' error text not formatted as expected
End Select
Else
' error text is not formatted as expected
End If
End If
Loop
Close #nFile
If Index > 0 Then
cboIPs.Clear ' combo for IP address
For Index = 0 To Index - 1
' add IPs to combo and use ItemData prop to cross-ref
cboIPs.Add vms(Index).ipaddr
cboIPs.ItemData(cboIPs.NewIndex) = Index
Next
End If
End Sub
' now when a combo box item is selected
vms(cboIPs.ItemData(cboIPs.ListIndex)) references the selected IP
' of course, this idea is based on the fact that no two vms() items will have same IP
-
Jun 20th, 2009, 03:04 PM
#7
Thread Starter
New Member
Re: Open .txt in RichTextBox
Thanks for your effort dude! BTW,
Run time error-13 on this line :
vms(Index).moref = CLng(Mid$(strOut, i + 1))
when I move along mouse cursor to this line, it's return :
vms(Index).moref = 0
when I changed moref as string it's return =""
-
Jun 20th, 2009, 03:08 PM
#8
Re: Open .txt in RichTextBox
Oh, duh on me! Change i + 1 to iPos + 1 throughout. That's the problem with "air code", not tested. Sorry.
-
Jun 20th, 2009, 03:15 PM
#9
Thread Starter
New Member
Re: Open .txt in RichTextBox
nvm, you did enough! My main target actually call combo by "name" instead "ipaddr". After modified it, things I thought impossible become possible now.
Thanks once again for your effort. BTW, how could I rank you?
-
Jun 20th, 2009, 03:24 PM
#10
Re: Open .txt in RichTextBox
 Originally Posted by athlon_crazy
nvm, you did enough! My main target actually call combo by "name" instead "ipaddr". After modified it, things I thought impossible become possible now.
Thanks once again for your effort. BTW, how could I rank you?
Thanks is enough. You're welcome. If you want to "rank" me, click on the scale icon located at the far left of this post.
Last edited by LaVolpe; Jun 20th, 2009 at 03:28 PM.
-
Jun 20th, 2009, 03:36 PM
#11
Thread Starter
New Member
Re: [RESOLVED] Open .txt in RichTextBox
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|