Ekona Pikin

Ekona Pikin

  • NA
  • 38
  • 12.8k

Printing listbox Items in order of occurence

Mar 5 2015 8:37 AM

I have an application that I have built and is just sort of printing app. First I populate ListBox and format it then goto c:\somefolder in some folder are pdf files it looks on listbox and see if there is any file name that matches an item on the listbox and sends it to the default printer.Everything is working great ,but say I have JohnDoe.pdf,ShahRukh.pdf,Vijay.pdf how can I make it to send the files in that order, for now it works great ,but I want to be able to print ==> JohnDoe.pdf first, then ShahRukh.pdf and so on and so forth.Please if you have any idea to spare is much welcome. Thanks in advance. This what I have right now it works great but print all items that match but randomly .I want it to respond or print matches in order of occurrence from top to bottom.This my code that does it but it just prints randomly any ideas.

private void btn_PrintMacro_Click(object sender, EventArgs e)
{
try
{
string dir = @"C:\slim\slimyyyy";//GOTO this directory ["C:||"==> and look for a folder called "WHAT---EVER"
if (Directory.Exists(dir))//If a directory defined above exists then do the followings
{
string[] pdf_specFiles = Directory.GetFiles(dir);
if (pdf_specFiles.Length > 0)

{
foreach (object item in listBox1.Items)
// foreach (object line in rchTxtContent.Lines)
{
foreach (string file in pdf_specFiles)
{
string fileName = Path.GetFileName(file);
if (fileName == item.ToString())
{
PrintDocument(Path.GetFullPath(file));
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex);
}
}

Answers (10)