Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Archive for September 2009

Rediscover WinForm, landing Alien base?

leave a comment »

Simple scenario each time a province change, district, commune & village will reset, not a big deal for me now a day to have this scenario on the web application but hey look it WinForm :( let see

I started from Form Load event cbProvince combobox get binded then wire cbProvince with SelectedIndexChanged event handler to load list of district into cbDistrict combobox and so on the code look more or less like this


public void Form_Load(object sender, System.EventArgs e){
  // binding cbProvince
}

public void cbProvince_SelectedIndexChanged(object sender, EventArgs e){
  //... binding the cbDistrict ComboBox using current selected value from cbProvince
}

public void cbDistrict_SelectedIndexChanged(object sender, EventArgs e){
  //... binding the cbCommune ComboBox using current selected value from cbDistrict
}

Sound good all event wired hit F5 to try out. Guess what happen? it slow & AND finally throw an exception !!! but how? years passed and this behavior didn’t change a bit

// flag state
private bool formReady = false;
public void Form_Load(object sender, System.EventArgs e){
  // binding cbProvince
  formReady = true;
}

public void cbProvince_SelectedIndexChanged(object sender, EventArgs e){
  if(!formReady) return; // this is the trick don't fire the event
  formReady = false;
  //... binding the cbDistrict ComboBox using current selected value from cbProvince
  formReady = true;
}
public void cbDistrict_SelectedIndexChanged(object sender, EventArgs e){
  if(!formReady) return; // this is the trick don't fire the event
  formReady = false;
  //... binding the cbCommune ComboBox using current selected value from cbDistrict
  formReady = true;
}

Written by Chorn Sokun

September 29, 2009 at 10:50 pm