Tag: visualbasic

Generate Random String

randomstring

 

Imports System.Text
 
Public Class Form1
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim s As String = “ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz”
    Dim r As New Random
    Dim sb As New StringBuilder
 
    For i As Integer = 1 To 8
      Dim idx As Integer = r.Next(0, 61)
      sb.Append(s.Substring(idx, 1))
    Next
 
    TextBox1.Text = sb.ToString
  End Sub
End Class

Simple wait routine in VB.NET

Public Class Form1

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
HoldYourpants(2) ‘ wait 2 seconds
HoldYourWater(2) ‘ more efficient code

  End Sub

  Public Sub HoldYourPants(ByVal PauseTime As Integer)
    Dim Start As Double
    Start = Microsoft.VisualBasic.Timer ‘ Set start time.

    Do While Microsoft.VisualBasic.Timer < Start + PauseTime
      Application.DoEvents()
    Loop
  End Sub

  Public Sub HoldYourWater(ByVal Seconds As Integer)
    System.Threading.Thread.Sleep(Seconds * 1000)
  End Sub

End Class

Read MAC Addresses in VB.NET

The following code is a working example for determining the MAC address of your network card.

Imports System.Net.NetworkInformation

Public Class Form1
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    MessageBox.Show(getMacAddress)
    End
  End Sub

  Function getMacAddress()
    Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
    Return nics(1).GetPhysicalAddress.ToString
  End Function

End Class

Set Default Enter Button in ASP.NET

I recently had to define a default Enter button on an ASP.NET page but could not use the Form field because it was inherited from a master page.  I eventually discovered that this can be done by defining the button in a panel as shown below.

<asp:Panel runat=”server” DefaultButton=”btnSearch”>
..
<asp:Button ID=”btnSearch” runat=”server” Text=”Search” />
..
</asp:Panel>

Use  txtSearch.Focus() to set focus on the default field (in this case txtSearch).