Sunteți pe pagina 1din 2

When I first started to learn how to program Windows applications I was mystified by the Windows Registry.

I discovered that programs would store information there and most "normal" users would never even know about it. This was the perfect place to store encrypted passwords and things like that. Of course now days the registry is becoming less and less helpful because it has grown enormously and is a pain to move when a computer is upgraded. Still storing a value in the registry, especially program settings or things such as that, can be very helpful. This code snippet shows you how to use Visual Basic's built in SaveSetting and GetSetting commands to store and retrieve data in the registry. To use this code create a new Visual Basic project, add two command buttons to it, add two text boxes. Go to the code area and add the following source code. Private Sub Command1_Click() SaveSetting "MyString", "New", "Test", Text1.Text End Sub Private Sub Command2_Click() Text2.Text = GetSetting("MyString", "New", _ "Test", vbNullString) End Sub Private Sub Form_Load() Text1.Text = "This is a Test" Command1.Caption = "Write Value" Command2.Caption = "Read Value" End Sub After you have added the source, run your program and you should be able to save and retrieve data from the registry. ________________________________________________________________________________________ _______________ You can always close your current form with the Unload Visual Basic command. When that line is encountered your form will disappear. However, wouldn't it be more exciting if instead of just disappearing your form collapsed in towards the center of itself and then disappeared. The way we do this is by setting the forms width and height to a smaller amount over and over again. This can be done easily with a Do while loop. The important thing is to make sure you stop collapsing the form before it gets too small. You will get an error if you set the width or height to anything less than 0. To try this out create a Visual Basic 6 project and a command button to your main form. Double click on the command and enter the following code in the click event handler for your button. Do While Me.Width > 1800 Me.Width = Me.Width - 300 Me.Left = Me.Left + 150 If Me.Height - 400 Then Me.Top = Me.Top - 1 Me.Height = Me.Height - 150 End If Loop

Unload Form1: End Once you have the code entered run your program and click the button. You should see your form leave with a little style ________________________________________________________________________________________ _______________ An often requested task that new developers to VB ask is how can I write a program that will launch other programs. Its fun to create your own little launcher program that does things like allows you to quickly launch just the programs you want. Obviously program launchers already exist out on the market that you could simply use. But one of the great things about learning a programming language like Visual Basic is that you can create your own programs that behave exactly how you want them to. To try out the this sample source code snippet simply create a VB project, add a command button to the main form, double click this button to get into the click event handler for it and add the following code. Dim Res Dim Filename Filename = "C:\windows\notepad.exe" 'Check file is here first If Dir(Filename) = "" Then MsgBox Filename & " not found", vbInformation Else Res = Shell("Start.exe " & Filename, vbHide) End If This source code uses the VB6 Shell command to launch a new program. We specifically call the Start.exe program as it can be used to launch other programs easily.

S-ar putea să vă placă și