0

I recently started using Visual Studio to update some older python scripts into stand alone UIs. I have some python experience, but C# is fairly new. Currently I am working on a calculator that will take in two user inputs, and calculate a height and width based on the items in two comboboxes.

Basically the user inputs a pixel size and file size into a text box, then selects the type of imagery from combobox1 (BW (1), Color (3), or Color-IR (4)) for my calculations I will need to reference the (1, 3, or 4) associated to the specific imagery, and in combobox2 (8 bit, or 16 bit).

Ultimately, from these inputs I am going to calculate a height and width that we can use during my project prep process. I have set up my UI and have code for my pixel size and file size, and manually added my data into both comboboxes from the properties menu for the drop down list.

I would like to create specific variables from the items in the drop down menus to use throughout my math equations.

If I have a pixel size of .2, file size of 250mb, Color, and 16 bit... calculate.

A little help in pointing me towards creating a statement like...

IF (combobox1.text == "color") and (combobox2.text == "8 bit")

// Math

...or another recommendation on the best course of action for a C# noob such as myself would be greatly appreciated!

I have tried creating a dictionary for the combobox1 with the type of imagery and the number associated with it, but I don't fully understand how to pull the (3) from Color if that is what the user selects to apply in my equations.

public partial class Form1 : Form
{

    Dictionary<string, int> ColorBands = new Dictionary<string, int>
    {
        {"BW", 1 },
        {"Color", 3 },
        {"Color-IR", 4 }
    };



    public Form1()
    {
        InitializeComponent();
    }
    private void BT_HeightAndWidth_Click(object sender, EventArgs e)
    {

        try
        {
            // Calculate Height and Width from Pixel Value and Sheet Size.
            decimal PixVal;
            if (!decimal.TryParse(TXB_PixelSize.Text, out PixVal)) ;
            {
                MessageBox.Show("Pixel Size isn't valid!");
                return;
            }
            decimal SheetSize;
            if (!decimal.TryParse(TXB_SheetSize.Text, out SheetSize)) ;
            {
                MessageBox.Show("Sheet Size isn't valid!");
                return;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Skabrewz
  • 11
  • 4
  • It is easier to create a class to hold the values and override the ToString() method to alter what is displayed to the user. A sample can be found at: https://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – Wiz Aug 05 '19 at 19:40

1 Answers1

0

You're almost there.

You can use the SelectedValue property of the combo box to get the selected value. Then use the Dictionary.TryGetValue() to get the appropriate number.

var selected = combobox1.SelectedValue.ToString();
if (ColorBands.TryGetValue(selected, out int value))
{
    // Use it
}
Sach
  • 10,091
  • 8
  • 47
  • 84
  • Thank you, this helped clear up some of my confusion on how to call from the dictionary. I have an idea of how this should work, but not enough practice to be able to write it out in C# as I would if this were python. – Skabrewz Aug 05 '19 at 20:30