|
-
Apr 3rd, 2007, 08:04 AM
#1
Thread Starter
Junior Member
Read/Write arrays of UDT in Binary Files
In my program users can add and remove Label and image control at runtime from a picture wich is the container, so users can add label and image control as they want. I loop each control to determine each property and save it in a binary files, to later read this file and load each control.....
Code:
Public Type misText
nombre As String
indice As Long
titulo As String
alto As Long
ancho As Long
tope As Long
izquierda As Long
FontName As Variant
FontSize As Variant
FontBold As Variant
FontItalic As Variant
FontUnderline As Variant
ForeColor As Variant
End Type
Public Type misImag
nombre As String
indice As Long
alto As Long
ancho As Long
tope As Long
izquierda As Long
imagPath As String
End Type
Public cantText As Long
Public cantImg As Long
Public miText() As misText
Public miImg() As misImag
Public Sub Guardando(sFic)
Dim Imagenes()
Dim Textos()
Dim CodigoB
Dim objCtl As Control
cantText = 0
cantImg = 0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Lo primero que hago es abrir el fichero y borrarlo, porque sino se anexa la info
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Open (MDIMain.CommonDialog1.FileName) For Binary As #1
Close #1
Kill (MDIMain.CommonDialog1.FileName)
For Each objCtl In FrmDiseno.Controls
If objCtl.Container Is FrmDiseno.Picture1 Then
Select Case objCtl.Name
Case "Texto1"
cantText = cantText + 1
ReDim Preserve miText(cantText)
miText(cantText).alto = objCtl.Height
miText(cantText).ancho = objCtl.Width
miText(cantText).izquierda = objCtl.Left
miText(cantText).tope = objCtl.Top
miText(cantText).titulo = objCtl.Caption
miText(cantText).FontBold = objCtl.Font.Bold
miText(cantText).FontItalic = objCtl.Font.Italic
miText(cantText).FontName = objCtl.Font.Name
miText(cantText).FontSize = objCtl.Font.Size
miText(cantText).FontUnderline = objCtl.Font.Underline
miText(cantText).ForeColor = objCtl.ForeColor
Case "Imagen1"
cantImg = cantImg + 1
ReDim Preserve miImg(cantImg)
miImg(cantImg).alto = objCtl.Height
miImg(cantImg).ancho = objCtl.Width
miImg(cantImg).imagPath = objCtl.Picture
miImg(cantImg).izquierda = objCtl.Left
miImg(cantImg).tope = objCtl.Top
End Select
End If
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Ahora guardo la informacion en el fichero (Save the information in files)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim nFic As Integer
nFic = FreeFile
Open sFic For Binary As nFic
Put nFic, , cantText
For I = 1 To cantText
Put nFic, , miText(I)
Next I
Put nFic, , cantImg
For I = 1 To cantImg
Put nFic, , miImg(I)
Next I
Put nFic, , myBarcode
Close nFic
End Sub
My Question is... How could i read this info from the File???
Thanks.....
-
Apr 3rd, 2007, 09:03 AM
#2
Re: Read/Write arrays of UDT in Binary Files

You can put some type of separator between the data items and then use Split to help read them in.
-
Apr 3rd, 2007, 09:07 AM
#3
Re: Read/Write arrays of UDT in Binary Files
Create a UDT that has 2 items, a misText item and a misImag item. Write this item to the file. I'd include the number of controls in the For Each loop as one of the elements in the variable, and I'd add a variable for the name of the control.
To read, just read the file back into the new UDT and set your control properties to the values in the UDT. You know the name of each control read, and you know the number of controls.
(It's easiest, for debugging purposes, to maintain a single variable length in the file.)
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Apr 3rd, 2007, 09:35 AM
#4
Re: Read/Write arrays of UDT in Binary Files
You can read/write UDT arrays directly to a file using Binary Access Write and the Put statement, no need for parsing/looping, etc.
-
Apr 3rd, 2007, 10:58 AM
#5
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
If the Image control have a picture ...How could include the picture in the binary files????
Thanks
-
Apr 3rd, 2007, 11:44 AM
#6
Re: Read/Write arrays of UDT in Binary Files
 Originally Posted by Yovy7410
If the Image control have a picture ...How could include the picture in the binary files????
Thanks
If you have a UDT with a member that is a picture ie:
vb Code:
Type MYTYPE
strOne As String
picAPicture As StdPicture
End Type
Private Type udtType() As MYTYPE
That picture data should get written to the binary file as well simply with the Put statement.
I've never tried it with a Picture in a UDT but try it, it should work.
-
Apr 3rd, 2007, 12:05 PM
#7
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
-
Apr 3rd, 2007, 01:50 PM
#8
Re: Read/Write arrays of UDT in Binary Files
Take a look at this thread for an example I made for someone else some time ago:
http://www.vbforums.com/showthread.p...oto=nextoldest
-
Apr 3rd, 2007, 05:08 PM
#9
Re: Read/Write arrays of UDT in Binary Files
Why not just write the UDT's to a database, then you can retrieve them any way you want.
-
Apr 3rd, 2007, 05:19 PM
#10
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
Because i want to associate this file with a given extension (.etq) in my program, so user can load it from the main App...
Thanks every body....
-
Apr 3rd, 2007, 05:22 PM
#11
Re: Read/Write arrays of UDT in Binary Files
And you can't do that with a database?
-
Apr 3rd, 2007, 05:24 PM
#12
Hyperactive Member
Re: Read/Write arrays of UDT in Binary Files
to put the UDT into a binary w/o parsing, you will need fixed-length strings.
instead of: nombre as string
use: nombre as string * 55
for a 55char wide string.
using fixed length strings allow you to move the whole UDT to/from a disk instantly.
but, if you don't use fixed length, who knows where the data will fall when put back into the UDT...
-
Apr 3rd, 2007, 06:18 PM
#13
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
Yes randem i could do it with a DB, but the idea is a File, so user could save that information where ever they want....thanks...
in the oder hand rnd me
is necessary putting fixed-length strings in Binary access?
-
Apr 3rd, 2007, 06:23 PM
#14
Re: Read/Write arrays of UDT in Binary Files
Then if you want to use a flat file you are going to need keys that proceed each UDT to tell you which one it is and how long it is so that you can get to the next one. Not too hard do do.
-
Apr 3rd, 2007, 06:41 PM
#15
Re: Read/Write arrays of UDT in Binary Files
Or you need to include a header on your flat file to show where in the file each UDT is located (offsets).
-
Apr 3rd, 2007, 06:58 PM
#16
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
-
Apr 3rd, 2007, 07:00 PM
#17
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
Have some example about this???
-
Apr 3rd, 2007, 07:07 PM
#18
Re: Read/Write arrays of UDT in Binary Files
No examples but here is the procedure. You will create a fixed size header for your file. Example 256 bytes. In the 256 bytes you will define the offset to where you write your UDT and the type and size of your UDT.
Let say you define 7 UDT's. Your header would look something like this
072561340139080
This would state that UDT no 7 is at offset 256 and is 134 bytes long.
and UDT 01 is at offset 390 and is 80 bytes long.
-
Apr 3rd, 2007, 07:16 PM
#19
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
Yes.... is a good one....thanks
-
Apr 3rd, 2007, 07:20 PM
#20
Re: Read/Write arrays of UDT in Binary Files
And I would suggest using binary data (as opposed to text) in your header to conserve data size.
-
Apr 3rd, 2007, 07:24 PM
#21
Thread Starter
Junior Member
Re: Read/Write arrays of UDT in Binary Files
-
Apr 3rd, 2007, 08:05 PM
#22
Re: Read/Write arrays of UDT in Binary Files
Use something like this for your header. Each entry will be 5 bytes then you could use a smaller header if your UDT's are limited.
Code:
Private Type Header
UDTType as byte
Offset as long
Length as long
End Type
-
Apr 3rd, 2007, 11:14 PM
#23
Re: Read/Write arrays of UDT in Binary Files
You don't need to store the position of every UDT item in the array, just how many there are in the array.
Did you not click the example I posted above?
 Originally Posted by rnd me
to put the UDT into a binary w/o parsing, you will need fixed-length strings.
instead of: nombre as string
use: nombre as string * 55
for a 55char wide string.
using fixed length strings allow you to move the whole UDT to/from a disk instantly.
but, if you don't use fixed length, who knows where the data will fall when put back into the UDT...
And you don't need to use fixed-length strings either.
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
|