How To Generate Random Numbers In C#

This is a simple tutorial
   
Random random = new Random();
int num = random.Next(1, 100000);
it generate numbers between 1 - 100000 , you can also change them . if you want to generate numbers between 1 - 500
you can use this
   
Random random = new Random();
int num = random.Next(1, 500);
just a simple change
make a new project and add a label and a button and use this code to generate random numbers
  
  private void button1_Click(object sender, EventArgs e)
        {
            Random random = new Random();
                        int num = random.Next(1, 100000);
                        label1.Text = Convert.ToString(num);
        }
   
you can even use a textbox instead label

Useful Snippets For C#

Opening another form

new Form2().Show();

Minimize Form 

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;

Maximize Form

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

Copy A File

File.Copy("C://program files/test.txt", dialog.FileName);

Delete A File

File.Delete("C://program files/test.txt");

Open A Website

System.Diagnostics.Process.Start("http://www.ultimateprogrammingtutorials.blogspot.com");

How To : Matrix on C# Console

hello all , after a long time i am back to write , so today i am gonna teach you to create a matrix screen on c# console , its pretty easy so lets begin making it , open your visual c# 2008/2010 and select console application and name it whatever you want now add this code


#define readkey
using System;

namespace m7tr1x
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Matrix";
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WindowLeft = Console.WindowTop = 0;
            Console.WindowHeight = Console.BufferHeight = Console.LargestWindowHeight;
            Console.WindowWidth = Console.BufferWidth = Console.LargestWindowWidth;
#if readkey
            Console.WriteLine("Press Any Key to Continue");
            Console.ReadKey();
#endif
            Console.CursorVisible = false;
            int width, height;
            int[] y;
            int[] l;
            Initialize(out width, out height, out y, out l);
            int ms;
            while (true)
            {
                DateTime t1 = DateTime.Now;
                MatrixStep(width, height, y, l);
                ms = 10 - (int)((TimeSpan)(DateTime.Now - t1)).TotalMilliseconds;
                if (ms > 0)
                    System.Threading.Thread.Sleep(ms);
                if (Console.KeyAvailable)
                    if (Console.ReadKey().Key == ConsoleKey.F5)
                        Initialize(out width, out height, out y, out l);
            }
        }

        static bool thistime = false;

        private static void MatrixStep(int width, int height, int[] y, int[] l)
        {
            int x;
            thistime = !thistime;
            for (x = 0; x < width; ++x)
            {
                if (x % 11 == 10)
                {
                    if (!thistime)
                        continue;
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.SetCursorPosition(x, inBoxY(y[x] - 2 - (l[x] / 40 * 2), height));
                    Console.Write(R);
                    Console.ForegroundColor = ConsoleColor.Green;
                }
                Console.SetCursorPosition(x, y[x]);
                Console.Write(R);
                y[x] = inBoxY(y[x] + 1, height);
                Console.SetCursorPosition(x, inBoxY(y[x] - l[x], height));
                Console.Write(' ');
            }
        }

        private static void Initialize(out int width, out int height, out int[] y, out int[] l)
        {
            int h1;
            int h2 = (h1 = (height = Console.WindowHeight) / 2) / 2;
            width = Console.WindowWidth - 1;
            y = new int[width];
            l = new int[width];
            int x;
            Console.Clear();
            for (x = 0; x < width; ++x)
            {
                y[x] = r.Next(height);
                l[x] = r.Next(h2 * ((x % 11 != 10) ? 2 : 1), h1 * ((x % 11 != 10) ? 2 : 1));
            }
        }

        static Random r = new Random();
        static char R
        {
            get
            {
                int t = r.Next(10);
                if (t <= 2)
                    return (char)('0' + r.Next(10));
                else if (t <= 4)
                    return (char)('a' + r.Next(27));
                else if (t <= 6)
                    return (char)('A' + r.Next(27));
                else
                    return (char)(r.Next(32, 255));
            }
        }

        public static int inBoxY(int n, int height)
        {
            n = n % height;
            if (n < 0)
                return n + height;
            else
                return n;
        }
    }
}


now run the application , enjoy

How To Change Extensions In VB.NET

In this tutorial i'm going to tell you how to change file extensions.
     Dim type As String = ".mp4"
        MsgBox(TextBox1.Text + " is changed into a " + type + " file.")
        Dim oldFile As String = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 4)
        FileCopy(TextBox1.Text, oldFile + type)
How to use it:
You have a form containing: 2 buttons, 1 textbox (or combobox)
When you click button one you can say it must open a openfiledialog:
Dim ofd As New Openfiledialog
If ofd.showdialog = dialogresult.OK then
TextBox1.Text = ofd.filename
End If
If we click the button, a openfiledialog will appear and we can choose a file name and if we click on 'ok', then the textbox will contain the filename of the openfiledialog.
Now double click on the other button and fill in the code above.
That's it!

How To Make Fade Effect In VB.NET

fade in / out , fade in when your app starts and fade out when your app getting closed .

you just need to add these codes , fade in for form_load and fade out for form_closing.

Public Class Form1
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        For FadeOut = 90 To 10 Step -10
            Me.Opacity = FadeOut / 100
            Me.Refresh()
            Threading.Thread.Sleep(50)
        Next
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For FadeIn = 0.0 To 1.1 Step 0.1
            Me.Opacity = FadeIn
            Me.Refresh()
            Threading.Thread.Sleep(100)
        Next
    End Sub
End Class
preview -

How To Get The Source Code Of A Website Using Web Browser And HttpWebRequest In VB.NET

this is going to be a simple tutorial , there are two ways to get the source code of a website in vb.net you know what are they so lets start making them

Web Browser

1.open your visual basic 2008/2010

2.create a new project and name it whatever

3.add a webbrowser from toolbox and place it some where else

4.add a richtextbox and a textbox

5.add a button and change the button text property to get source code

6.double click your form and erase everything add this code
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        RichTextBox1.Text = WebBrowser1.DocumentText
    End Sub
End Class
7.debug

preview -



HttpWebRequest

httpwebrequest is fast and more efficient than webbrowser 

1.open your visual basic 2008/2010

2.create a new project and name it whatever you want

3.add a richtextbox and a textbox

4.add a button and name it get source code

5.double click your form and erase everything and add these code
Imports System.Net
Imports System.IO

Public Class Form1

    'Author : Mohamed Shimran
    'Blog : http://www.ultimateprogrammingtutorials.blogspot.com

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(TextBox1.Text)
        Dim response As System.Net.HttpWebResponse = request.GetResponse()

        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())

        Dim sourcecode As String = sr.ReadToEnd()

        RichTextBox1.Text = sourcecode
    End Sub
End Class
6.debug

preview - 


How To Get IP Address From Host Name In VB.NET

This tutorial is about coding a Host To IP Address Program , how this works ? this finds the ip address of the host or i can say it converts the domain name to ip address so lets begin making this

1.open your visual basic 2008/2010

2.create a new project and name it whatever you like to

3.add a button and change its text property to get ip

4.add two textboxes

textbox1 is for host

textbox2 is for ip address

5.add two labels and change their text properties as below

label1 - host : 
label2 - IP Address :

How To Make A Credit Card Determiner In VB.NET

Today, we are going to make a Credit Card Determiner in VB.NET.
First if all open your Visual Studio or Visual Basic, create a new Project, add two text boxes & a button, change the button text property to check or whatever you want.

I recommend you to set your tools like this

How To Delete/Copy/Move A File In VB.NET

Deleting A File

Deleting a file is pretty easy , to delete a file you can use the delete method of System.IO

    Dim DeleteFile As String

        DeleteFile = "C:/users/shim/desktop/test.png"

        If System.IO.File.Exists(DeleteFile) = True Then

            System.IO.File.Delete(DeleteFile)
            MsgBox("File Deleted")

        End If

this is the directory of the file C:/users/shim/desktop/test.png