Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to change background and fore ground color of the text in console application using VB.Net

Author: DevASP
Download Source Code : 480_console_bkcolor.zip

In this simple article you will learn that how you can change background and fore ground color of the text in console application.

Simply just create New Project of console application in VB.Net:

 

Private Declare Function SetConsoleTextAttribute Lib "kernel32" _

          (ByVal hConsoleOutput As Integer, _

           ByVal wScreenColors As Integer) As Long

 

 

Private Declare Function GetStdHandle Lib "kernel32" _

       (ByVal nStdHandle As Long) As Integer

 

Private Const STD_OUTPUT_HANDLE As Integer = -11

 

 

Public Sub SetColor(ByVal forecolor As ConsoleColor, ByVal backcolor As ConsoleColor)

 

        Dim hConsole As Integer = GetStdHandle(STD_OUTPUT_HANDLE)

        backcolor = IIf(backcolor = 0, 256, backcolor * 16)

        SetConsoleTextAttribute(hConsole, forecolor Or backcolor)

 

End Sub

 

 

Public Sub SetColor(ByVal forecolor As ConsoleColor)

 

        Dim hConsole As Integer = GetStdHandle(STD_OUTPUT_HANDLE)

        SetConsoleTextAttribute(hConsole, forecolor)

 

End Sub

 

 

Public Enum ConsoleColor

 

        black = 0

        darkblue = 1

        darkgreen = 2

        darkaqua = 3

        darkred = 4

        purple = 5

        brown = 6

        grey = 7

        darkgrey = 8

        blue = 9

        green = 10

        aqua = 11

        red = 12

        pink = 13

        yellow = 14

        white = 15

 

End Enum

Now in main function write the following code:

 

Sub Main()

 

        Dim i As Integer

        For i = 0 To 15

 

            If i <> 14 Then

                SetColor(ConsoleColor.yellow, i)

                Console.WriteLine("www.DEVASP.net  Is Best")

            End If

 

        Next

 

        Console.ReadLine()

 

End Sub

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Capture And Save Form Using Interop and C#

Disclaimer - Privacy
© 2002-2017 DevASP.net