Load available printers to a combobox

Post

Posted
Rating:
#1 (In Topic #1340)
Regular
theyikes is in the usergroup ‘Regular’
hi guys! I'm not quite sure if this qualifies as a QUICK question but I'm gonna ask anyway!

Ok i'd like to populate a combobox with a list of available printers.

I'm aware lpstat -a in a konsole yields the result I'm after but I can't figure out how to apply that to Gambas!

Anyone able to help me out…….. again! :)
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
If you want to know a list of available printers in your system, maybe this page can help you:

Individuare le stampanti presenti nel proprio sistema - Gambas-it.org - Wikipedia

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
Try this routine (Note: I did write it here without testing and  the IDE for syntax control):

Code (gambas)

  1. Public Sub ListPrinters()
  2.  
  3.   Dim aPrinters As String[]
  4.   Dim sOut, sLine As String
  5.  
  6.   Exec["lpstat", "-a"] Wait To sOut
  7.   aPrinters = Split(sOut, "\n")
  8.   For Each sLine in aPrinters
  9.     Print sLine
  10.   Next
  11.  

The Print instruction can be replaced by placing it in for example a ListView.

You can further refine the output by searching each sLine for the position of the text 'accepting' and use some String functions to cut of the line and have just printer name remaining. Look for functions like Instr() to determine position and Left() etc. to remove the unwanted text.


Enjoy

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
Well, here is my solution. The code requires that ComboBox1 to be on the Form.

Code (gambas)

  1. Public Sub ListPrinters()
  2.  
  3.   Dim sPrinters As String[]                                                     'To store the printers
  4.   Dim sList, sDefault As String                                                 'sList will store all the Printers, sDefault will store the Default Printer
  5.   Dim iLoop As Integer                                                          'Loop counter
  6.  
  7.   Shell "lpq" To sDefault                                                       'Get the Default Printer
  8.   sDefault = Left(sDefault, InStr(sDefault, " ") - 1)                           'Shorten the returned description
  9.  
  10.   Shell "lpstat -a" To sList                                                    'Get a list of all the Printers
  11.  
  12.   sPrinters = Split(sList, gb.NewLine, "", True)                                'Split the list and store in sPrinters
  13.  
  14.   For iLoop = 0 To sPrinters.Max                                                'Loop through all the Printers  
  15.     sPrinters[iloop] = Left(sPrinters[iloop], InStr(sPrinters[iloop], " ") - 1) 'Shorten the returned description
  16.   Next
  17.  
  18.   ComboBox1.List = sPrinters                                                    'Add the Printers to the ComboBox
  19.   ComboBox1.Text = sDefault                                                     'Display the Default Printer
  20.  
  21.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
If you are planning on using gb.form.print you can maybe use the Printer.List property?

Code (gambas)

  1. ComboBox1.List = Printer.List
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
theyikes is in the usergroup ‘Regular’
 Thanks for all the replies . Cogler your's worked perfectly. This is why i love linux, the community support is amazing!
Online now: No Back to the top
1 guest and 0 members have just viewed this.