Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with combo boxes

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
0
0
CA
I have some code that checks a value in a combo box before it executes but it does not appear to be seeing the value selected in the combo box until I select the item in the combo box for a second time. Im using a case statement when determining what to do based on the values in the combo box.

My code looks like this:

private void optypCB_Click(object sender, EventArgs e)
{
switch (optypCB.Text)
{
case "Draw":
wearTB.Text = "4";
pinsTB.Text = "0";
nitrogenLowerCheckBox.Checked = true;
densityTB.Text = Convert.ToString(.35); break;
case "Trim":
wearTB.Text = Convert.ToString(4);
pinsTB.Text = Convert.ToString(4);
densityTB.Text = Convert.ToString(.35); break;
case "Form":
wearTB.Text = Convert.ToString(4);
pinsTB.Text = Convert.ToString(4);
densityTB.Text = Convert.ToString(.35); break;
case "Pierce":
wearTB.Text = Convert.ToString(4);
pinsTB.Text = Convert.ToString(4);
densityTB.Text = Convert.ToString(.35); break;
case "Blank":
wearTB.Text = Convert.ToString(4);
pinsTB.Text = Convert.ToString(4);
densityTB.Text = Convert.ToString(.45); break;
default:
throw new Exception("Unknown selection");
}
}
 
The Click event is fired before the value in the combox is changed. Use the SelectedIndexChanged event instead.
 
when i used switch (optyp.SelectedIndexChanged) I get an error saying:

The event
System.Windows.Forms.SelectedIndexChanged can only appear on the left hand side of += or -=

 
if optypCB is a combobox then the problem is your testing the text property instead of the selected item property. that's my guess anyway.

you could refactor the code to look something like this in the click event.
Code:
var adapter = new FormToInterfaceAdapter(this);
var value = text to compare against.
foreach(var command in commands)
{
   if(!command.IsSpecifiedBy(value)) continue;
   command.Process(adapter);
}
then now you need to create the specification, command and adapter objects.
Code:
interface IFoo
{
   int Wear {set;}
   int Pins {set;}
   double Density {set;}
}

class FormToInterfaceAdapter : IFoo
{
   private readonly MyForm form;
   public FormToInterfaceAdapter(MyForm form)
   {
      this.form = form;
   }

   public int Wear {set{form.wearTB.Text = value.ToString(); }}
   public int Pins {set{form.pinsTB.Text = value.ToString(); }}
   public double Density {set{form.densitTB.Text = value.ToString(); }}
}
Code:
interface ISpecification<T>
{
   bool IsSpecifiedBy(T item);
}

class StringSpecification : ISpecification<string>
{
   private readonly string valueToCompare;
   public StringSpecification(string valueToCompare)
   {
       this.valueToCompare = valueToCompare;
   }

   public bool IsSpecifiedBy(string item)
   {
       return valueToCompare == item;
   }
}
Code:
interface ICommand
{
   void Process(IFoo foo);
}

class DefaultValuesCommand : ICommand
{
   private int wear, int pins;
   private double density;

   public DefaultValuesCommand(int wear, int pins, double density)
   {
      this.wear = wear;
      this.pins = pins;
      this.density = density;
   }

   public void Process(IFoo foo)
   {
       foo.Wear = wear;
       foo.Pins = pins;
       foo.Density = density;
   }
}

public class ConditionalCommand : ICommand, ISpecfication<string>
{
    public ConditionalCommand (ISpecification<string> condition, ICommand command)
    {
       //ctor args as usual.
    }

    public bool IsSpecifiedBy(string item)
    {
        return conidtion.IsSpecifiedBy(item);
    }

    public void Process(IFoo foo)
    {
        command.Process(foo);
    }
}
now we just tie it all together
Code:
class MyForm : Form

ConditionalCommands[] commands;

//or whatever the form initialize event is

protected void Init()
{
   commands = new[]{
      new ConditionalCommand(
         new StringSpecification("A"), 
         new DefaultValuesCommand(1, 2, .3)),
      new ConditionalCommand(
         new StringSpecification("B"), 
         new DefaultValuesCommand(4, 5, .6))
   };
}

public void some_user_control_event(...)
{
   var adapter = new FormToInterfaceAdapter(this);
   var value = text to compare against.
   foreach(var command in commands)
   {
      if(!command.IsSpecifiedBy(value)) continue;
      command.Process(adapter);
   }
}
what we have done is separated the concerns into unique objects. then used composition to tie the objects together.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
You misunderstood me, you need to use the event...

Code:
private void optypCB_SelectedIndexChanged(object sender, EventArgs e)

rather than your current event

Code:
private void optypCB_Click(object sender, EventArgs e)

Ryan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top