Click here to Skip to main content
15,795,789 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

I have created a form which produces barcodes, the user can select which printer tray from the default printer to use via a combo box, the bypass tray is best as the barcodes need to be printed on labels.

This code works ... but I have found that if a user tries to print out barcodes from our application as the first thing they do then the selected printer tray is ignored. If, however, the user opens MS Word and then sets the printer tray there to be bypass and prints off a document then returning to try and print barcodes again to the bypass printer works!!

Does anybody have any ideas, it appears that for whatever reason the printer tray is ignored the first time. The printer is a Xerox Workcentre 5645 and I'm using the PrintDocument command in my code.

Thanks,

Graham


Thanks for your response, the first section of code shows where I set the combo box up:

PrintDocument printDoc1 = new PrintDocument();
comboPaperSource.DisplayMember = "SourceName";
PaperSource pkSource;
for (int i = 0; i < printDoc1.PrinterSettings.PaperSources.Count; i++)
{
pkSource = printDoc1.PrinterSettings.PaperSources[i];
comboPaperSource.Items.Add(pkSource);
}


I then pass the combo-box value into a separate code file

BarcodePrinter tmpbcp = new BarcodePrinter(txtBarcodeData.Text, comboPaperSource.SelectedIndex);


Finally the code below is where I set the papersource

PrintDocument tmpprndoc = new PrintDocument();
tmpprndoc.DefaultPageSettings.PaperSource =
tmpprndoc.PrinterSettings.PaperSources[m_PrinterTray];


The interesting thing is that this codes works just not when the user runs as the first thing they do on their machine!
Posted
Updated 3-May-11 7:19am
v2
Comments
musefan 3-May-11 11:48am    
Sounds like your not setting the printer tray in your PrintDocument code, can you post some code to show what your app is doing
musefan 3-May-11 12:20pm    
I have deleted your solution and added the code to your original questions. If you add a solution then your Q&A will no longer show on the Unanswered list (which is bad) - I will look a your code now...
Graham Cockram 4-May-11 12:29pm    
Hi musefan,

Thanks for your comments I have tried your code and it is faster, however I haven't managed to solve the original problem. I think it probably is an issue with the printer etc.

Thanks again,

Graham.

I cannot see an obvious problem but I would personally do my code as follows...

PrintDocument printDoc1 = new PrintDocument();
comboPaperSource.DisplayMember = "SourceName";

foreach(PaperSource ps in print.PrinterSettings.PaperSources)
    comboPaperSource.Items.Add(ps.SourceName);


Then pass string value to BarcodePrinter class:

BarcodePrinter tmpbcp = new BarcodePrinter(txtBarcodeData.Text, comboPaperSource.Text);
//this sets variable _selectedPaperSource in BarcodePrinter class


Then set paper source:

PrintDocument tmpprndoc = new PrintDocument();
tmpprndoc.DefaultPageSettings.PaperSource.SourceName = _selectedPaperSource;


perhaps your issue is something to do with the paper source index, or maybe something complete different. You should do some debugging, use a Message box to show PaperSource.Source name that you pass to the BarcodePrinter class and also the one that is used after you assign it to tmpprndoc (so test tmpprndoc.DefaultPageSettings.PaperSource.SourceName before you print)

hope that helps in some way
 
Share this answer
 
Hello,

I used this code to change the printer tray in the code, maybe it could help you.

C#
    string _paperSource = "TRAY 2"; // Printer Tray
    string _paperName = "8x17"; // Printer paper name

//Tested code comment. The commented code was the one I tested, but when 
//I was writing the post I realized that could be done with less code.
	
    //PaperSize pSize = new PaperSize()  //Tested code :)
    //PaperSource pSource = new PaperSource(); //Tested code :)

    /// Find selected paperSource and paperName.
    foreach (PaperSource _pSource in printDoc.PrinterSettings.PaperSources)
    if (_pSource.SourceName.ToUpper() == _paperSource.ToUpper())
    {
	printDoc.DefaultPageSettings.PaperSource = _pSource;
    //pSource = _pSource; //Tested code :)
    break;
    }
    foreach (PaperSize _pSize in printDoc.PrinterSettings.PaperSizes)
    if (_pSize.PaperName.ToUpper() == _paperName.ToUpper())
    {
	printDoc.DefaultPageSettings.PaperSize = _pSize;
    //pSize = _pSize; //Tested code :)
    break;
    }
	 
    //printDoc.DefaultPageSettings.PaperSize = pSize; //Tested code :)
    //printDoc.DefaultPageSettings.PaperSource = pSource;	 //Tested code :)
	//My webPage www.grupokino.com :D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900