How to mark a multiple selection on a DataGrid control? - c#-4.0

I've got a custom control based on a Datagrid. What I want to achieve is doing multiple selection of rows and then right click on the row selector to open a context menu.
What actually happens: Selection of multiple rows works fine until the user right clicks on the row selector. Once that is done, there is only one selected row the one over which the right mouse button was clicked.
What I need to happen: Allow the user to do a multiple selection (rows have a style for selection) and right click to open the context menu without loosing the previous selection.
What do I need to do?

No it doesn't happen like that at all. Make sure that you have DataGridView MultiSelect property set to TRUE. Then assign your ContextMenustrip control to the datagrid's ContextMenuStrip property.
That should do the work and multiple selection should remain on right mouse click as well unless in your code behind somewhere you are altering that.
In which case, you will have to debug and find out where and how.

Just revisited the code and found that I was not updating the SelectedItems IList:
Dispatcher.BeginInvoke(new Action(delegate
{
foreach (var item in e.RemovedItems)
{
SelectedItems.Add(item);
}
SelectedItemsList.Add(SelectedItem);
}), System.Windows.Threading.DispatcherPriority.ContextIdle, null);

Related

Listbox macro selects four listbox items when only clicked once

When I click the first item in a multi-select listbox the first click triggers the clicked item as well as items around it.
Many selected
I dug into why and added code to listen to the listbox selection changed event, adding only the selection changed listener and no code made it go away.
Private Sub AllListBox_SelectedIndexChanged
It comes back every so often. Maybe it has to do with the multiselect.
The values are added programmatically by looping through every header on the page and adding it as the value.
Dim InList As Boolean
For Each col In Range(DataRangeBox.Value).Cells
'Find the row with the headers on it
If col.row = Range(DataRangeBox.Value).Cells(1).row Then
'Add item to all available list box
If IsNull(AllListBox) Then
AllListBox.AddItem col.Text
End If
End If
Next col
Is it similar to the problem discussed here. Will adding the index explicitly solve the problem?
One selected now
Solved my problem.
On the listbox properties there is the attribute "Multiselect" and I noticed that it was on "fmMultiSelectSingle" when I was getting the problem. I knew I put it on "MultiSelectExtended" as its setting when I created it. So I played with it and once I had it on the one I wanted, I didn't see the problem. I could also reliably reproduce it by making it "fmMultiSelectSingle".
I'm having the same problem described: Usually, the first time (only) clicking an item in a listbox, I see 4 items selected (one clicked and 3 below it). For me, the listbox's Multiselect property is properly set to MultiSelectExtended so not what was described above. I've confirmed no code is setting the listbox property otherwise and I've tried manually changing the property to Single and then back to Extended but the problem persists.
I have my problematic listbox on a tab. After opening form/screen, user works in one tab before clicking on this other tab. (When going to this tab with listbox, some brief code runs.)
MY SOLUTION:
Today, I decided to add code to use SetFocus for the listbox after user clicks on that tab (at the end of the brief code that currently runs). So focus is on the listobx before I click any item in the list for the first time. This fixed my multiselect issue! Now, only the item clicked in list is selected, correctly.
I hope this idea helps someone else experiencing this quirky problem.

Delete value(s) in the list box by click a button

[22 Feb 2016 update]
Regarding my previous questions about list box, I can move values between two listboxes and save values after I receive useful answers.
However, if I retrieve those saved values, the listboxes can display the proper values but I cannot move those values and get exception message.
I can only move values between two listboxes if I don't save them.
Therefore, I am planning to have another button for delete listboxes values. I am not sure this is a good practice/design in xpages but I don't have a better method solve the exception.
I am sorry if I caused any inconvenience in this question. Thank you.
[23 Feb 2016 update]
According to the latest comments and answer, I notice that I made a big mistake because I mixed the document and value together.
I decide to break the design into few steps to find the problem occurs.
Due to I can move values between two listboxes and save them. I use another listbox test whether I can retrieve those saved values or not.
The third list box, I use View Scope Variable(similar to listbox B) but I use another variable name to avoid vague.
Here is my code of the third listbox:
var item = getComponent("comboBox4").getValue();
if ((item == null) || (null == item))
{
return "void";
}
else if ((item != null) || (null != item))
{
var lookupItem = #DbLookup(#DbName(),"ViewName", item,3 )
return lookupItem;
}
if (!viewScope.totalItems)
{
viewScope.totalItems = [lookupItem];
}
return viewScope.totalItems;
In the combo box, I use onchange partial update and apply to the third list box . When I run the program, I select a value from the combo box, the third listbox can display the relevant values that I saved before.
That part is fine, so I keep the third listbox for testing. And I put some dummy but unique data (to prevent confusion) related to the combo box.
Here is my first attempt: I choose a value from a combo box, the third listbox can display the relevant values that I saved. I move one value from listbox A to listbox B and click save. The third list box can reflect the value that I save.
In my second attempt: after the first attempt, listbox B still contains the value from listbox A, so this time, I move that value back to listbox A and click save. In the result, in the third list box, I see that value disappear.
At this moment, there is no value in listbox B and I add another two button and write similar code to pretend move values between the third list box and listbox B.
I test it, I select the value from the combo box, the third list box shows the proper values. But when I select a value from the third list box and click the button to move it to listbox A. I think that value will move to the listbox A but the result is nothing happens.
I try the other way, I select a value from listbox A and click the button to move it to the third button. Again the result is nothing happens.
After those fail attempts, I think the problem occurs in the buttons.
Here are the code of the two buttons
Button 1 (move value to the third listbox):
if (viewScope.ALstBoxItem) {
var sel = [].concat(viewScope.ALstBoxItem);
for (var i = 0; i < sel.length; i++) {
viewScope.totalItems.add(sel[i]);
viewScope.AselectItems.remove(sel[i]);
}
viewScope.totalItems.sort();
viewScope.ALstBoxItem = "";
}
Button 2 (move value to the listbox A):
if (viewScope.TotalItemsVariable) {
var sel = [].concat(viewScope.TotalItemsVariable);
for (var i = 0; i < sel.length; i++) {
viewScope.AselectItems.add(sel[i]);
viewScope.totalInItem.remove(sel[i]);
}
viewScope.AselectItems.sort();
viewScope.TotalItemsVariable = "";
}
Recall to comments in the question, I guess I should focus on the values in the listbox, not the button. I search on the internet about hide selected value and find these websites almost can give me the idea to solve the problem:
xpages hiding/showing fields based on a combobox value
Hiding based on previous combo box choice in xpages?
https://www-10.lotus.com/ldd/ddwiki.nsf/dx/dynamical_elements_on_xpages.htm
I try to apply those summary to suit my case but I still cannot move values after I save.
Grateful if someone can give advice please. Thank you very much.
Please step back and think about what you are building. You have two list boxes. They contain values, not documents, so a deleteSelectedDocument simple action can't work. Similarly, access to delete documents isn't relevant.
Also, from previous questions of this topic, I don't think you are setting values in the listboxes, you are using the events to set options, i.e. the options are not highlighted and so selected in the listbox, they are just sitting in the listboxes as options available for selection. Look at the code you're using to add options to the listboxes and use the corresponding code to remove the options.
I've added an image that corresponds to what I think you're trying to build, and the difference between "value" and "option", except in your example it sounds like you're moving the option from ListBox 1 to ListBox 2 as soon as it's selected, not using an "Add" button. Or to break the process down further, it sounds like, when an option in ListBox 1 is selected, you are looking to remove it from the list of options in ListBox 1 and add it to the list of options in ListBox 2. On your save, it sounds like you're wanting to store the options into a NotesDocument and, when you return, set the options for your ListBoxes based on what is stored in the NotesDocument.
If so, the questions and answers on this topic have given you everything you need to code those steps in the process.

VBA Listbox not updating to reflect new source list when called from itself

Edit:
Playing around further, it seems that you can't assign a new array to a listbox via the .list=somelist approach when the assignment happens in response to a click on the listbox itself. Or rather, you can assign the list, but it simply won't visually populate the listbox...although Excel seems to think it's there.
The .additem or .removeitem methods do however result in a visual change when called from a listbox click, which means you have to build your new array up that way I guess. How tedious.
You can however feed a listbox an entirely new array using the .list=somelist approach via a CommandButton-initiated action. But the user has to obviously click a button to do this. Which in my case below defeats the purpose.
Edit Over.
I'm designing a UserForm to help filter PivotTables. It's a cross between the existing Pivot Filter functionality and a Slicer. Here's what you see if you double-click on a PivotField header:
Note the Search field at the top, and the three buttons immediately below that search field. If you type something into that field, then instead of displaying everything that's currently filtered, you instead get a list of any matches, and you can then apply those search results to the underlying PivotTable via those three CommandButtons. The first cb simply filters the PivotTable to reflect the search, and the other two let you add or remove any search result from an existing filter.
I want to do away with those three command buttons, and instead (in the event that a search is performed) simply list those three options at the top of the ListBox above any search results returned. Clicking on those three options will then trigger the exact same code as currently triggered by the Command Button.
Here's how that looks currently if I actually type something in that Search box (Note I haven't yet removed the three command buttons this does away with from that Search frame):
I've added a simple bit of code to the lbResults_Change() event handler that checks if a user clicks any of those first three options. All that code does is trigger the exact same routines as would be triggered if they'd simply clicked on the actual command buttons themselves:
For i = 0 To 4
If Me.lbResults.Selected(i) Then Exit For
Next i
Select Case i
Case 0: cmdApplySearch_Click
Case 1: cmdAddToFilter_Click
Case 2: cmdSubtractFromFilter_Click
Case 3: Me.lbResults.Selected(3) = False
End Select
Here' the problem: The listbox gets updated just fine if I click on those command buttons, as you can see from the below. The Pivot has been filtered accordingly, and those contextual search options have been removed from the top of the listbox (and the search field cleared):
But the listbox does NOT get updated if the exact same routine is triggered from a click on one of those top three options in listbox itself:
As you can see from the above screenshot, it still shows those three options at the top, even though they are NOT in the array that I assigned to the listbox, as evidenced by the screenshot below:
But as you could see from the earlier screenshot, it shows 7 items in the ListBox instead of the three there actually are. But there are indeed just three items that should be showing in that list box:
? .ListCount
3
? .List(0)
263213: ICT Systems Test Engineer
? .List(1)
263299: ICT Support and Test Engineers nec
? .List(2)
839313: Product Tester
Basically, as soon as I try to update what's in the listbox via a click on the listbox itself, I can't update it.
It doesn't seem to matter if I set focus to something other than the listbox before the filtering code executes, and I've even tried completely clearing the Listbox with .clear. It just doesn't clear, until I manually click on one of those command buttons again.
Anyone have any pointers?
Ah, what an idiot I am for overlooking the painfully obvious solution. The ListBox list happily redraws in response to any event other than the ListBox_Click event, right? So all I needed to do was to use the ListBox_Click event to determine what got clicked (as I currently do), and then use the ListBox_MouseUp event to trigger the actual updating of the ListBox list. Works a treat.
Hopefully this post will save someone else two days of pain in the future. Probably me.

Select All checkbox lotus xpages

I'm working on an Xpages application on which I have a view control.
I tried to put a checkbox in the column header to select all of the check boxes in the view.
The problem is when I go to another page from the view, its lines are not checked and the selection is made only on the visible page.
So, I want to be able to select all the rows in all the pages of the view, this without the selection disappears when switching from one page to another of the view.
There are couple of problems with views and selections.
First of all, pager actions to move between pages does not process 'select all rows' data because it is enabled to use partial execution by default. If you put partialExecute="false" into your pager, you will see that 'select all rows' checkbox will be maintained between pages.
However, if you have a checkbox on a column and the columnHeader, the component maintains a selectedIds array in the back-end. Unfortunately, this array holds only visible selections. Because the array is maintained by the viewPanel component, which is not aware of the list of data entries that are not shown.
Also, checkbox implementation does not provide any even mechanism where you can grab selections on the back-end to cache them between pages.
To determine select all checkbox can be doable with a little trick. Assuming you are using all default styles;
<xp:inputHidden
id="inputAllSelected"
value="#{viewScope.allSelected}"
defaultValue="false"></xp:inputHidden>
<xp:scriptBlock
id="scriptBlock1">
<xp:this.value><![CDATA[
function getSelectAllCheckbox() {
return dojo.query("input.xspCheckBoxViewColumnHeader")[0];
}
function toggleSelectAll(){
dojo.byId("#{id:inputAllSelected}").value=getSelectAllCheckbox().checked;
}
dojo.addOnLoad(function() {
dojo.connect(getSelectAllCheckbox(), "onchange", toggleSelectAll);
});
]]></xp:this.value>
</xp:scriptBlock>
To cache checkboxes between pages, you would implement your own checkboxes by using custom columns. I recommend using a data table component to get more flexibility.

Action Button works differently if added to the DataView

I have a button that calls a Dialog that has a combo box bound to a sessionScope variable. The combo box contains a list of form names that I use to then call an XPage to create a new document. The first value in the forms list is "" so the user needs to select a value. I have a button with the onClick event of:
var c = getComponent("CreateDocDialog");
sessionScope.put("ssSelectedForm","")
c.show();
I have the button and dialog on a customControl with a repeatControl and when the button to open the dialog is clicked the ssSelectedForm is null and the current value of the combo is blank, which is exactly what I want it to be. However, if I add the same button and dialog to a customControl with a dataView control the dialog displays but ssSelectedForm is not null so the combo box displays the last value of ssSelected. I have placed the button outside the dataView, in several of the facets on the dataview, but all with the same results.
Very confusing.
Bill,
I am going to answer the same as my comment since I am pretty confident about what I said.
Go ahead and change the scope from sessionScope to viewScope. I had a similar issue once, and I 'fixed' it by clearing out the sessionScope variable after I used it. This worked, but I realized that it was not necessary, and that by changing to viewScope the lifecycle will be shorter and there was no need to clear out the value when I was done with it.
I don't know why this fails when added to the data view, but if this fixes it that would be all that matters.

Resources