Adding items to RibbonDropDown at runtime - c#

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.

Related

User sortable list control

I need to give to a windows form user (C# 4.0 application) a way to act on a control showing a list containing several items. What is needed is to give to the user a way to select an item and then to move it upside or downside because changing the order. A possible solution is in the following image that shows a possible implementation using a couple of buttons (labeled + and -) to change a sort key value for each element then giving the reorder responsibility to an override of the alphabetical sort provided by a ListBox control. The change must persists until form disposal
I am wondering if there is a better or simpler way to get the same result; maybe there is some control featuring capabilities that I am unaware of. Thanks
There's no builtin control for this. The solution you suggest can easily be implemented using the Remove and Insert methods for the ListBox.Items collection.
Another solution could be to allow the user to drag & drop the items. Look at CodeCaster's link in the comments to your question.

Search Contract ListView always selecting first item in list

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.

Adding event in code to Drop-down list box in InfoPath

I'm trying to add the OnChange or Changed or IndexChanged event to the "Drop-down list box" control in InfoPath using VS Tools for applications.
If found the following code how this is done with buttons:
((ButtonEvent)EventManager.ControlEvents["ButtonName"]).Clicked += new ClickedEventHandler(FormCode_Clicked);
I would think this is also possible with other controls, but I can't seem to find the cast object to be able to add the event to my drop-down list.
((?)EventManager.ControlEvents["Project_x0020_Number"]).SelectedIndexChanged += new EventHandler(FormCode_SelectedIndexChanged);
I tried the object ComboBox, but then their was an error that this object could not be casted to the type ComboBox.
I hope someone can give me some advice. All I want to do is basically run a Query that filters my data depending on the selected value in my "Project Number" box.
maybe also good to mention: I'm changing the form that will go in a word document (used as template in SharePoint). I don't really think this matters, but thought I mention it any way.
Thanks in advance
Oxillery
You can approach this problem without writing code. I dont have Infopath in this machine to give you a sample. But I think you can solve this with the method described here in point 26:
http://www.bizsupportonline.net/infopath2003/avg-function-infopath.htm
Instead of calculating the average as in the example above you will set some parameters in your datasource and perform a refresh based on the value set in your dropdown.

Disabling item in a ComboBox C#

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.

ListView.Refresh 'clears' the listview

My code is looping through operations and is adding feedback to a listview control. Previously, my code has been working OK, but something has changed today...
The listview control starts off like this:
...but after I issue a call to Update() or Refresh(), rather than show the items that I have added, it renders like this:
At this point, the control is still enabled and visible. In fact the only interaction my code has with it is to add new items and Update().
To add items, I'm using the following:
lvwDrawings.Items.Add(new ListViewItem(new string[]
{
drawing.PartNo,
drawing.Revision.ToString(),
drawing.Issue,
drawing.DrgTypeText,
errorStatus !=null ? errorStatus : drawing.Status,
drawing.Filepath
}));
In case I'd introduced some problem here, I tested with the simpler lvwDrawings.Items.Add("test");, but the result was the same. If I do a QuickWatch, the control correctly tells me that it contains x items...
OK, I've found the problem - for some unknown reason, where I'd previously had lvwDrawings.Items.Clear() to clear the list between different runs, I somehow ended up with lvwDrawings.Clear(). This clears not only the items in the listview, but also the columns.
It's curious on two counts: I would have though that when I tried to add a ListViewItem with specific columns that it would have objected when there weren't any columns, and also, what on earth did I think I was doing when I made the change(??!).
Moderately interesting diversion:
I discovered the problem by creating a second listview below the first, and working through until I hit the problem. As part of that process, I added columns in the design, one of which I called Path, to which the designer didn't object.
However, in the code, references to methods of the static class Path (e.g. Path.GetDirectoryName()) resulted in an error - 'ColumnHeader Path does not have a method 'xxx'' or similar. Clearly, it assumed that when I referred to Path, I was referring to the column within the listview.
Furthermore, when I went to rename the column (to 'FullPath'), it renamed all references to Path in the code, eg. FullPath.GetDirectoryName()....

Categories