Hi guys

I have a a text box that is initialized with some value, let's say "William".

When I run the program, the text box shows "William" of course, but what I want to do is to type something else, let's say "Vincent" and set this value as the default value, so when I run the program next time, it will show "Vincent" instead of "William".

Thanks in advance.

Recommended Answers

All 10 Replies

The easiest way to do this, is to have a settings file (you could use the registry also, but I find files are easier to clean up, and fit logical structure a lot better). In your settings file, you have 1 line per text box (or default value). This way, line 1 of the file will always be the default value for text1. Line 2 for text2, etc, etc. Then, on form load (or sub main, or whatever), just open the file for input, read it in, and set the values accordingly. This is how I do it. I actually have a sub that I call on startup, that makes sure everything is set before it actually loads. I just callt he sub in form_load, and it makes sure I have directories (should I need them), and if I have settings it loads the settings....

Ok, I thought of doing it like that, but is there a specific or a recommended type of file to use? Can you give an example maybe? it would be greatly appreciated.

Just a plain text file.... something lto write the file ike:

open app.path & "\settings.dat" for output as #1
     print #1, text1.text
     print #1, text2.text
close #1

and read it like:

if dir(app.path & "\settings.dat", vbnormal) <> "" then
     open app.path & "\settings.dat" for input as #1
          line input #1, text1.text
          line input #1, text2.text
     close #1
end if

I think files are great because you can transfer them around, but they do involve more steps than the registry. I personally would rather use the windows registry, specially if you only need the textbox to display the last entry. From the coding point of view, I think it's easier than using files, and I think the registry helps keep prying eyes out.

As a person who is diligent in the open source community, even programs that are NOT open source, should still have the availibility to have the settings altered fairly easily. That's one reason I've hated proprietary software, is because it's on my computer, I should be able to mess with it as I please..... If I want to change the settings with Notepad, or DOS Edit instead of some gui that someone has decided was the best interface, I should have the option and that right, and that should be made fairly easily to access. Granted, you could use regedit to "manually" alter settings, but it's the principle of the idea behind it. Plus, there isn't really that many more steps needed.... on form unload, simply do an open for output (which overwrites previous values) and on form_load read the input into the correct textboxes.... but either way you choose to do it, I wish you the best of luck.

Hi.

Thanks again, that worked !!

I have only one more question: when i read from the file I have to save the values to a variable, and then copy the value of the variable to the combo box. If I do it directly it does not work. However, when writing to the file, I can do it directly. Am I doing something wrong? I guess that's the way it is.

Here is the code for reading:

if dir(app.path & "\settings.dat", vbnormal) <> "" then
     
    Open App.Path & "\settings.dat" For Input As #1
          
          Do While Not EOF(1)
            Line Input #1, name
            cmbNames(i).Text = name
            i = i + 1
          Loop
          
     Close #1
    End If

and for writing:

Open App.Path & "\settings.dat" For Output As #1
     
     Print #1, cmbNames(0).Text
     Print #1, cmbNames(1).Text
     Print #1, cmbNames(2).Text
     
Close #1

Thanks.

No. You are absolutely correct. I tried to do it that way also (skipping variables) and it gets pretty upset. I guess it's just the name of the game, and I appreciate your return response.

About the response is no problem at all, I try to clarify the problem, so whoever has the same question also has the whole answer and not a part of it.

Thanks a lot for your support.

Hi;

You might try using .AddItem instead of .Text. By clearing the combo box first ( .Clear ) it should then refill the list.

if dir(app.path & "\settings.dat", vbnormal) <> "" then

Open App.Path & "\settings.dat" For Input As #1

Do While Not EOF(1)
Line Input #1, name
cmbNames(i).Text = name
i = i + 1
Loop

Close #1
End If

Try this code:

If Dir(App.Path & "\settings.dat", vbNormal) <> "" Then
Open App.Path & "\settings.dat" For Input As #1
 
cmbNames.Clear
 
Do While Not EOF(1)
Line Input #1, Name
cmbNames.AddItem (Name)
i = i + 1
Loop
 
Close #1
 
End If

Hi, again; Please disregard my prior entry. It doesn't make sense to me either when I see it in the light of day. ( NOTE TO SELF: Do NOT try to write code in early morning when you can't sleep ). :o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.