How do I disable the toolstripitems on BindingNavigator? - c#

I want to enable or disable the toolstripitems on the bindingnavigator programmatically.
When I try to set the toolStripitem's enable property, it does not change. It does not give any exception but the value of Enable property does not change.
What did I miss?

The BindingNavigator takes control of these buttons itself, to enable/disable them as appropriate; i.e. you can't go prev/next if there is no more data, you can't add/delete without the underlying source letting you (IBindingList.AllowNew/.AllowRemove.
Which buttons do you want to tweak? For example, you can disable add/remove by using a data-source such as BindingList<T>, and setting AllowNew=false/AllowRemove=false.
Alternatively, simply write your own buttons completely (forget BindingNavigator) - it isn't a lot of work, especially if you start with a BindingSource on the form (since that has all the necessary MoveFirst() etc.

Related

How to restrict control from initializing

I have multiple controls in my User controls like
text box, drowndown, listview , gridview and etc.
I have set some property in usercontrols which set enable and visible property of each control.
like isdropdownvisible, istextboxvisible and etc.
But I want those control which are set visible=false does not get initialized. so that processing.
Or suggest me another method which can enhance page speed
Since part of the initialization itself is the setting of the visible flag, i.e. the system does not know whether a control is visible or not until after it is initialized, I'm afraid what you ask for is not only impossible, but illogical as well.
If you have a problem that some controls have too heavy initializations, that are not needed immediately, you can load them in some dynamic manner, but I could not be more specific, without some example code.

Telerik Tab Control - Only the currently SelectedPage shows controls visibility correctly

I'm using the 2013 Q3 Telerik Tab control in a C# WinForms project. If I test the .Visible property of a control placed on a Page in the Tab then it will always return false unless I Select the page. Is there another property besides .Visible that can be used to test the Visibility of a control on a Page without having to select it?
Here's my dilatation:
You should not change the native meaning of the control's properties. Period. I asked in a comment what were you trying to achieve, because I suspected that you're basing some UI logic on some control's visibility, which in my opinion isn't too good approach. The visibility should be bound to the background logic, not the oposite. You're hiding controls for some reason, because at some point something happened - so keep track of that "something" instead of inspecting its results. IMO the Visibility property should be set, but never checked.
Alternatively if it's not possible to change the concept for some reason, as a workaround I'd attach a handler to the VisibleChanged event and set Enabled property if the sender is not a tab control - then by checking against Enabled property you'd know whether the control is in use or not. I find it difficult to imagine a situation in which I'd need to check whether a control is visible.

Conditionally validate Dynamically generated Controls in Silverlight

I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.

What is the main difference between ReadOnly and Enabled?

In Windows Forms controls, there are two properties: ReadOnly and Enabled.
What is the difference between these two properties? I feel like they behave the same way.
As it says in the following forum post:
In the context of a TextBox, readonly
allows the user to set focus to and
select and copy the text but not
modify it. A disabled TextBox does not
allow any interaction whatsoever.
Use ReadOnly when you have data that
you want the user to see and copy, but
not modify. Use a disabled textbox,
when the data you are displaying is
not applicable in for the current
state of a dialog or window.
Taken from: MSDN Forums
ReadOnly I generally associate with a TextBox or other control that contains text; it dictates whether or not the user can modify the text displayed by the control. The user can still select the text, though (e.g., to copy and paste it into another program).
Enabled basically controls whether or not any user interaction with the control is possible. For instance a Button with Enabled == false cannot be clicked; a CheckBox with Enabled == false cannot be toggled, etc. Note that a TextBox with Enabled == false also cannot have its text selected (that would be user interaction).
Furthermore, controls with Enabled == false do not raise events related to user interaction such as Click.
Enabled specifies whether user interaction is allowed. If a control is disabled then it will not generate any UI events.
ReadOnly determines whether the user can edit the contents of the control. For example, a ReadOnly TextBox cannot be edited, but you can still click on it, select the text contained within it, etc.
If you take a text box with a scrollbar as an example, ReadOnly does not allow the user to edit text, but the scrollbar is still active (think about licence boxes in installation programs). Enabled = false will cause the entire control to disable, not just the text editing area.
Some controls, e.g., buttons, can only be enabled or disabled. A disabled control will give a visual indication that it cannot be interacted with right now (typically by graying/fading out). Others can also be read-only, in that they can be interacted with in some way but not edited. For example, an up-down control is often like that, which means you can press the buttons to change the value but not edit it by normal typing. (It's also possible to have controls that you can never interact with but which are still usefully disable-able; a label can still be grayed out as part of indicating that a whole area of the GUI is not usable right now, which is a more pleasing visual effect than just changing the controls that are interactive.)
It's virtually always the case that being disabled implies that a control is also read-only. I've seen a few cases where that wasn't true, and it always felt more like a bug than anything else to me. It's best to regard the states as representing a tri-state value where one of the states is not used for some controls.
In addition, I read here that if a textbox is marked Enabled="false", its value isn't preserved in the Viewstate across postbacks.
Don't forget, a screen reader for the visually impaired cannot read the text in a disabled text box, since it cannot receive focus. For ADA compliance, use ReadOnly.

How to catch user changes in databound controls?

I have application full of various controls databound to my classes. I would like to ask user "You are closing application and you made some changes. Do you want to save your changes?". For this I need to recognize that user made any changes.
How to catch user made changes in databound controls? Is textBoxXXX_TextChanged the only way to do this?
Thanks in advance for all your answers.
It depends on the datasource; for example DataTable and DataSet sources contain the GetChanges() methods which allow you to easily see if rows have been added/removed/modified. Other data sources will have their own implementations, if any. If there is no implementation then it's up to you to determine how to check for those changes.
In any event this is something you should do at the data-level, not the UI (by watching for "changed" events). Watching events doesn't scale beyond a couple controls and maintenance can be iffy.
Update: Not sure why I didn't think of it, but a second option is to add a BindingSource to your UI object and use it as a databinding proxy (your UI controls databind to the BindingSource and the BindingSource binds to the real datasource). It provides a better approach than handling all your individual "Control_Changed" events, and requiring rework of your other layers (esp. if they aren't custom data-types).
You need to provide custom logic for that, there's not really an automatic way of doing this. As I see it there are several options:
At the start of the editing, save a copy of the original data object, and when you need to check, compare the current object with the saved one. The comparison can be custom (field by field) or semi-automatic by use of serialization (compare the serialized forms) - if it is serializable.
In each of your data object's property set accessors, test for a change in value and mark the object as 'dirty'.
As been discussed, there are many ways to do this depending on how granular you want to get.
A relatively easy way using client side javascript would be to do something like the following:
Hook into the onchange events of the form elements. You could do this dynamically on page load using javascript/DOM.
When the onchange error handler is called, you could set a page level variable: pageHasChanged = true;
Hook into the page's beforeonunload event (occurs when the user tries to navigate away from the page) and check the pageHasChanged variable to see if any changes were made. If changes were made you could alert the user.
This doesn't give you the detail of what changed, but would be fairly easy to modify to track which form elements changed.

Categories