473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ComboBox: Cancel SelectedIndexCh anged event

7 New Member
Hi,
I have a ComboBox that contain ProductIDs and a DataGridView that lists Categories which a product can be a member of. My DataGridView list ALL categories, however, it has a checkbox column that get checked when the ProductID is a member of that Category, otherwise it is unchecked. A user may also check or uncheck a category checkbox then save.

The problem is, if a user forgot to save before changing the ComboBox, then his changes on the previous ProductID is gone!

How can I give the user a chance to either save or ignore the changes done on the previous ProductID (before selecting a new ProductID)?

I searched the internet for an answer and I found the following code but it is in C# and I am not sure if it solves the problem or not!!

Cheers

Expand|Select|Wrap|Line Numbers
  1. public class ComboBoxEx : ComboBox
  2. {
  3.    public event CancelEventHandler SelectedIndexChanging;
  4.    [Browsable(false)]
  5.    public int LastAcceptedSelectedIndex { get; private set; }
  6.    public ComboBoxEx()
  7.    {
  8.       LastAcceptedSelectedIndex = -1;
  9.    }
  10.    protected void OnSelectedIndexChanging(CancelEventArgs e)
  11.    {
  12.       var selectedIndexChanging = SelectedIndexChanging;
  13.       if (selectedIndexChanging != null)
  14.          selectedIndexChanging(this, e);
  15.    }
  16.    protected override void OnSelectedIndexChanged(EventArgs e)
  17.    {
  18.       if (LastAcceptedSelectedIndex != SelectedIndex)
  19.       {
  20.          var cancelEventArgs = new CancelEventArgs();
  21.          OnSelectedIndexChanging(cancelEventArgs);
  22.          if (!cancelEventArgs.Cancel)
  23.          {
  24.             LastAcceptedSelectedIndex = SelectedIndex;
  25.             base.OnSelectedIndexChanged(e);
  26.          }
  27.          else
  28.             SelectedIndex = LastAcceptedSelectedIndex;
  29.       }
  30.    }
  31. }
  32.  
May 21 '09 #1
3 12515
tlhintoq
3,525 Recognized Expert Specialist
I usually create a bool property called "IsDirty"
As soon as a change is made I make this bool "true", then call a method called "UpdateControls " which is a bunch of "enable" and "disable" actions based on the bool
When you save, mark the bool false and call "UpdateControls ()"

Expand|Select|Wrap|Line Numbers
  1. private void UpdateControls()
  2. {
  3. if (IsDirty)
  4. {
  5.      myComboBox.Enabled = false;// Now a change can't happen until I save
  6.      btnSave.Enabled = true;
  7.      this.Text = this.Text.TrimEnd('*') + "*"; // give the form title a * for dirty
  8. }
  9. else
  10. {
  11.      myComboBox.Enabled = true;
  12.      btnSave.Enabled = false;// Why show a save button if its not dirty
  13.      this.Text = this.Text.TrimEnd('*'); // Take away the * dirty mark
  14. }
  15. }
  16.  
May 21 '09 #2
almisba7
7 New Member
Hi tlhintoq,

Well, it can be done your way, I was hoping though if I can have a custom changes to the "SelectedIndexC hanged" event of the combobox,,, just like the C# code I included. By the way I am a VB programmer not C#, actually I don't understand C#, the reason why I included that code is because I found that code in a site and they say it suppose to solve my problem, but I don't know how to translate it to VB! Did you check that code? What do you see?? Do you think it can do the trick?

Cheers
May 24 '09 #3
tlhintoq
3,525 Recognized Expert Specialist
Did you check that code? What do you see?? Do you think it can do the trick?
I did put that code into my "BytesTesti ng" little project that use to check things out from here. Yes it works. But will it work the way you think it does, for your purposes? I don't know. But what is it that you think it will do? It is a control inherited from ComboBox. It has a couple values such as "LastSelectedVa lue". You still need to write code to check if LastSelectedVal ue is what you saved etc.
May 24 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
16235
by: ScottO | last post by:
I would like the user to have to select something in a System.Windows.Forms.ComboBox. private void MyForm_Load(object sender, System.EventArgs e) { ... comboBox.DataSource = data; comboBox.SelectedIndex = -1; ... }
0
5424
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this board. I have a form that contains a ListView and a GroupBox control. The GroupBox control itself contains several TextBox Controls. I have Validating and Validated event handlers set for the GroupBox control, and a SelectedIndexChanged event...
0
2052
by: Doug | last post by:
This is a repost of an item that I still cannot resolve. I have 3 combo boxes. The first leads to the second to the third. When I have selected a value in the second box, the third box shows the available information based on the second combo box selection. But if I change my mind and select a different item in the second box, after the third box has been populated, the third box still retains the information that was previously...
7
8804
by: sparkle | last post by:
Hi Everybody, I'm filling a combobox from a class, which works fine on it's own. But when I insert code to fill in other controls something in the combobox fill is causing the SelectedIndexChanged event to happen when the form (hence combobox) loads, not when I select something from the combobox. Do I have something in the wrong order? I've read that that sometimes
2
3692
by: blue_nirvana | last post by:
I use a AddHandler statement in the load event of a form to assoicate a routine with a combobox. When I populate the form, I select the approiate value from the combobox by using combobox.selectedvalue = value. The weird thing is sometimes this causes the assoicated routine to be called and sometimes it does not. The combobox below it that is completely identical except for the name works every time. After the form is displayed, you can...
0
1756
by: peter78 | last post by:
I wanted to implement an autocomplete feature on the combobox where you would type in partial text and it would try to match it for you. It doesn't exist in .Net yet, but I'm guessing it will in the next version. Here are instructions how to do it: http://support.microsoft.com/default.aspx?scid=kb;en-us;320107 There is one bug, though.
3
8782
by: MachuPicchu | last post by:
Hi, I'm refreshing a panel based on a selection from a winforms combobox. In my _SelectedIndexChanged event for the combobox, if changes to the panel's controls are present I am checking the response from a messagebox to proceed using MsgBoxStyle.YesNoCancel. When clicking cancel on the messagebox, is there a way to have the combobox rejected the changed index and revert back to the previous selection without setting the selectedItem?...
6
10822
by: tbrown | last post by:
I have a combobox with items like this: {one,two,three}. The selected index is 0, so "one" appears in the combobox text. When the user drops down the list, and selects "two", for example, I modify the Items collection to be {two,one,three} and now want "two" to appear in the combobox text. However, the combobox text is now blank. the is apparently somehow the result of having changed the combobox.Items collection. If, trying to fix...
2
2972
by: tshad | last post by:
In my VS 2003 Windows Forms page, when I initially fill my ComboBox (SystemList), it goes to the SelectedIndexChanged event which calls the Loademails() function. I then call it again in the Form1Load function. How do I get it not to call it in the SelectedIndexChanged from the Form1Load function? Normally, I want it to call it but not when I initally fill the ComboBox. *********************************************************...
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.