Forum guidelines
  • Home
  • »
  • Printing
  • »
  • convert papersource.sourcename to papersource.kind
AuthorQuestion
convert papersource.sourcename to papersource.kind
Posted on: 20 Dec 2012
Hey 
I have 2 types of data, in the namespace System.Drawing.Printing:

Papersource.sourceName and Papersource.Kind.

SourceName gets the name og the papertrays from a printer, like "tray 1"

Kind gets the int value from the printer so fx. tray 1 have a kind value of 258.

in my combobox i only want to show the sourcenames, but if i choose "tray 1" i want to get a messagebox where i get the kind value of the sourcename.

Does anyone understand and have a example :)

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: convert papersource.sourcename to papersource.kind
Posted on: 21 Dec 2012   Accepted Answer
Try it like this:

PrinterSettings ps = new PrinterSettings();
PrinterData pd = new PrinterData();
PrintDocument printDoc = new PrintDocument();
PaperSource pkSource;
ArrayList arr = new ArrayList();

private void Form1_Load(object sender, EventArgs)
{
   for (int i = 0; i < printDoc.PrinterSettings.PaperSources.Count; i++)
   {
       if (printDoc.PrinterSettings.PaperSources[i].SourceName == "Frontoffice")
       {
          pkSource = printDoc.PrinterSettings.PaperSources[i];
       } 
       arr.Add(printDoc.PrinterSettings.PaperSources[i]);
   }

   comboPaperSource.DataSource = arr;
   comboPaperSource.DisplayMember = "SourceName";
   comboPaperSource.ValueMember = "RawKind";
   comboPaperSource.SelectedItem = pkSource;
}

Re: convert papersource.sourcename to papersource.kind
Posted on: 20 Dec 2012  
If you set the combobox's:

1  DataSource property to an array or List of PaperSource objects,

2. DisplayMember property to "SourceName"; and

3. ValueMember property to "Kind" or "RawKind"

then, when the selection changes, you can just show the current SelectedValue in the MessageBox:

MessageBox.Show(comboBox1.SelectedValue.ToString());

Re: convert papersource.sourcename to papersource.kind
Posted on: 21 Dec 2012  
This i my example

        PrinterSettings ps = new PrinterSettings();
        PrinterData pd = new PrinterData();
        PrintDocument printDoc = new PrintDocument();
        PaperSource pkSource;
        ArrayList arr = new ArrayList();

        private void Form1_Load(object sender, EventArgs e)
        {
            comboPaperSource.DisplayMember = "Frontoffice";
            for (int i = 0; i < printDoc.PrinterSettings.PaperSources.Count; i++)
            {
                pkSource = printDoc.PrinterSettings.PaperSources[i];
            }

            arr.Add(pkSource);

            comboPaperSource.DisplayMember = pkSource.SourceName;
            comboPaperSource.ValueMember = pkSource.RawKind.ToString();

        }

But is dosent work. you can see i use 2 displaynames, because the first is where i declare which printer i wanna have information from. and secondly there is no data in my combobox.

Re: convert papersource.sourcename to papersource.kind
Posted on: 09 Jan 2013  
Thanks mate.. it works perfectly :)
Re: convert papersource.sourcename to papersource.kind
Posted on: 11 Jan 2013  
do you know how to do this in a webform ?

cus i cant figure it out /:

the code attached is the same as the accepted question in here :)

or maybe have an alternative solution :i

Re: convert papersource.sourcename to papersource.kind
Posted on: 11 Jan 2013  
TBH, web applications are not really my thing but you will have problems with this line:

comboPaperSource.SelectedItem = pkSource;

because the SelectedItem property of the DropDownList (the equivalent of the ComboBox in ASP.NET) is read-only.

You'd need to replace it with a line such as this:

comboPaperSource.SelectedValue = pkSource.SourceName;

However, notice that this code will only show the papersources in relation to a printer attached to the server - not the client, which can't be accessed directly for security reasons.

To do the same for the client you'd either need to use javascript or to ask the client to install a custom windows control (impossible, of course, unless the client machine is running Windows).

Re: convert papersource.sourcename to papersource.kind
Posted on: 11 Jan 2013  
can i do somthing like saving the selectedvalue into a list or arraylist so i can get the rawkind number in this way ?

i dont need to display the rawkind to the user, but only the sourcename.. the rawkind number is only to change the papersource

Re: convert papersource.sourcename to papersource.kind
Posted on: 11 Jan 2013  
Sorry, in my previous post I should have set the SelectedValue to the RawKind property of the paper source:

comboPaperSource.SelectedValue = pkSource.RawKind.ToString();

That should cause the corresponding SourceName to be displayed in the DropDownList.







Re: convert papersource.sourcename to papersource.kind
Posted on: 11 Jan 2013  
public partial class _Default : System.Web.UI.Page
   
{
       
PrintDocument printDoc = new PrintDocument();
       
PaperSource pkSource;
       
List<object> listPaperSource = new List<object>();

       
protected void Page_Load(object sender, EventArgs e)
       
{
           
for (int i = 0; i < printDoc.PrinterSettings.PaperSources.Count; i++)
           
{
               
if (printDoc.PrinterSettings.PaperSources[i].SourceName == "test")
               
{
                    pkSource
= printDoc.PrinterSettings.PaperSources[i];
               
}
                listPaperSource
.Add(printDoc.PrinterSettings.PaperSources[i]);
           
}
           
DropDownList1.DataSource = listPaperSource;
           
DropDownList1.DataTextField = "SourceName";
           
DropDownList1.DataValueField = "RawKind";
           
DropDownList1.SelectedValue = pkSource.ToString();
           
DropDownList1.DataBind();
       
}
if i use selectedvalue instead, i get no errors, only when i compile, it says:
{"Object reference not set to an instance of an object."}
does it help you ? :) is it because of a postback ?
can i in someway use a 2 dimensional list to hold sourcename and rawkind value ? and in that way get the rawkind that is corrosponding to the sourcename?

Re: convert papersource.sourcename to papersource.kind
Posted on: 11 Jan 2013  
Well, you will only need to load the DropDownList once and so all the code in Page_Load should be subject to it not being a postback:

if (!IsPostBack)
{
   // code
}

I'd declare the three fields to be protected rather than private (the default).

Also this line:

DropDownList1.SelectedValue = pkSource.ToString();

should be:

DropDownList1.SelectedValue = pkSource.RawKind.ToString();

However, if pkSource is null (which would explain the exception), then that suggests that there isn't a paper source on the server printer with a SourceName property of 'test'.

It's certainly possible to create a Dictionary (say) to map the RawKind numbers to the SourceName but I don't really see the point of doing this when databinding the DropDownList to a List of PaperSource objects should achieve the same effect. 

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.