11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>101</strong> TECH TIPSFor <strong>Visual</strong> <strong>Studio</strong> Developersa real lifesaver if you’re coding in both C++ and VB and theprograms have to play nicely. You can directly receive errorresults raised in the VB code instead of hacking around it by settingbreakpoints in the client, setting the next line of execution tosomething that returns to the caller, and using the debugger tosave the error code into a variable on the client. Why the “run,”“step over,” and similar buttons don’t do what ALT+F8 and ALT+F5do is beyond me.VB5, VB6Level: Beginning—Michael Nyman, Tualatin, Ore.Generate Rule-Based Random StringsUse this function to generate random strings that abide by certaincriteria. It’s perfect for password generators or strings used in achallenge/response authentication scheme:Public Enum RandomStringOptionsrsoAllChars = 0rsoAllCharsExtended = 1rsoKeyboardChars = 2rsoAlphaNumericChars = 3End EnumPublic Function RandomString(Optional ByVal _MinLength As Long = 20, _Optional ByVal MaxLength As Long = 29, _Optional ByVal ExclusionCharacters As String = _" ", Optional ByVal RandomOption As _RandomStringOptions = rsoAlphaNumericChars) _As String'===================================================' Generates a random string using ...' Max/MinLength: Determines the minimum and' maximum size of the string.' ExclusionCharacters: Characters that cannot' appear in the random string.' RandomOption: Special options used to define' additional rules.'===================================================' Where random string is builtDim Buffer() As Byte' Next character to testDim NextChar As Byte' The lower range of the char tableDim iCharLo As Integer' The upper range of the char tableDim iCharHi As IntegerDim iAs Long' Sanity checkIf MinLength < 1 Or MaxLength < MinLength ThenErr.Raise 5, App.ProductNameEnd IfIf RandomOption = rsoKeyboardChars Then' -- only keyboard characters are supported' Characters 32 through 126 are keyboard' charactersiCharLo = 32: iCharHi = 126ElseIf RandomOption = rsoAlphaNumericChars Then' This range included entire alphanumeric' charactersiCharLo = 48: iCharHi = 122ElseIf RandomOption = rsoAllCharsExtended Then' -- we can use the entire "standard" ascii' character setiCharLo = 0: iCharHi = 127Else ' RandomOption = rsoAllChars' -- we can use the entire character set,' including extended charactersiCharLo = 0: iCharHi = 255End If' Fire up the random number generatorRandomize Timer' Size the buffer to fit a random number size' within the desired string length range.ReDim Buffer(1 To Int((MaxLength - MinLength _+ 1) * Rnd + MinLength))' Loop through the output bufferFor i = LBound(Buffer) To UBound(Buffer)' Loop until "good" character is selectedDo' Get a random character in the character' set rangeNextChar = Int((iCharHi - iCharLo + 1) * _Rnd + iCharLo)' Make sure not in exclusion listIf InStr(ExclusionCharacters, _Chr(NextChar)) = 0 Then' Check if AlphaNumeric?If RandomOption = rsoAlphaNumericChars _ThenSelect Case NextCharCase 48 To 57, 65 To 90, 97 To 122' within the alphanumeric range' of charactersExit DoCase Else' just keep on looping until' alphanumeric' character generated.End SelectElse' we have a non-excluded charExit DoEnd IfEnd IfLoop' Assign this char, and get nextBuffer(i) = NextCharNext i' Return the resulting stringRandomString = StrConv(Buffer, vbUnicode)End FunctionC#Level: Beginning—Monte Hansen, Ripon, Calif.Close a Windows FormA Close button, which closes a form when the user clicks on it, isone of the most common interface controls added to a Windowsform. Unfortunately, the wizard does not generate the code foryou, so you must do it manually. Add a button to the form; set itstext to Close, Cancel, or Exit; and give it a meaningful name suchas m_CloseButton. Next, create a Click event method handler(such as OnCloseButtonClick) and add a new delegate, initializedwith that handler to the Close button’s Click event:public class MyForm : Form{protected Button m_CloseButton;public MyForm(){InitializeComponent();CancelButton = m_CloseButton;}private void InitializeComponent(){m_CloseButton = new Button();4 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> SEPTEMBER 2001

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!