How to prevent a gridview to get focus? - c#

I have 2 grids. When user is doing edits in one grid, I want to disable the other grid from getting focus, or atleast other grid from changing its foccussed row.
Example:
Parent grid, children grid.
If user is making edit in children of particular parent. I want to prevent user from suddenly shifting focus to different parent row. how to do that ?

The only ways you can prevent a control receiving focus is to change it's Enabled or Visible properties.
Simply changing the parent's Enabled property to "false" (e.g. for the CellBeginEdit) and then back to "true" (e.g. in the CellEndEdit event) will prevent the user from selecting a new parent row.

There are a lot of ways to do this but thats how I would do it (just a concept so ignore if any property does not match):
Create a property called mode and then use enumeration to set it to Edit or None etc.
Suppose you have GridViewParent and GridViewChild. In the FocusedRow event of child grid, at the start of all code, set the value of mode to Edit and at the end of all code in the event set it to None.
Then in the FocusedRow event of parent grid check whether the mode value is edit or not, if it is edit then use e.Cancel or something to get out of the focused event of the parent.
Now if you let me know exactly which grid are you using I might send you the code.

Related

In a property grid is there a way to unselect all grid elements programatically?

I am working on a project in which I am using a property grid to display the properties of the selected control.
The Property Grid is fixed to the left edge of the container and in the rest of the space I have the form I am designing.
On clicking a control on the form, the specific control’s property is getting selected.
In the above figure, I have selected the textbox and the textbox’s properties get shown on the propertygrid.
Here if you observe, by default, the Name property is highlighted as well.
Is there some way to unselect this property programmatically?
I have tried some suggestions online but none have helped. I am not able to find find a way to remove all selections from the PropertyGrid, but its behaviour seem to be different form a DataGrid...
Here is why I need this...
On selecting a control, if a property in the property grid is selected, then the property is getting modified.
For example, If i cut the control using Ctrl + X, the selected value in property grid is getting cut which in some cases is forcing user to set the property before modifying anything on the form.
I have tried selecting multiple controls, but in that case alse the selected property seems to be persistent
Since PropertyGrid uses DefaultProperty to select a property in its grid, as an option you can set DefaultProperty attribute at run-time for your object to a non-browsable property, for example:
this.propertyGrid1.SelectedObject = null;
TypeDescriptor.AddAttributes(someControl,
new Attribute[] { new DefaultPropertyAttribute("Site") });
this.propertyGrid1.SelectedObject = someControl;
Well, what you are trying are hacks. It is never a good idea to do such hacks particularly if you are not the only person that use the software.
In your case, the focus should be on the designer while you interact with it. So if the user press Ctrl+X, the designer should respond to the keyboard and it should not have any effect on the property grid (as only one control can have the focus at the same time).
Thus it is up to you to make sure that your designer is focusable, that it has the focus when initially displayed, that it get the focus when you press the TAB key. Pressing the TAB key again should put the focus on the property grid so that user can interact with the grid without using the keyboard.
If you have more than these 2 controls, then obviously TAB should also stop at any appropriate controls. Also, it can be a good idea to have some direct shortcuts like F4 to (show and) activate the properties pane.
If you are not able to make it works, then the best compromise would be to use another windows for the properties grid. By using a distinct Tool windows for the properties, it should not respond to the keyboard when the main windows has the focus.
Here are some links that might help you:
Panel not getting focus
Control.Focus Method() — See Remarks section.
In any case, you should not prevent Ctrl+X to works as expected when the property grid has the focus and a property is selected. Users don't like software that do not follows UI conventions.
As a software developer, you should as much as possible ensure that your application follows standard behaviors. I recommend you that you take one or 2 extra days developing your software properly instead of doing hacks.
Often, compromise to gain a few days will never be fix and will be a pain for many years. Better to do it right from the start. Unselecting an item in the property grid is not an acceptable workaround. Your manager should not allows you to do that.

wpf: how to edit selected item with possible cancellation?

I basically want to create something like this:
So, a user can add/remove items from the list and edit them in the red panel below the list. When the item is selected, the changes can be made in the panel.
Then, the changes can be either saved or canceled with one of the buttons below. User cannot select another item in the list without explicitly cancelling the changes.
How to do this? I only see the option of making the red panel a separate control and changing its DataContext manually on ListView.SelectedItem changes. The red panel's DataContext is a special wrapper on a ListView's item which has 'save' and 'cancel' options. ListView is set to IsEnabled = False so that its SelectedItem doesn't change when editing is in progress.
How would you do this?
The question is quite close from, for example, this one :
How do I stop binding properties from updating?
Anyway the WPF Object that will handle this is the BindingGroup :
http://msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup.aspx
the most easy apporach is to use a dialog for edit a selecteditem. thats what i do in my project. i use this dialogservice and handle the result.
if you want to handle all in one view you could set a property SelectionEnabled=false when the SelecteItem is set. and then SelectionEnabled=true when the save or cancel command is invoked.
the datacontext for your edit panel is simply your SelectedItem.
You can try to use bindings with UpdateSourceTrigger=Explicit. The blog post Edit With Explicit UpdateSourceTrigger will give you more information about how this can be implemented.

Preventing a control changing its Text property while it is not enabled

I have some DomainUpDown controls in my application. The behaviour is set like this:
Normal: Each control can individually goes Up/Down (Change selected item)
Special: If a check box is checked, Up/Downing the first control will cause the other controls to change as well
Now there are some cases that I want some of the controls to be disabled by setting Disabled = true. I did it, but in this case changing the selected item of the first control will cause the other disabled controls to change the selected item as well.
Is there a way to completly disable a control so it does not accepts something like control.Text = "bla bla" ?
P.S: I need not to hide the control!
One way:
You can hold a list of Textbox.Text values, which will hold entries for all of your textboxes.
Upon OnTextChanged:
if your textbox is enabled - update it's text property and store it up in the list.
if your textbox is disabled - set it's text property to the last value you had in your list.
You can use a Dictionary or anything similar that contain KeyValuePair objects.
I'm afraid there's not. At least not without additional work.

In Winforms, why is validation not being fired after leaving TextBox and entering a DataGridView?

I'm overriding the OnValidating event in a custom Winforms text box. I'm finding that if the text box (which is bound to an object) has focus and then I give a grid focus using the mouse, the OnValidating event doesn't always get fired. When I first give the grid focus, it gets fired fine. But, if put one of the grid's cell in edit (blinking cursor), from there on out it seems to not get fired when I go back between the text box and grid using the mouse. If I change focus using the tab key, the validating always gets fired. If I give focus to a non-grid control using the mouse, the validation is always getting fired.
I tried to recreate this functionality from scratch in a simple form and I can't recreate the problem. The grid I'm using in the setup where I'm getting the problem is a custom DataGridView with custom column types. I'm wondering if the grid is the problem. But, I don't see how it could affect the text box events. Any ideas?
It probably has to do with the CausesValidation property.
A control's validation is suppressed if focus is going to a control that has CausesValidation set to false. It's just a wild guess, but I'm thinking some control inside the grid has CausesValidation = false;
This property is meant for things like "Cancel" buttons, but can cause lots of confusion.

DataGridView cannot be Focused, but one of them can

I've come across the strangest bug pertaining to DataGridViews in Windows Forms.
I have a TabControl, that is supposed to contain a docked DataGridView in each tab page. I thought it would be convenient that the grid is focused upon changing the tab page, so that the user could simply hover the mouse over the grid and start scrolling when he changes the page. So, I just put a grids[tabs.SelectedIndex].Focus() in the event handler for changing the tab page.
However, something really strange happened. In my test application, I have three tab pages. If I try scrolling the grid right after starting the application, it doesn't work; I have to click in the grid first. I was expecting this. However, if I change the tab page, I can't scroll in any of the other grids until I click, except for the first one!
So, if I switch pages to the second page, then back to the first, I can automatically scroll that grid without clicking, but if I then switch to the third, I have to click for the grid to focus.
I had a look at the CanFocus properties of the grids, and it seems that only the first grid has it set to True. They are all created programmatically, and all in the same way. I don't see why they would be different.
Any ideas?
Inactive tab pages have their Visible property set to false. The documentation for CanFocus says:
In order for a control to receive
input focus, the control must have a
handle assigned to it, and the Visible
and Enabled properties must both be
set to true for both the control and
all its parent controls
Well, I solved it. Stupid programming error on my part, I had grids[tabs.TabIndex].Focus() instead of grids[tabs.SelectedIndex].Focus().
Oh well.

Categories

Resources