I have found what seems to be an easy solution to disable certain items in a ComboBox in here. It states:
You can disable an item in a list box or combo box by adding a single
backslash to the beginning of the expression.
However if I write
testBox.Items.Add("\Test item");
or
testBox.Items.Add(\"Test item");
it gives a syntax error in VS2010. Maybe this function has been disabled in later than 2005 versions?
If I put an item through a VS2010 designer like this
\Test item
or I write
testBox.Items.Add("\\Test item");
then it appears with a backslash and not is disabled.
Thus my question is: is this method somehow available and I just fail to understand how to use it or I do have to create a custom ComboBox to achieve my goal (in title)?
sadly is it not possible for the combobox control.
I would recommend to just remove the item from the combobox list instead of trying to disable it.
with one of those 3 ways:
// To remove item with index 0:
comboBox1.Items.RemoveAt(0);
// To remove currently selected item:
comboBox1.Items.Remove(comboBox1.SelectedItem);
// To remove "Tokyo" item:
comboBox1.Items.Remove("Tokyo");
If you absolutely need to disable items, you will need to create a custom combobox.
UPDATE 1: This does NOT work, but I'm leaving it as is so the comments below make sense.
UPDATE 2: To answer your question... After a bit of googling around I believe your only option to achieve this with WinForms is to create your own control as you suggested.
I suspect the rules for working with items that begin with multiple backslashes would apply to escape sequences too. How about:
testBox.Items.Add("\]Test Item");
I'm not able to test it out, but it looks like it should work.
In general: You need to escape the backslash by writing \\. Otherwise the compiler tries to interprete \T as an escape sequence (which does not exist). I guess the designer does this for you already, but you can always take a look in the generated source code ;)
About disabling combobox items: The documentation you linked seems to apply for ListBoxes, not ComboBoxes. Furthermore, it refers to VisualFox Pro, not Windows.Forms. So I guess this won't work ;)
According to this discussion, you will need to subclass the control and overrride its paint handlers.
But before doing that, I would simply remove (or not even add) those items you wish to disable.
Related
I have a ToolStripMenuItem that serves as a drop down menu attached to the MenuStrip of my Form. The contents of this menu change depending on the status of the rest of the form and are populated in a loop.
The problem is that now I would like to add the functionality that the items in this menu have a keyboard shortcut based on their position in the list. The first item in the list should be Ctrl+1, the second Ctrl+2, etc.
Since ShortcutKeys is set using the Keys enum I do not know how I could map an incrementing value to the proper values in the enum. I had hoped something like this would work, but it did not:
newToolStripMenuItem.ShortcutKeys = Keys.Control | (Keys.D0 + menuItemNumber++);
Edit
There may be something else going on. According to this asker my initial solution should have worked, and the solution proposed by Migol produces the same result. The value in ShortcutKeys is shown to be some odd combination of characters with no apparent pattern.
You can use Enum.Parse method like this:
string enumName = "D" + menuItemNumber++;
newToolStripMenuItem.ShortcutKeys = Keys.Control | (Keys)Enum.Parse(typeof(Keys), enumName);
Enum.Parse documentation
This is embarrassing, but the problem completely unrelated to anything mentioned in this post. It was a stupid mistake made by my own bad programming habits in combination with a typo.
In my original ask I changed the name of my local variable from processorToolStripMenuItem to newToolStripMenuItem since this made more sense out of the context of the application. The item I was adding it to was a field named procesorToolStripMenuItem which as you can see is missing an 's'.
For the line of code setting the keyboard shortcut I made the same typo and was thus setting the keyboard shortcut of the wrong thing. They should never have been named something so similar to one another in the first place.
I have two doubts about the autocomplete feature of textboxes in C#.
First, I want to display the full list, not only the ones that start with the given text, and secondly I want to prevent the auto-complete of specific options (some are category titles).
I've been checking the textbox properties and there's nothing related to it, so probably the main question could be, Is there a way to modify / override the textbox events in order to handle the auto-complete actions? (I don't know if it applies to show the full list too)
I assume you're asking about a winforms textbox, as I dont think the WPF textbox supports autocomplete at all.
The base TextBox class will not support doing what you want, so you could in theory attempt to override all of the functionality in the TextBox class to do what you want, but the better idea would be to create a new custom control that inherits from TextBoxBase and implement the autocomplete behavior the way you want it.
I'm not sure about displaying the full list (perhaps a combobox or similar is more suited to this?) but you can definitely do something like this to swap which list of possible items can be displayed.
Another option, though one I like less, is to remove items you don't want to display at a given time from the collection dynamically, like this: textBox.AutoCompleteCustomSource.Remove("ACategoryTitle")
I could foresee that approach having many problems with trying to rebuild the list constantly. I would probably create a subclass of AutoCompleteStringCollection that wraps some LINQ code to nicely select the union of some lists and not others to display in the textbox.
I decided to build my own autocomplete tool with the help of a simple listbox and events, then I could achieve what I was expecting..
The CodingGorilla's answer probably leads to a better solution if you want something more decent, in my case for speed reasons I decided to do it that way but I'll mark his answer as the accepted in order to help other people who have the same doubt and they could consider that point..
My Search Contract populates a SearchContractResultsPages resultsList with objects, however when I try to select an item (which opens other page and passes the selection as a navigation parameter), the first item is always selected and passed as the parameter. I simply have no idea what to do to fix this, or what code needs changing, the resultsListView.SelectedItem is always the item opened, no matter what I actually select. The selection logic is carried out on the DoubleTapped event.
I don't know what code to post, so if anyone has any ideas, I will happily post I relevant parts you need to see.
Bit of a cop-out before I answer, but it depends...
If you are navigating to another page on SelectedItemChanged, the selected item should probably already be the correct one and you can use that as your navigation parameter. If this isn't working we will need to see more (come) code.
If you have an ItemClick event handler on the list view, you should use e.ClickedItem to get the correct reference back. Again, if this is what you are doing and it isn't working, we will need to see code.
Ah, I see you are switching selections on Double tapping the item. This could be the cause of your problem. Double-clicking to select is not one of the usual ways to select an item in a list. You should probably stick with the "small swipe" to select, as virtually all other apps in the ecosystem do. Not only will this let you verify what selection you have made before doing anything with it, but it will not confuse users since they're already used to the paradigm.
So I have a dropdown menu in a ribbon with contents that can be changed while it is being used. Outlook is also happy to let me 'add' or 'insert' items into it, as long as I do not add more than 1 item.
If I try to, I'll be told that the index is out of bounds rather than expanding the upper bounds for me.
I find that if I insert it into the collection in the designer portion of the code, it will work fine, but designer code is only run once, unless I Dispose the ribbon and re-create it.
Any ideas regarding how I can get this working
Try this. This should work for you.
RibbonDropDownItem item
= Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
item.Label = "First Name";
this.cbRecent.Items.Add(item);
Try the following directly inside the Ribbon Class:
RibbonDropDownItem item = this.Factory.CreateRibbonDropDownItem();
item.Label = "Text";
combo.Items.Add(item);
jeds, your approach doesn't work with "new". You have to use the "Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()". Otherwise, you are right and your approach works great with a RibbonGallery.
That approach also works great with a DropDown. I'm still often conflicted about which one to use...
However, other than those 2 objects (Dropdown and RibbonGallery), I believe drventure is correct. You simply have to stub out the objects ahead of time and use them as needed.
You can also use the XML Ribbon, but that creates an even bigger set of headaches (at least for my use cases).
Try using a Ribbon Gallery. I have been able to modify them during run-time with as little as
foreach (string s in list)
{
RibbonDropDownItem item = new RibbonDropDownItem();
item.Label = s;
rGallery.Items.Add(item);
}
where rGallery is a RibbonGallery.
Generally speaking, VSTO wants you to completely describe the UI elements you need one time, the very first time you're asked for them (via GetCustomUI).
I've run into similar probs before with vsto and about the only reasonable way around it I've found was to prepopulate (via the designer) all the elements you might need (so let's say 10 items in your drop down list).
Then, programmatically HIDE or SHOW those items and update their captions and other properties as necessary while your addin runs.
That way, you never have to dynamically add or remove anything.
I would like to have the standard silverlightcombobox behave like an html combobox.
So let's say I have a combobox for all states in the US, If I press the 'I' key, it should navigate the selected item to start at the I's ... Is there anyway to do this, it doesn't make sense that it's not built in functionality.
Maybe I missed the memo? Any ideas?
It's not something that comes as standard - even in Silverlight 4 (I hit this very problem today).
However, there are quite a few DIY implementations on the net:
http://gistom.blogspot.com/2009/12/silverlight-combobox-with-keyboard.html
http://www.codeproject.com/KB/silverlight/ComboBoxKeyBrdSelection.aspx
http://www.reflectionit.nl/Blog/PermaLinkd137c1f7-a515-4084-8199-f8b3cf892b8f.aspx
The author of the last post
created a small Behavior which fixes this problem. You can attach the KeyboardSelectionBehavior to a ListBox or ComboBox using Microsoft Expression Blend. You drag it from the Assets and drop it on your ComboBox or ListBox. If you have a custom ItemTemplate you will have to set the SelectionMemberPath property.
If you don't have access to Blend then just use the code as a template and edit the XAML by hand to produce the same result.