|
Hello all,
I have a form and a few comboboxes in it. When the user drops down a combobox and selects a different item, I wish to execute a piece of code, which involves a alot of database transactions. I am handling the SelectedIndexChanged(object sender, EventArgs e) event of the combobox. But this method gets called even if the user has selected the same item, which was previously selected. Am I handling the wrong event or is there anything more I need to do?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
brahmma wrote: Am I handling the wrong event
Ofcourse, you are handling the right event.
All you need is a simple if statement block like this
if(comb.SelectedIndex !== currentSelection)
{
/// All your code goes here
/// Where currentSelection is a user defined variable which must be
/// member of your Form class
}
Does this help
Mohamed Gouda
Egypt
|
|
|
|
|
Hello there,
Thank you very much for your inputs. I don't get it. Isn't the handler supposed to check it? The name of the handler is SelectedIndexChanged and why in the hell should I check if it is changed or not? Why does this event handler even exist? Shouldn't this event be triggered only when the selected index of the particular combobox is changed? Oh crap! Is this the way C# works? I think I am going back to MFC.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Unfortunately, there are a lot of PropertyChanged events in C# that are fired whenever a value is set whether a the value really changed or not.
And it is very annoying!
But going back to MFC isn't really an option for me
-^-^-^-^-^-
no risk no funk
|
|
|
|
|
Urs Enzler wrote: Unfortunately, there are a lot of PropertyChanged events in C# that are fired whenever a value is set whether a the value really changed or not.
And it is very annoying!
I'm annoyed too! But, I think I must get used to it rather than bitching about it
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Mohammed Gouda wrote: if(comb.SelectedIndex !== currentSelection)
What is that currentSelection here? Am I supposed to maintain a static variable where I store this information?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
brahmma wrote: Am I supposed to maintain a static variable where I store this information?
Yes, that is what I mean
Mohamed Gouda
Egypt
|
|
|
|
|
Then why do we have an OnSelectedIndexChanged event handler? Doesn't that suck?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Hello,
You are write!
Also the SelectedValueChanged has this behavior. It doesn't check for current setting!
Maybe the TextChanged helps you here. (I know that it could be multible text in the Items!)
All the best,
Martin
|
|
|
|
|
Martin# wrote: Maybe the TextChanged helps you here.
No, that has the same behavior too.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
brahmma wrote: No, that has the same behavior too.
Really?
I just tested it!
Selected same Item and event was not fired.
All the best,
Martin
|
|
|
|
|
Thanks for pointing it out. I had two projects open, and got confused between both. But I think I may run into trouble, because the combobox item text might be changed dynamically depending on a table updation in the database. Somehow, this problem is fixed for now. Thanks a bunch again.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Martin# wrote: I know that it could be multible text in the Items!
Exactly! What can I do in case I have multiple items with the same text in the ComboBox?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Hello,
brahmma wrote: What can I do in case I have multiple items with the same text in the ComboBox?
The question is what do you want it to do.
All the best,
Martin
|
|
|
|
|
Hmm... I want to execute a piece of code whenever the user changes his selection in a combobox. But the TextChanged handler may not work if I have duplicate items in the combobox. And the SelectedIndexChanged will be called even if the user selects the same item again, which sucks. So, do I need to check manually if the user has selected a different item every time and then execute my code? If yes, then why do we have event handlers?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Hello,
brahmma wrote: And the SelectedIndexChanged will be called even if the user selects the same item again, which sucks
So true!
brahmma wrote: So, do I need to check manually if the user has selected a different item every time and then execute my code?
I think in your special case, yes!
brahmma wrote: If yes, then why do we have event handlers?
I guess this statetment comes out of frustration.
An other possibility is, you do what MS was not willed todo:
inherit your own combobox, like this:
public class SpecialComboBox : System.Windows.Forms.ComboBox
{
private bool init = true;
private int oldIndex = 0;
public SpecialComboBox()
{
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
if((oldIndex!=this.SelectedIndex)|| init)
{
init=false;
oldIndex = SelectedIndex;
base.OnSelectedIndexChanged (e);
}
}
}
This should do it!
All the best,
Martin
|
|
|
|
|
Very good idea to create your own control that enhances performance of the vendor. I will vote 5 for your message Martin.
My greetings
Mohamed Gouda
Egypt
|
|
|
|
|
Thank you!
All the best,
Martin
|
|
|
|
|
Imagine an ERP package under development with some 300 forms (to which I have access). There are 500+ combo boxes. Now, I have to inherit my own combobox and then... Thanks, Microsoft.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
brahmma wrote: I have to inherit my own combobox and then
It's not good style but it works:
Change it directly in the .cs file (InitializeComponent code)
Replace in the editor "System.Windows.Forms.ComboBox" to "YoureNamespace.SpecialComboBox"!
-- modified at 7:20 Tuesday 10th July, 2007
All the best,
Martin
|
|
|
|
|
Hello C# guru's,
I've done my research but I just couldn't find one that does the following.
I have an MDI application. In the child form there is a panel whose height is dependent on the
current client area height of the child form. The child form is set to a default size when created but its size can be adjusted afterwards and can also be maximized. If I set the currently active child window to maximized and then open another child window, the panel height of the latter is set based on the default height of the child form even though it opens maximized. How do I make the child form calculate its actual client area whether it's maximized or not?
TIA!
I am a SysAdmin, I battle my own daemons.
|
|
|
|
|
Hey I’m developing a windows service (with C# .net 2005) to access the outlook 2003 calendar.
I’m running the service account as a Network Service
-------------------------------------------------------------------------------------------------
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.NetworkService;
----------------------------------------------------------------
Service is instlling properly. But when I strat the service error pope out saying unotheraized access at the point I create outlook object.
----------------------------------------------------------------
private Outlook.Application objOutlook = new Outlook.Application();
----------------------------------------------------------------
Error: An unhandled exceptoin (“System.UnathorizedAccessException”) occurred servicename.exe[2804]
|
|
|
|
|
Well your trying to access something and it's saying your unauthorised. Now feel free to call me crazy but it sounds like your unauthorised to access outlook from that account :P
It might be that thats not the problem but the easiest way to test is to give the service your user account and test it again. If the problem goes away then the network service doesn't have the permissions needed.
|
|
|
|
|
|
I am familier with MS Access rather than SQL
And to do that there, I choose my format in the database designe phase (Set the date field format to whatever you want). and do not need coding for this.
I think SQL Server provides same thing
Mohamed Gouda
Egypt
|
|
|
|