-1

I am attempting to pull in a list of printers on a persons computer and give that person the ability to select a printer. I am using a ComboBox to display the list of installed printers (but have tried multiple other Controls).

I have placed this code in several areas in an attempt to solve this issue:

private void PrintForm_Load(object sender, EventArgs e)
{
     foreach (string my_installed_printers in PrinterSettings.InstalledPrinters)
     {
          printer_list.Items.Add(my_installed_printers);
     }
}

Here is my code for detecting the index changing:

private void printer_list_SelectedIndexChanged(object sender, EventArgs e)
{
     Console.WriteLine("SelectedIndex: " + printer_list.SelectedIndex);
     Console.WriteLine("SelectedItem: " + printer_list.SelectedItem);
     Console.WriteLine("SelectedValue: " + printer_list.SelectedValue);
     Console.Write("Items: ");
     foreach (string my_item in printer_list.Items)
     {
         Console.Write(my_item + ", ");
     }
     Console.WriteLine();
}

It always returns:

SelectedIndex: -1 SelectedItem: SelectedValue: Items:

The strange thing is that I can still see all of the items in the ComboBox, but I cannot seem to refer to them in the code. The program doesn't think they exist. I am still pretty new with C# so I really appreciate the help.

  • Tried this code and problem not reproduced. Probably something else is at work here. Look at the properties modified on your ComboBox – Steve Jul 02 '16 at 23:20
  • I've deleted and created the ComboBox from scratch several times, without changing any properties and only using the code mentioned above. Still has the same issue. :( – Xyletic Jul 02 '16 at 23:27
  • Can you post where you're adding the items to the combo box? Maybe the problem is there. – Scott Hannen Jul 02 '16 at 23:29
  • Could you add a _Console.Writeline(printer_list.Items.Count)_ in the code of the event handler? – Steve Jul 02 '16 at 23:35
  • Steve, the count is consistently 0. – Xyletic Jul 02 '16 at 23:37
  • Scott, it's the first block of code in the post, nothing different. I can see the items, but when I select them, it's defaulting to -1 since it doesn't seem to see them within the code. – Xyletic Jul 02 '16 at 23:38
  • Like you are using a different combo than the one you have filled. – Steve Jul 02 '16 at 23:38

1 Answers1

0

So I figured it out... Like I said I'm still learning.

I had manually placed the InitializeComponent() within the form creation. After removing that separate command, the items were able to be selected as intended.

Thank you for your help and suggestions!