Load available printers to a combobox

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
theyikes
Posts: 23
Joined: Wednesday 18th September 2024 7:05pm

Load available printers to a combobox

Post by theyikes »

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! :)
vuott
Posts: 297
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Load available printers to a combobox

Post by vuott »

If you want to know a list of available printers in your system, maybe this page can help you:

https://www.gambas-it.org/wiki/index.ph ... io_sistema
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
gbWilly
Posts: 107
Joined: Friday 23rd September 2016 11:41am
Location: Netherlands
Contact:

Re: Load available printers to a combobox

Post by gbWilly »

Try this routine (Note: I did write it here without testing and the IDE for syntax control):

Public Sub ListPrinters()

  Dim aPrinters As String[]
  Dim sOut, sLine As String

  Exec["lpstat", "-a"] Wait To sOut
  aPrinters = Split(sOut, "\n")
  For Each sLine in aPrinters
    Print sLine
  Next

End


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
- Dutch translation for Gambas3
- Gambas wiki content contributer
- Gambas3 Debian repository


... there is always a Catch if things go wrong!
User avatar
cogier
Site Admin
Posts: 1158
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Load available printers to a combobox

Post by cogier »

Well, here is my solution. The code requires that ComboBox1 to be on the Form.

Public Sub ListPrinters()
  
  Dim sPrinters As String[]                                                     'To store the printers
  Dim sList, sDefault As String                                                 'sList will store all the Printers, sDefault will store the Default Printer
  Dim iLoop As Integer                                                          'Loop counter
  
  Shell "lpq" To sDefault                                                       'Get the Default Printer
  sDefault = Left(sDefault, InStr(sDefault, " ") - 1)                           'Shorten the returned description
  
  Shell "lpstat -a" To sList                                                    'Get a list of all the Printers
  
  sPrinters = Split(sList, gb.NewLine, "", True)                                'Split the list and store in sPrinters
  
  For iLoop = 0 To sPrinters.Max                                                'Loop through all the Printers  
    sPrinters[iloop] = Left(sPrinters[iloop], InStr(sPrinters[iloop], " ") - 1) 'Shorten the returned description
  Next
  
  ComboBox1.List = sPrinters                                                    'Add the Printers to the ComboBox
  ComboBox1.Text = sDefault                                                     'Display the Default Printer
  
End
User avatar
BruceSteers
Posts: 1838
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Load available printers to a combobox

Post by BruceSteers »

If you are planning on using gb.form.print you can maybe use the Printer.List property?

ComboBox1.List = Printer.List
If at first you don't succeed , try doing something differently.
BruceS
theyikes
Posts: 23
Joined: Wednesday 18th September 2024 7:05pm

Re: Load available printers to a combobox

Post by theyikes »

Thanks for all the replies . Cogler your's worked perfectly. This is why i love linux, the community support is amazing!
Post Reply