VBTheory™ Tutorials

VBTheory Tutorials, A New World Of Programming

Free C# and VB.NET tutorials for everyone.

More involving than others

VBTheory Tutorials uses many social networks in order to involve its audience.

Friday, November 7, 2014

How to create a shortcut - VB#25

Download IWshRuntimeLibrary:
Link

Code:
Imports IWshRuntimeLibrary

Public Class Form1

    Private Sub BtnPath_Click(sender As Object, e As EventArgs) Handles BtnPath.Click
        Dim s As New SaveFileDialog()
        s.Filter = "Shortcut (*.lnk)|*.lnk"

        If (s.ShowDialog() <> DialogResult.OK) Then Return

        If (s.FileName.EndsWith(".lnk")) Then TxtPath.Text = s.FileName
    End Sub

    Private Sub BtnFile_Click(sender As Object, e As EventArgs) Handles BtnFile.Click
        Dim o As New OpenFileDialog()
        o.Filter = "Any file (*.*)|*.*"

        If (o.ShowDialog() <> DialogResult.OK) Then Return

        TxtTarget.Text = o.FileName
    End Sub

    Private Sub BtnFolder_Click(sender As Object, e As EventArgs) Handles BtnFolder.Click
        Dim f As New FolderBrowserDialog()

        If (f.ShowDialog() <> DialogResult.OK) Then Return

        TxtTarget.Text = f.SelectedPath
    End Sub

    Private Sub BtnIcon_Click_1(sender As Object, e As EventArgs) Handles BtnIcon.Click
        Dim o As New OpenFileDialog()
        o.Filter = "Icon file (*.ico)|*.ico"

        If (o.ShowDialog() <> DialogResult.OK) Then Return

        TxtIcon.Text = o.FileName
    End Sub

    Private Sub BtnCreate_Click(sender As Object, e As EventArgs) Handles BtnCreate.Click
        Dim wsh As New WshShellClass()
        Dim shortcut As IWshShortcut = wsh.CreateShortcut(TxtPath.Text)

        shortcut.TargetPath = TxtTarget.Text
        shortcut.Description = TxtDescription.Text
        shortcut.IconLocation = TxtIcon.Text

        shortcut.Save()
    End Sub
End Class



Friday, October 31, 2014

Graphics 3 - Advanced Shapes - CSharp #5

Code:
Arc:
 e.Graphics.DrawArc(Pens.Black, x, y, width, height, start, end);

Pie:
 e.Graphics.DrawPie(Pens.Black, x, y, width, height, start, end);

Polygon:
 e.Graphics.DrawPolygon(Pens.Black, new []{...});

Bézier:
 e.Graphics.DrawBezier(Pens.Black, x1, y1, x2, y2, x3, y3, x4, y4);

ClosedCurve:
 e.Graphics.DrawClosedCurve(Pens.Black, new []{...}, tension, fillmode);

Note: You can replace "Draw" with "Fill" and "Pens" with "Brushes" if you want you shapes to have fills instead of contour only



Friday, October 24, 2014

Asking for administrator privileges - VB#24


Code:
IsAdmin function:
    Private Function IsAdministrator() As Boolean
        Try
            Dim user As WindowsIdentity = WindowsIdentity.GetCurrent()
            Dim principal As WindowsPrincipal = New WindowsPrincipal(user)
            Return principal.IsInRole(WindowsBuiltInRole.Administrator)
        Catch ex As Exception
            Return False
        End Try
    End Function

Checking if the app has privileges:
	If (IsAdministrator()) Then
		MessageBox.Show("the application is running as admin")
	Else
		MessageBox.Show("the application is not running as admin")
	End If

Restarting the app as admin:
	If (IsAdministrator()) Then Return

	If (MessageBox.Show("do you want to restart the application to get admin privileges?", "", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
		Dim p As New Process()
		p.StartInfo.FileName = Application.ExecutablePath
		p.StartInfo.Verb = "runas"
		p.Start()
		Process.GetCurrentProcess().Kill()
	End If



Friday, October 17, 2014

Graphics 2 - Gradients & more advanced brushes - CSharp #4


Code:
SolidBrush:
 SolidBrush b = new SolidBrush(Color.Blue);
Or:
 Brush b = SystemBrushes.Control;
Or:
 Brush b = Brushes.Blue;

TextureBrush
 TextureBrush b = new TextureBrush(new Bitmap(@"PATH HERE", WrapMode.Tile);

HatchBrush
 HatchBrush b = new HatchBrush(HatchStyle.Cross, Color.Black);

LinearGradientBrush
 LinearGradientBrush b = new LinearGradientBrush(new Point(X1, Y1), new Point(X2, Y2), color1, color2);

PathGradientBrush
 PathGradientBrush b = new PathGradientBrush(new [] {new Point(X, Y), new Point(X, Y), ...})
 b.CenterPoint = new Point(X, Y);
 b.CenterColor = Color.Blue;
 b.SurroundColor = new [] {Color.Red, Color.Black, ....};

Don't forget to add this at the end to fill the form with your brush:
 e.Graphics.FillRectangle(b, e.ClipRectangle);


HatchStyles:
All HatchStyles available in C#



Friday, October 10, 2014

How to shutdown, restart or logoff - VB#23

Code:
Shutdown:
System.Diagnostics.Process.Start("shutdown", "-s -t 00")

Restart:
System.Diagnostics.Process.Start("shutdown", "-r -t 00")

LogOff:
System.Diagnostics.Process.Start("shutdown", "-l -t 00")



Friday, October 3, 2014

Graphics 1 - Pens & Brushes - CSharp #3

Code:
Creating a pen
Pen p = new Pen(Color.Blue, 5);

Or:
Pen p = Pens.Blue;

Drawing a line:
e.Graphics.DrawLine(p, X1, Y1, X2, Y2);

Drawing a circle:
e.Graphics.DrawEllipse(p, X, Y, WIDTH, HEIGHT);

Drawing a rectangle:
e.Graphics.DrawRectangle(p, X, Y, WIDTH, HEIGHT):

Creating a brush:
Brush b = new SolidBrush(Color.Black);

Or:
Brush b = Brushes.Black;

Drawing text:
Font f = new Font("Segoe UI", 15, FontStyle.Italic | FontStyle.Bold);
e.Graphics.DrawString("YOUR TEXT HERE", f, b, X, Y);

Filling circle;
e.Graphics.FillEllipse(b, X, Y, WIDTH, HEIGHT);

Filling rectangle:
e.Graphics.FillRectangle(b, X, Y, WIDTH, HEIGHT);



Friday, September 26, 2014

How To Download A File (2 Different Ways) - VB#22


Code:

Synchronous:
Dim w As New WebClient
w.DownloadFile("https://dl.dropboxusercontent.com/u/58717780/tutorial.txt", "C:\Windows\Temp\test.txt")
Process.Start("C:\Windows\Temp\test.txt")

Asynchronous:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim w As New WebClient

  AddHandler w.DownloadFileCompleted, AddressOf DownloadComplete
  AddHandler w.DownloadProgressChanged, AddressOf ProgressChanged

  w.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/u/58717780/tutorial.txt"), "C:\Windows\Temp\test.txt")
End Sub

Private Sub DownloadComplete(sender As Object, e As   System.ComponentModel.AsyncCompletedEventArgs)
  Process.Start("C:\Windows\Temp\test.txt")
End Sub

Private Sub ProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
  ProgressBar1.Value = e.ProgressPercentage
End Sub



Friday, September 19, 2014

How to start application with Windows (2 Different Ways) - CSharp #2


Code:
Create registry:
 RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
 key.SetValue("Tutorial", Application.ExecutablePath);
 key.Close();
 MessageBox.Show("Success");

Remove registry:
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
 key.DeleteValue("Tutorial");
 key.Close();
 MessageBox.Show("Success");

Create file:
File.Copy(Application.ExecutablePath, @"C:\Users\VB\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\tutorial.exe");

Delete file:
File.Delete(@"C:\Users\VB\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\tutorial.exe");



Monday, July 2, 2012

Minimize Your Application To The Tray - VB#21



Code:
Resize Event:
If WindowState = FormWindowState.Minimized Then
    NotifyIcon1.Visible = True
    Me.Hide()
    NotifyIcon1.ShowBalloonTip(5000)
End If

Notifyicon double click event:
  Me.Show()
  NotifyIcon1.Visible = False

Exit context strip click event:
  NotifyIcon1.Visible = False
  End
        
Closing event:
  NotifyIcon1.Visible = False
  End
        
Show context menu strip click:
  Me.Show()
  NotifyIcon1.Visible = False



Monday, June 25, 2012

RGB Codes - VB#20


Code:
For the Sliders (put the minimum as 0 and the maximum as 255)
-Slider 1 is for the red
-Slider 2 is for the green
-Slider 3 is for the blue
-Slider 4 is for the opacity

Sliders_valuechanged event :

panel1.backcolor=color.fromargb(Trackbar4.value, Trackbar1.value,  Trackbar 2.value,  Trackbar 3.value)
label1.text="Color("+ Trackbar 4.value+", "+ Trackbar 1.value+", "+ Trackbar 2.value+", " Trackbar 3.value+")"



Share