Category Archives: Windows

VB6: List default printer using code in Windows


kernel32 hold lots of interesting function that you may not utilized yet. With the Profile String functionality programmers can list default printer name, with serial port, Driver etc. Is that interesting.

Just create a bunch of function and call it.

Declare the functions

Create the following functions and Alias which is the prerequisite for our program.

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Define the GetDefaultPrinter function

Let create our custom functions for fetching the default printer.

Public Function GetDefaultPrinter() As Printer

Dim strBuffer As String * 254

Dim iRetValue As Long

Dim strDefaultPrinterInfo As String

Dim tblDefaultPrinterInfo() As String

Dim objPrinter As Printer

' Retreive current default printer information

iRetValue = GetProfileString("windows", "device", ",,,", strBuffer, 254)

strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)

tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, ",")

For Each objPrinter In Printers

If objPrinter.DeviceName = tblDefaultPrinterInfo(0) Then

' Default printer found !

Exit For

End If

Next

' If not found, return nothing

If objPrinter.DeviceName <> tblDefaultPrinterInfo(0) Then

Set objPrinter = Nothing

End If

Set GetDefaultPrinter = objPrinter

End Function

Using the function to list default

Drag and drop a command button and a Text Box to your form and drop these code into click event of the button.

Private Sub Command8_Click()

Dim objPrinter As Printer

Set objPrinter = GetDefaultPrinter()

Text2.Text = "Default printer is: " + objPrinter.DeviceName & _

Chr(13) & " Driver name is: " + objPrinter.DriverName _

& Chr(13) & " Port is: " + objPrinter.Port

Set objPrinter = Nothing

End Sub

Lets run and see what going on

VB6: List all printers using code in Windows


Listing all printers with name, Port in VB6 is simple as listing default printer port using Kernel32 library . Today we going show you how to do this.

As usual we start with function declaration, then define a new function which add all information into a list box. Here we goes

Kernel32 GetProfileString method

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Add printer information into a list box

Public Sub GetPrinterList(lstPrinter As ListBox)Dim PrintData As PrinterDim defprinterpos%

For Each PrintData In Printers

‘ Add printer name and port to list

lstPrinter.AddItem PrintData.DeviceName & ” at: ” & PrintData.Port

‘ Check for default printer

If PrintData.DeviceName = Printer.DeviceName Then defprinterpos = lstPrinter.NewIndex

Next

lstPrinter.ListIndex = defprinterpos%

End Sub

This sub procedure will take list box as argument, and add printer to this list box.

Call the sub

The final line of code just invoke the procedure and see what we have.

Happy coding

VB6: List all printers using code in Windows


Listing all printers with name, Port in VB6 is simple as listing default printer port using Kernel32 library . Today we going show you how to do this.

As usual we start with function declaration, then define a new function which add all information into a list box. Here we goes

Kernel32 GetProfileString method

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Add printer information into a list box

Public Sub GetPrinterList(lstPrinter As ListBox)
Dim PrintData As Printer
Dim defprinterpos%
For Each PrintData In Printers
‘ Add printer name and port to list
lstPrinter.AddItem PrintData.DeviceName & ” at: ” & PrintData.Port
‘ Check for default printer
If PrintData.DeviceName = Printer.DeviceName Then defprinterpos = lstPrinter.NewIndex
Next
lstPrinter.ListIndex = defprinterpos%
End Sub

This sub procedure will take list box as argument, and add printer to this list box.

Call the sub

The final line of code just invoke the procedure and see what we have. 
Happy coding

VB6: List default printer using code in Windows


kernel32 hold lots of interesting function that you may not utilized yet. With the Profile String  functionality programmers can list default printer name, with serial port, Driver etc. Is that interesting. 


Just create a bunch of function and call it.

Declare the function

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Define the GetDefaultPrinter function

Public Function GetDefaultPrinter() As Printer
    Dim strBuffer As String * 254
    Dim iRetValue As Long
    Dim strDefaultPrinterInfo As String
    Dim tblDefaultPrinterInfo() As String
    Dim objPrinter As Printer
    ‘ Retreive current default printer information
    iRetValue = GetProfileString(“windows”, “device”, “,,,”, strBuffer, 254)
    strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) – 1)
    tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, “,”)
    For Each objPrinter In Printers
        If objPrinter.DeviceName = tblDefaultPrinterInfo(0) Then
            ‘ Default printer found !
            Exit For
        End If
    Next
    ‘ If not found, return nothing
    If objPrinter.DeviceName tblDefaultPrinterInfo(0) Then
        Set objPrinter = Nothing
    End If
    Set GetDefaultPrinter = objPrinter
End Function

Use the function to list default  

Private Sub Command8_Click()
Dim objPrinter As Printer
Set objPrinter = GetDefaultPrinter()
Text2.Text = “Default printer is: ” + objPrinter.DeviceName & _
Chr(13) & ” Driver name is: ” + objPrinter.DriverName _
& Chr(13) & ” Port is: ” + objPrinter.Port
Set objPrinter = Nothing
End Sub

Auto complete Box

Add auto complete box for datagrid in C#


As we learned how to add an auto complete option for TextBox control in C#, we can also add this feature where a Text entry works. We are back with Datagrid example here. All you have to do is follow these steps.

  1. Drag and drop a Datagrid control to your C# Windows Application.
  2. Find Data Grid’s  EditingControlShowing event using the Properties window.
  3. Create a TextBox for auto completion, using DataGridViewEditingControlShowingEventArgs object.
  4. Check for exact column where we want to place auto completion box.
  5. Create auto completion string set using AutoCompleteStringCollection class.
  6. Fill Auto completion Mode,Source and custom source properties

The Code

 String t = dataGridView1.Columns[1].HeaderText;
            if (t.Equals(“Items”))
            {
                TextBox auto = e.Control as TextBox;
                if (auto!=null) {
                    AutoCompleteStringCollection autotxt = new AutoCompleteStringCollection();
                    auto.AutoCompleteMode = AutoCompleteMode.Suggest;
                    auto.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    autotxt.Add(“Citrizine”);
                    autotxt.Add(“CDT”);
                    autotxt.Add(“DAXIRGIN”);
                    autotxt.Add(“CX4”);
                    autotxt.Add(“DNT-Tab”);
                    auto.AutoCompleteCustomSource = autotxt;
                }
            }
It’s looks like simple, sin’t it?

Install packages with the Python package Index


PyPI – the Python Package Index is the largest Python package repository meant for the Python programming community. It consist of dozens of packages from programmers around the world which minimizes the coding tasks. Flask and Django were some of the top Python frame work in the PyPI.

The easiest way to install a Python Package from repository is pip command, a samll Python program. You can install the pip by download and compile the get-pip.py files.

Compiling get-pip

Go to your command prompt and locate the get-pip.py file and compile with Python interpreter
C:/>Python get-pip.py  and will install the Python Package Index command on your computer.

Installing packages

Lets install Django framework .
C:/> pip install django
will install the Django web frame work on your system , make sure your internet/Wi-Fi connection turned on.

Uninstalling packages

You can also use the Python Package Index (pip) to uninstall a package as follows
C:/> pip uninstall

HOW TO CONFIGURE PYTHON PATH ON WINDOWS?


Python is one of the hobbyist programming language .It laid backbone to many online service like Google search. Linux and other popular OS come with default Python installation but Windows doesn’t.
You can find a free copy from Python.org for your OS and can run the IDLE GUI/Command line for compiling python scripts and have fun. At the same time the Python interpreter is not available to run from all of your drives using MS Dos.
The Windows installer let you add the Python path to Windows, sometimes you need retouch to activate the option. The Installer add a ‘/’ at the end of the string, which Windows don’t like at all. Let’s fix it
Right click My Computer – Properties – Advanced System Settings and access the environment variables.
Find path value under System Variables.
 Remove the ‘\’ at the end of the string. You can also set the system path manually, remember the ‘\’ slash. A reboot will set the Python path active.

Reboot and launch command prompt try Python from any drive of your choice and it will invoke the Python command line.

Create a Auto generating Excel expense sheet with VBA


Microsoft Excel becomes one of my favourite application since I marry Excel macros, it help me lot to minimize tedious jobs. Here I will explain how you can perform an advanced Macro operation with the help of VB coding operations in Microsoft Excel 2013/2010/2007.

Making of Expense Sheet

 Firstly design your expense sheet as you like and Name the worksheet says ‘XP monthly’ which is short for Expense Monthly. It will be the base sheet which is working as source for additional sheet.

Sheet Renaming and checking for duplicate sheet

This task will require VB coding, just hit Alt+F11 to activate VB Editor and drop the following codes in Sheet1(XP Monthly).
Private Sub Worksheet_Activate()
Dim j, str, temp
If Name “xps” Or Name “XP Monthly” Or Left(Name, 3) = “XP-” Then

‘ FIND THE SPACE IN IN SHEET NAME FOR TITLE
For j = 4 To Len(Name)
 temp = Mid(Name, j, 1)
 If temp ” ” Then
   str = str & temp
 Else
  Exit For
 End If
Next
Range(“A7”) = str ‘ Name of the Month
‘………………………………………………………………………
End If
‘If (Left(Name, 3) = “xps” Or Name = “Monthly”) Then
Dim flag
flag = 0
‘ FIND THE PERIODS IN SHEET NAME
For j = Len(Name) To 1 Step -1
 temp = Mid(Name, j, 1)
If temp = “.” Then
 flag = 1
 Exit For
End If
Next
‘………………………………………………………………………
If (flag > 0) Then MsgBox “Naming is possible”
Dim nameStr As String
nameStr = “XP-” & MonthName(Month(Date)) & ” ” & Year(Date)
Dim i
‘ CHECK WHETHER THE SHEET NAME ALREADY TAKEN
Dim h, yy
For h = 1 To Worksheets.Count
 If Sheets.Item(h).Name = nameStr Then
  foo = True
  MsgBox “Sorry the XPs Sheet already exist for this month,Rename the sheet manually”, vbCritical, “Naming Error”
  Exit For
 End If
Next
If foo = False Then
 i = MsgBox(“Do you want to alter the worksheet name as ” & “‘” & nameStr & “‘” & “?”, vbYesNo, “Naming offer”)
 If i = vbYes Then Name = nameStr
End If
End If
End Sub

For avoiding the accidental renaming we add some numbers (NewXPS)with sheet name when it is duplicating.Above action will be perform when user switch between sheets during which Activate event of Sheet is fired.

Copying the Base sheet

Now we are ready create new Macro for copying the Base sheet for current month.Press Alt+F8 and enter macro name XPSheet and hit Create button and drop the following Macro code.

Sub NewXPS()
‘ NewXPS Macro
Sheets(“XP Monthly”).Select
Sheets(“XP Monthly”).Copy After:=Sheets(1)
ActiveSheet.Name = “XP-” & MonthName(Month(Date)) & ” ” & Year(Date) & ” ” & Rnd((1))
End Sub

The Rand() will generate random number file name, which used to avoid duplicating sheets. The sheet will be renamed later as current month, if there is no duplicate sheet otherwise you have rename it manually.

How to use this Macro enabled sheet

Whenever you need a new Expense sheet just run the Macro and switch between sheets once, it will rename automatically. VB Macro programming offer flexibility and ease of peace, working with MS Excel.

You can download and use complete Work Book from our Doc storage.

Create a bootable Windows USB in 5 minutes


Windows is my favourite OS since the Windows 7 edition. Usually we install most of OS from CD/DVD ROM. What about an USB or an SSD? You can do install Windows 7/8 from a boot able USB/Pen drive, but simply copying all CD contents to USB doesn’t work at all.

There are many programs which can reincarnate USB into bootable USB. But I suggest do it like an expert with some simple command.


First Step

Connect your device to a Windows installed system/PC also connect the USB want to use with a primary slot. We need at least 8 GB for installation purpose.

We are going to format the USB, make sure all the necessary data is backup from the storage device.

Preparing the USB with command prompt

Launch the command prompt with Admin privileges( Run -cmd and press enter) and  type c:/windows/system32 and issue the DISKPART command.  Now you have two command window.

Now list all disk partition with DISK LIST and identify your partition number, go to my computer and check volume size if had a confusion. The size will be 7534 MB for the 8 GB USB and I have disk with no 1.

Select the partition by issuing select disk [Disk #] command here it is SELECT DISK 1  for my computer, it may vary depending on the hard disks of PC you are using.

Now clean the disk. Issue CLEAN  command and make a new partition using CREATE PARTITION PRIMARY and make new partition active with ACTIVE command

Second Step

Need to format the newly created partition with the format command. FORMAT FS=NTFS QUICK. It will take some time to finish the process. Have a tea …. lol

Now we need two commands which let you go to the finalize step , ACTIVE and EXIT command (you will be automatically exit from the diskpart utility), don’t exit the command prompt.

Final Step

Go to your My computer and load the Windows installation DVD  and not the drive letter of DVD and US , suppose I have letter  ‘E’ for DVD and F  for USB.

Now the command prompt look like C:/Windows/System32>, OK ? lets make the bootable records follow these steps (issue following commands)

  1. E: CD BOOT and repeat CD BOOT until the prompt changed to E:/BOOT> (‘E’ stands for DVD Drive)
  2. Lets update USB Drive ‘F’ with BOOTMGR with compatible Bootcode, issue the BOOTSECT.EXE/NT60 F:(USB Drive).

If everything goes right( You will get the message ‘Bootcode was successfully updated on all targeted volumes’) , it will be OK for copying complete Windows (7/8/8.1)DVD content to the USB.

How to Boot

Choose  your BIOS feature to make your USB device booting device, the setting may vary depending on the different BIOS. Now you know how to install Windows for USB.

Connect the USB to a computer and restart , boot with USB…

Thank you for reading.