Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Save Settings To A Filename 1

Status
Not open for further replies.

MrVB50au

Programmer
Mar 16, 2003
326
AU
Here's what's happening:

I have a DB file with lots of records, when I click on a button, it uses the currently displayed record to open another form that has a picturebox with label controls containing the information just gathered.

After manipulating fontname size and positioning the labels on the picturebox etc. Instead of saving the picturebox only (with the details) as a bmp file, I would like to allow the user to type a filename and have the settings (eg: fontname, size, color, etc.) saved into that file.

As it is at the moment the user needs to recreating the whole thing just to make a few adjustments.

I would like the user to be able to open the saved file and simply make the appropiate adjustments then resave it again.

Is it possible that this can be achieved?

Thanks for your valuable time,

Andrew.
 

It sounds very possible but you may have some work ahead for you. You will need to figure out a format that you can save this info to and then be able to read it back in. Then if you are wanting to save the picture created itself into this file you will need some sort of delimiter to distinguish the custom information from the graphic information.

Here is an example program that you can use to determine which controls are contained by the picture box. You will need to add a picture box, 2 command buttons and a label to the form (the code will do the rest)...
[tt]
Option Explicit

Private Sub Form_Load()
Picture1.Left = 30
Picture1.Top = 30
Picture1.Height = (Me.Height / 2)
Picture1.Width = (Me.Width / 2)

Set Command1.Container = Picture1
Command1.Left = 30
Command1.Top = 30

Set Label1.Container = Picture1
Label1.Left = Picture1.Width - (30 + Label1.Width)
Label1.Top = Picture1.Height - (30 + Label1.Height)

Command2.Left = Picture1.Width + Picture1.Left
Command2.Top = 30

End Sub

Private Sub Command2_Click()

Dim C As Control

For Each C In Me.Controls

If C.Container.Name = "Picture1" Then

Debug.Print C.Left
Debug.Print C.Top

End If

Next

End Sub
[/tt]
So as you can see I am able to find out which controls are a part of picture1 (or contained by) and retrieve their properties which is what you will have to do. It is then up to you to define the format you will save it in even though I do have a suggestion for you...

Create a dummy project or look at one of you simpler projects that has a picture box and a couple of controls contained within the picture box and a couple of controls not contained within the picture box. Open the form (.frm) file in notepad and take a look at how it is organized. I am not saying you should make a duplicate of that file but do you see how it is organized...
[tt]
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 525
Left = 2940
TabIndex = 4
Top = 1230
Width = 1245
End
Begin VB.Frame Frame1
Caption = "Frame1"
Height = 525
Left = 2490
TabIndex = 3
Top = 2010
Width = 1245
End
Begin VB.PictureBox Picture1
Height = 1695
Left = 240
ScaleHeight = 1635
ScaleWidth = 2505
TabIndex = 0
Top = -180
Width = 2565
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 765
Left = 30
TabIndex = 1
Top = 90
Width = 1215
End
Begin VB.Label Label1
Caption = "Label1"
Height = 435
Left = 870
TabIndex = 2
Top = 1080
Width = 1275
End
End
Begin VB.Label Label2
Caption = "Label2"
Height = 525
Left = 1020
TabIndex = 5
Top = 2010
Width = 1245
End
End
[/tt]
See how the command button and label are under the picture box definition and indented?

Ok, so that is the controls and how to save them now to add the graphic to this file...

What you could do is to save the contents of the picture box to a bmp file and then read the bmp file in and add it to this file so then when you read it back in all you would need to do is to seperate the bitmap info from the control info and save it to a bmp and use the loadpicture function to reload it into the picture box.

So that leaves a delimeter to tell the difference between the control info and the bitmap info...

Start bmp

I think you get what I am getting at.

Good Luck

 
I've got a bit of work ahead of me but this project and you know is worth the work not to mention the learning I'm getting from doing so.

Yes I'm understand everything you're saying, but how would I read the information back line by line ?

Lets just say for argument sake that I do use this format.

How will I go by getting the information back into the application and onto the form where it belongs?

Thanks heeps.

Andrew.
 

[tt]
Dim FName As String, FNumb As Integer, S As String
FNumb = FreeFile
FName = "User Selected File Name (and path)"
Open FName For Input As #FNumb
Do While Not Eof(FNumb)
Line Input #FNumb, S
'use if statement here to test for bmp file starting and for figuring out what this current line means
'...
Loop
Close #FNumb
[/tt]
So then when it comes to figuring out what the current line means I will leave it up to you as I will also do for figuring out how to extract the bmp info.

Good Luck

 
vb5prgrmr

What you mean?
'use if statement here to test for bmp file starting and for figuring out what this current line means

With S as being the string to read in the information, would I still be able to set Left,Top,Width and Height values?

This is going to be some task hey?

I will email you the screenshot of the actual form.

Thanks again,

Andrew.



 

What I mean by that is once you read each line in you will have to test for what it means, wether it is the left property, the caption property, or the line that signifies that the next line is the start of the bmp information.

Yes it will take a bit of coding to be able to create the file and then to be able to read it back in.

Good Luck

 
Simular to an INI file then?

If this is the case, would it profit me to use an INI file format (as I do now with the app' settings)?

I can get the user to type in a filename and that filename will be used for the filename of the INI file.

All I need then is to either leave the extension as it is and/or change the .INI extention to (eg: .CRD) extention. If so, how will I be able to change the file extention?

What do you think of this idea, advisable or not?

Thanks again,

Andrew.
 

use the ...
[tt]
Name OldName As NewName
[/tt]
Or you can use the copyfile/kill method

Any format you wish. It is your program/idea. I would say otherwise if you were making a clone of another program or making an addin for another program.

Good Luck

 
OK, I'd keep you posted on how I'm progressing.

Thanks heeps,

Andrew.

 
So far so good!
Have given the user the option of typing in the filename to save the settings and they save correctly to an ini file.

One small hickup tho'
I have tried to do a FileCopy as in:
FileCopy oldFile, newFile but when I open the new file it shows gobble-di-gook characters and not the characters like the ini file (which is readable).

Here's the code:
-----------------------------------------------------------------
Private Sub SaveToPictureFile()
Dim PathFileName As String
Dim INIFileName As String
If Len(Label3.Caption) > 3 Then
Label3.Caption = Label3.Caption & "\"
End If
INIFileName = Label3.Caption & Text1.Text
SaveToNewINIFile INIFileName
PathFileName = Label3.Caption & Text1.Text & ".bmp"
SavePicFile = PathFileName
End Sub

Private Sub SaveToNewINIFile(INIFile As String)
Dim oldFile As String
Dim newFile As String
oldFile = INIFile & ".ini"
newFile = INIFile & ".crd"
'
With frmCard
For i = 0 To .lblCrd.Count - 1
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "Caption", .lblCrd(i).Caption
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "Top", Val(.lblCrd(i).Top)
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "Left", Val(.lblCrd(i).Left)
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "FontName", .lblCrd(i).Font.Name
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "FontSize", Val(.lblCrd(i).Font.Size)
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "Bold", .lblCrd(i).Font.Bold
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "Italic", .lblCrd(i).Font.Italic
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "Underline", .lblCrd(i).Font.Underline
SaveToINIFile oldFile, "FILESETTINGS", "LBL" & i & "ForeColor", Val(.lblCrd(i).ForeColor)
Next i
End With
'
'FileCopy didn't work, so lets try renaming it.
Name oldFile As newFile
End Sub
-----------------------------------------------------------------

Can you see the problem?

It must be the format of the file as .crd must be already a taken extention.

The INI File itself is fine. It's only the renaming makes the CRD file look stupid.

What am I doing wrong?

Regards,

Andrew.
 

Right off hand I do not see what could be causing the corruption of the file so here are a few tests for you to try to help you figure out what may be going on.

After you save the settings the file are you sure that the file is closed?

End your program and make a copy via explorer into another directory and open up the ini file via double click. What program opened it? Now through explorer rename the file and use the same program to open it up and take a look at the contents. Is there a difference?

As for the crd extension being known and when you change the file name/extension to crd that is all you are doing. The contents of the file should not change.

One other thing you may want to look at before you do your name or copy is to use the setattr function with the vbNormal attribute set.

I have just tried the rename on my computer and the same contents come up so there is definitly something going on with what or how you are doing it???

You will find it, I have faith.

Good Luck

 
OK, here's the differences:

when I had changed the extension of newFile
This worked >> newFile = INIFile & ".txt"

But only after I used:
Close
Then I renamed it.
Name oldFile As newFile

However, as soon as I use the .crd extension for the file, it goes all gobble-di-gook again as if it was saved as a binary file and not a text file.

Is there a way to be sure that the file is saved as a text file?

Thanks again,

Andrew.


 

>Is there a way to be sure that the file is saved as a text file?

pause your program and open up the ini file in notepad (or make your own notepad version and read it in yourself).

then step through to where it is renamed and open up the new file in notepad.

There should not be a difference between the contents of the two files.

Good Luck

 
Ok I used the breakpoint to break at that place
Opened the ini file and looks good.
then took one more step and .crd looks the same as ini.

What Now?

 
to take some precautions I've been saving them to another drive (eg: D:\) demo07.crd was saved while breakpoint was at name oldfile as newfile

but when I took the breakpoint off and reran the program, it did exactly the same thing as before. I saved the next one to demo08.crd and again it went all gobble-do-gook.

I don't know what's happening!

You Think I should use DoEvents before the renaming of the file?

 
OK, it's working beautifully now, I went back using the FileCopy command and Kill oldFile. I decided to change the extension (if that had anything to do with it I don't know) to .ebc opened the file and everything looks good now.

Also, I had found that I had to use the Close command and not use the FileCopy command inside the procedure, even if it was the last statement in the procedure. I had to do it after the called procedure had finished what it was doing.

Now, the next fun bit, Opening the file. This shouldn't be too much of a problem I hope......LOL I guess the next thing would have to be the FileCopy command again to change it back to the ini file and then destroy the other file. However, I might get the program to first make a copy of the (eg: demo02.ebc) file and put it in the systems temp folder. This way I will be able to retrieve settings if anything does go wrong. "Just a precaution!"

I will keep you posted on how the progress is going.

You are right, this is a big job and no matter how condence I wanted to make it, I found myself still having to write lots of code. Oh Well, that programming I guess.

Must go now and see how this will work out.

Regards,

Andrew.
 
OK, Had come up with a decision that I'm not sure if I'm correct and how to pin point to the existing (Loaded) INI file so I can close it so the user can open another file.

This is my thinking:

After the settings have been loaded, do I need to close the ini file then?

What's happening is that I go and open another file and I get the message: "Can't Open INI File". Is this to do with the reason of the old file still maybe in memory?

Tho is does however load some of the setting that I did for that file (and not others), I still get that message. Or is it something in my loading code?

Any ideas?

Andrew.
 
I had pin pointed the problem and man, it's so damn finiky! One little mistake somewhere can throw the whole thing out of wak!

Saved another file and reopened it in notepad and yep you guessed it, a variable wasn't stored and it threw the lot off course.

Andrew.
 

It is best that if you open a file and you are done with it you should close it. Meaning that if you are using a file to read in settings so the user can view/edit the information, then when you are done reading it in close it. Who knows how long or how much the user is going to change that information. Then if they do change something then use a boolean variable to test against when the user wants to open another file or exit the application. If the user has not made any alterations when they want to open another file or exit the program you will not have to deal with saving the information back out.

Good Luck

 
So we would say for example, using a boolean expression as:

***In the main module***
Public Dirty As Boolean
-----------------------

Then when the user changes anything on the loaded file, then Dirty = True So then when the user closes the Card Editor it will prompt them to save the information. I think it also might be a good idea to give them a choice to either Save or SaveAs buttons on the Prompt you think?

After loading the file, do I simply use the Close function or would I need to specify the actual file?

VB Help says to simply use the Close function.

Is there a way that I can make the Close function tell it what file to close? It's not really important, I'd just like to know that Demo01.ini filename was specifically Closed by say: Close Demo01.ini
Then I can use the FileCopy command and Kill Demo01.ini

Thanks heeps,

Andrew.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top