URL Encoding in Windows Forms

If you need to do some URL encoding in a Windows Forms app, the HttpUtility class has a shared (static) method to URL encode a string. Below is a sample.

NOTE: The HttpUtility class also contains a shared (static) HTML encoder too!

Option Strict On
Option
 Explicit On

Imports
 System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Web

Public Class WinApp
    
Inherits System.Windows.Forms.Form

    
private m_url as TextBox

    <STAThread()> _
    
Shared Sub Main
        Application.Run(
new WinApp())
    
End Sub

    Public Sub New
        Me.Text = "URL Test"

        m_url = New TextBox()
        m_url.Size = 
new Size(280,30)
        m_url.Location = 
new point (5,5)

        Controls.Add(m_url)
        m_url.Text = 
"http://www.microsoft.com?value="; & HttpUtility.UrlEncode("encode me please")
    
End Sub

End
 Class

No Comments