Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to clear a combobox?
Message
 
À
28/07/2004 11:36:56
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
00898211
Message ID:
00929265
Vues:
29
Bonnie,

Are you suggesting that it is necessary to bind the combobox SelectedValue to something in order for the design to work? In my form, there really isn't anything to bind the combobox SelectedValue to. The value gets used as a parameter to other methods, but there is no object to bind it to.

I tried creating a form property to bind the SelectedValue to:

this.cboDivision.DataBindings.Add("SelectedValue",this,"Division_pk");

but this gave an error, too. I'm not clear on what properties of what objects you can bind things too. Anyway, it would be a kludge.

Thanks for your patience. I believe you that your form works. My form has more than one combobox (3 in fact) in the same situation. Would I have to treat all of them this way, setting their properties in the Load event, before any of them come up empty? This is all so much trial and error (mostly error)!

Maybe I'll try creating a test form with only one combobox and nothing else and see it I can get that to work.

---Tim

>>>(Trying to add the databinding gave me an error "cannot bind to member or column corpunit_name", so I omitted that. I don't understand why you would include it.)
>
>At a quick glance I think *that* is the problem. You had the statement wrong and that's why you're getting a compiler error, but that's not the point. Notice in my example that I'm binding the .SelectedValue to a totally different DataSet, not the DataSet that is the DataSource for the Combo. IOW, what this binding is supposed to do is that once the user has selected a value from the Combo, it fills either a column in a Table or a property ... whatever you've bound to. Since you've removed that statement, the ComboBox is not bound to anything to be set when something is chosen.
>
>~~Bonnie
>
>
>
>>Bonnie,
>>
>>I've been away from this project for a while, but I recently got around to trying your suggestion. I reset (cleared) the comobox datasource, displaymember, valuemember and SelectedIndexChanged event handler in the designer. In imitation of your sample code, I added this code to the form Load:
>>
>>// Load Divisions into divisionAllDataSet and cboDivisions
>>LoadDivisions(); // this fills the data set
>>cboDivision.DataSource=this.divisionsAllDataSet1.corpunit;
>>cboDivision.DisplayMember="corpunit_name";
>>cboDivision.ValueMember="corpunit_pk";
>>cboDivision.SelectedIndex=-1; // this has no effect here (???)
>>//cboDivision.DataBindings.Add("SelectedValue",divisionsAllDataSet1,"corpunit_name");
>>this.cboDivision.SelectedIndexChanged += new System.EventHandler(this.cboDivision_SelectedIndexChanged);
>>
>>(Trying to add the databinding gave me an error "cannot bind to member or column corpunit_name", so I omitted that. I don't understand why you would include it.)
>>
>>No luck. The combobox initially displays the first item. I still have to use my Clear All button to clear it. Aargh!
>>
>>---Tim
>>
>>
>>>Thomas,
>>>
>>>I've messed with this some more, and here's what I found. In the form's load event, I'm simply setting up the Combo's DataSource, setting the SelectedIndex to -1 and setting the event handler. This combination seems to work ... my form comes up with the combo showing nothing.
>>>
>>>private void Form1_Load(object sender, System.EventArgs e)
>>>{
>>>    // this simply fills the table that is the source of the combo
>>>    this.FillCodesData();
>>>
>>>    this.cboCode.DataSource    = this.oCodes;
>>>    this.cboCode.DisplayMember = "Description";
>>>    this.cboCode.ValueMember   = "Code";
>>>    this.cboCode.SelectedIndex = -1;
>>>    this.cboCode.DataBindings.Add("SelectedValue", this.oData, "Code");
>>>
>>>    this.cboCode.SelectedIndexChanged += new System.EventHandler(this.cboCode_SelectedIndexChanged);
>>>
>>>}
>>>
>>>The only thing I can think of that might have been causing your problem is that maybe you didn't set .SelectedIndexChanged event handler here in the Load? Just a guess. But, this code works for me.
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>>Bonnie,
>>>>
>>>>Yes, I have a SelectedIndexChanged event handler for the combobox. If the index is not -1, the method sets the values of some other controls on the form according to the selected value in the combobox. There is no SelectedValueChanged event handler.
>>>>
>>>>I tried postponing the databinding of the combobox to the form Load method as you suggested, but this didn't work. Something is occurring after form Load that is changing MyComboBox.SelectedIndex.
>>>>
>>>>Do you know where I can find a description of the sequence of events that fire when a Windows form is instantiated and shown?
>>>>
>>>>I am not having any problem clearing other comboboxes on the form where I add values to the Items collection rather than binding to a dataset.
>>>>
>>>>---TML
>>>>
>>>>>Do you have a SelectedIndexChanged or SelectedValueChanged EventHandler? These events fire many times when a control is first instantiated. Typically what I do is to not DataBind and not set up the delegates for the eventhandlers until the Form load instead of in the Form's init. (This means having to do it manually in code instead of in the Property Sheet).
>>>>>
>>>>>~~Bonnie
>>>>>
>>>>>
>>>>>>I have a WinForm where the user can select criteria for a database search. On the form (among other controls) is a combobox which is bound to a table in a dataset. When the form appears, I want the combobox display item to be empty. So I created a method ClearAll() that sets
>>>>>>
>>>>>>this.MyComboBox.SelectedIndex = -1;
>>>>>>
>>>>>>I tried placing a call to this method at the end of the form Load method, after filling the dataset, but this did not work. However, I did add a command button to the form and put the call
>>>>>>
>>>>>>this.ClearAll();
>>>>>>
>>>>>>in the click event handler of the command button. This did have the desired effect of clearing the combobox display item. So apparently calling ClearAll in the Load method is too early in the sequence of events as the form becomes visible.
>>>>>>
>>>>>>How can I get the combobox to display empty initially? Is there a later form event that should handle this?
>>>>>>
>>>>>>TML
Thomas M. Lamm
Bradbury & Associates
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform