Back |
Code Snippets I really don't have much here but I think I cover some of the more important/popular questions. These all apply to VB5 although some may work in previous versions of Visual Basic.
This just means opening a file with it's default application. For instance, opening an html file with the users default browser. I've seen people do this the hard way. They use API to check the default application in the system registry. The easiest way I've found is to shell the start command but hide the DOS box. Public Sub startFile (strFileName as String)
End Sub Short, simple, efficient, best of all, no API calls. This should NOT be used for anything too serious. Public Function encodedStr (strSimple As String) As String
End Function This Just gets the ASCII value for each character and turns it into a number plus 111, then converts it back to an ASCII character. You can really make the number anything you want, but you do want it to be too long or else when you try to set it back to an ASCII character you'll get an error. For instance, there is no Chr$(19487), just doesn't exist. Now to decode it... Function decodedStr (strEncoded As String) As String
End Function That's all there is to it. Like I said, you probably wouldn't want to use this for anything too serious. Getting/Setting your application's settings in the registry This one is pretty easy. To save a setting use.. SaveSetting(AppName As String, Section As String, Key As String, Setting As String) Or... for example SaveSetting("MailApp", "Server", "ServerName", strServer) To get the settings use.. Setting = GetSetting(AppName As String, Section As String, Key As String, [Default]) As String Or.. for example (again) strServer = GetSetting("MailApp", "Server", "ServerName", "pop.somwhere.com") As String This will set the variable strServer to whatever the setting in the registry is. If the setting does not exist, then strServer will be set to the default value of "pop.somewhere.com" Checking for previous instances of your application. This just means, checking if your program is already running. Just use this in your startup form or module. If App.PrevInstance Then End Simple enough eh? Returning the file name from a path. This would be used as something like ... theFile = getFileName(path) Public Function getFileName(thePath As String) As String
End Function Only allow numbers in a text box. This would go in the KeyPress event of the text box that you only want to contain numbers. The only exception is the backspace, so the user can correct mistakes. Private Sub txtInt_KeyPress(KeyAscii As Integer) ' don't allow a non-numeric character to be entered unless it's a backspace
End Sub More to come.. |