Event when finished selecting items? - c#

I have a ListView control that is displaying a list of files in "Details" mode. I will allow the users to select one or a number of these files at the same time.
I have an action I want to carry out on the selected files, however as it seems logical to me that I only initiate this action once I know what files are selected.
To clarify:
User selects one file - onSelectionFinished is fired and doThisAction(selectedFile[0]) can proceed.
User selects multiple files - onSelectionFinished is fired and doThisAction(selectedFile[0]) can proceed follwed by doThisAction(selectedFile[1]) etc...
I have tried using SelectedIndexChanged but when the user selects eg 3 files, my action routine is fired 6 times: Index 0, Indices 0, 1 and then Indices 0,1,2 - a very inefficient program!

If you allow the user to select multiple files then you're not going to know when the user is done selecting and you certainly don't went to run the operation with every selection change.
Instead of trying to react to selection events you should have a button (or some other control) that runs the operation(s) on the items selected in the list view. Only the user knows when he/she is done and will tell you.

You could throttle the SelectedIndexChanged event. If the user has not changed their selection in a certain time period then assume they are done and call your method. See here for an example.
However, it may be better for you to let the user decide when he/she is done as described by #Paul Sasik by means of a button click.

Dont set the selection event instead let user select items needed and place the listview mouseclick event and check if its the right mousebutton,and inside do the work
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//do work here
}
}
you can add a contextmenu to the listview so when user right-clicks it will add more flexibility(with more options on what to do to that selection):
MenuItem menuItem1 = new MenuItem("&Copy");
MenuItem menuItem2 = new MenuItem("&Delete");
contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem1, menuItem2 });
listView1.ContextMenu = contextMenu1;

Related

Tab index is not working on radio buttons

This is the part of my form that I am asking about
This is the tab index:
The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No
please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.
could you help me please?
Notice
both radio buttons has TabStop property to True
From How to: Set the Tab Order on Windows Forms (MSDN):
A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.
In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).
To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.
As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.
It worked for me!
first you have to create a method like this:
private void TabStopChanged(object sender, EventArgs e)
{
((RadioButton)sender).TabStop = true;
}
and then, put this in your Form_Load event:
private void Form_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(RadioButton))
((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
}
}
For radio buttons, you don't have to use Tab to navigate. Just use right and left keys to traverse radio buttons.
Check out this link to read more - https://www.csun.edu/universal-design-center/web-accessibility-criteria-tab-order

How to require ComboBox selection?

Working in Windows Forms (C#), creating a Wizard, I'd like to require the user to select an option in a combobox before being allowed to click "Next" to the next page in the form.
I thought I saw where to do this in the past, but I cannot find anything now.
Thx for any help...!
J
There are multiple ways of doing this. And different application use their preferred way.
One way to have an empty or 'Select Value' option at the top of the list of your combo box. Then when the user click the 'Next' button, check whether this is the value which is selected. If so, don't allow to go next. Otherwise allow to proceed.
My way is to set 'SelectedValue' property to -1 (means select nothing) and check whether is it -1 when the user press 'Next'. (If any valid value is selected, then this property should have a value higher than -1.)
Trigger on the selection changed event for the combo box, and then set the button enabled property:
private void comboBoxSelectionChanged(obj sender, EventArgs e)
{
nextButton.enabled = true;
}
There are many ways you can validate the selection ...or force a selection ...2 off the top of my head:
set the combobox to CausesValidation (IIRC) to true and handle xxxValidating(o,e) and xxxValidated(o,e) events
handle Next button's OnClick event and check the combobox SelectedItem or SelectedIndex properties:
/* sudo */
(o, e) => {
if(fooCombo.SelectedIndex == {...}) {
// show dialog, etc.
}
}

Disabling the onSelectedIndex changed event of WPF dropdown combo on navigating through arrrow keys

When I am having a value item selected in my WPf DropDown Combo Box then navigating using keys Left and right arrow keys result in firing of selected changed event for each item.
How to overcome this problem
The most easy and suitable way I found to overcome this problem is as follows:
rather than using SelectedIndexChanged event I used on DropDownClosed event and all code that is wriiten earlier inside selected index changed moved to this event under a if condition that checks whether a item is selected or not. Like this.
private void OnCmbOperatorsListDropDownClosed(object sender, EventArgs e)
{
if (cmbOperatorsList.SelectedIndex != -1)
InsertText(cmbOperatorsList.SelectedValue.ToString());
//Do whatever u want with selected item
}
So in this way when i navigate through Arrow keys SelectedIndexChagned event will not fired or since i haven't used that event so it will not create any problem.
As per my knowledge this is not possible straight away. I could have implemented this in a kind of "selection simulated" manner.
Handle arrow keys on combobox dropdown in PreviewKeyDown event by setting e.Handled = true. So that usual navigation based selection wont happen.
Inthese handlers based on Keys, change the Background and Foreground of the previous or next item from the drop down list so that it will look as if its selected and highlighted.
Then perform selection of the item which curently has the "simulated selection background - foreground" when dropdown closes. After dropdown closure, revert the background and foreground style.
But thats just my way of doing it.
You can use the PreviewKeyDown event like
private void combo_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Left) || (e.Key.Equals(Key.Right)))
{
((ComboBox)sender).SelectionChanged -= combo_SelectionChanged;
}
}
and if u want to attach that event you can add this PreviewMouseDown event.
This is what i tried and may not be a proper method of doing such cases

Detect if mouse click hits a item not in listbox [C#]

If the user click on a item in the listbox, the listboxItems_SelectedIndexChanged is called. But, even if the user miss an item and randomly clicks inside the listbox (not on items) the listboxItems_SelectedIndexChanged is still called.
How can I change this? I only want action on item click.
Note: removing the ability to navigate the application with keyboard is not a option.
I guess that in some cases you don't have enough list items in your control, therefore you have some space that you can click on and then SelectedIndexChanged is fired.
I guess you cannot dynamically resize the control to always fit the number of list items or else you wouldn't be asking this question.
Now, what should happen when the user click (selects) the same list item? Should some logic happen even though the selected index is the same (so when it was clicked the first time the same logic happend)?
If you require that selecting the same index more than once should be ignored then you could use the following hack:
Keep a variable at the form scope (the form containing the listbox control) and each time the selection index changes set that variable. Then use it later to check if the same selection has been made to ignore handling the event. Here is an example:
private int _currSelIdx = -1; // Default value for the selected index when no selection
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == _currSelIdx)
return;
Console.WriteLine(listBox1.SelectedIndex);
_currSelIdx = listBox1.SelectedIndex;
}
It ain't pretty, but hey...whatever works!
Maybe SelectedIndexChanged is not the right place to put your logic, since it is triggered even when you change the selection with the keyboard.
I would use MouseClick instead, checking if the click occurred over the selected item, i.e. something like this:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedIndex < 0 || !listBox1.GetItemRectangle(listBox1.SelectedIndex).Contains(e.Location))
MessageBox.Show("no click");
else
MessageBox.Show("click on item " + listBox1.SelectedIndex.ToString());
}
This link may help, instead of double click, implement the same for single click
i want to detect an item double click in a winforms listbox control. [how to handle click on blank area?]

A button handling same event for 2 different buttons depending upon which one is clicked

I an developing this UI, that has 3 buttons.
delete
referesh process
save
user selects the row from the data grid and clicks on any of the 2 buttons, i.e delete or refresh process, but until and unless user presses the save button the changes are not saved into data base.
i was thinking to have same save button for delete and refresh process, depending upon which button user has clicked, UI will call stored procs.
The application is on C#, WPF.
Please give me some idea on how can i handle this or some other suggestion.
Simplest way is to just store which button was clicked and what row was selected and process this data in the Save handler.
Clicking the Delete or Refresh will overwrite the two flags (whichButton and gridRow). Based on the value of whichButton your Save handler then does the appropriate logic.
This means you can save only one change, which is what you seem to say...
If you want to be able to queue more row changes then you need to store the two flags above in a container and process the entries one by one in the Save handler. Think about what happens if the same row is added to the container for both a Refresh and a Delete...
Depends on what do you have as a data source for the grid. If the backend supports Unit of work (like EF or NHibernate) then you change data directly and the context will resolve changes and their order when you call Save on the context.
If you use some bound-list of objects as a data source (a View Model, highly recommended for a WPF app) then you should add a new field to the ViewModel ChangeType = ChangeType.RefreshProcess or ChangeType.Deleted and then on Save you process actions based on the Changed field.
Assuming that you have the selected row with you. You could have global boolean variable isDeleteClicked. When user clicks the delete button, in its event handler, you could set isDeleteClicked = true; When user clicks refresh process button, in that button event handler, you could do isDeleteClicked = false; When save button is clicked, in its event handler you could do
if(isDeleteClicked)
{
//call SP for delete work
}
else
{
//call SP for refresh work
}
Or another option could be to create an enumeration wtih three values for example.
public enum OperationSelected
{
DeleteClicked,
RefreshProcessClicked,
NoneClicked
}
Create a global enum variable opSelected like OperationSelected opSelected = OperationSelected.NoneClicked; On delete button event handler do
opSelected = OperationSelected.DeleteClicked;
On refresh process button event handler do
opSelected = OperationSelected.RefreshProcessClicked;
On Save button event handler do
if (opSelected.ToString() == OperationSelected.DeleteClicked.ToString())
{
//call SP for delete work
}
else if (opSelected.ToString() == OperationSelected.RefreshProcessClicked.ToString())
{
//call SP for refresh work
}

Categories

Resources