Button custom attributes for windows forms - winforms

I need to add a secondary Id or even some custom text to controls like Button, Textbox, ListBox etc., so that i can later use it for programmatic purposes and should not be displayed to user.
I can do this in ASP.net using Attributes property for almost any control, but when I checked with windows forms I found it doesn’t have this property, can I find any other alternatives in windows forms?

You can use Control.Tag property for this.
myButton.Tag = whatever;
myTextBox.Tag = whatever;
...

Use the Control.Tag property. Source: MSDN
"A common use for the Tag property is to store data that is closely associated with the control."
This sounds like what you're after.

Related

How to get the old value of a TrackBar without a custom control or separate field?

I am trying to get the old Value of a TrackBar when the Scroll event gets raised. I could do this by creating a separate field and storing that value whenever it changes, or overriding the event in a custom control however, I am already using the built-in TrackBar and I'd prefer to not have to redesign my forms.
The Microsoft documentation does not seem to have any information on this and the EventArgs parameter on the OnScroll and OnValueChanged methods seem pretty generic.
Is there a way to achieve this and how?
Sources:
Microsoft Documentation: TrackBar.OnScroll Method
Microsoft Documentation: TrackBar.Scroll Event
How to override method and event in WinForm UserControl in C#?
Microsoft Documentation: TrackBar.ValueChanged Event
Microsoft Documentation: TrackBar.Value Property
Microsoft Documentation: TrackBar.OnValueChanged Method
I believe there isn't a built-in way to achieve what you're looking for.
To address your concern about having to replace all the TrackBar controls on a form with the derived custom control, here's how I usually do it:
Create your control class which inherits the TrackBar control.
Open your FormName.Designer.cs or FormName.Designer.vb file.
Click Ctrl+H to open the "Find and replace" window.
Enter System.Windows.Forms.TrackBar in the "Find" field, and YourNameSpace.CustomTrackBar in the "Replace with" field.
Click "Replace all" (You can do it one by one if you're afraid to mess something up).
Rebuild your project.
Hope that helps.

Changing the default setting of a WinForms control property

I seek to change the default setting of a .NET Windows Forms control's property, without creating a custom control inheriting from it.
What I want is, for example, to add a TextBox on my Form that already has the TextAlign property set to HorizontalAlignment.Right instead of HorizontalAlignment.Left. Even if only solution-wide, if achievable, I would love to know. This would save a lot of time for when one is working with a LOT of controls and needs to set their properties to specific, non-default, values.
Creating a custom control is just too overkill for this, and would clutter my solution with unnecessary things. I have also considered running a regex on the designer files solution-wide (to add the non-default values to such controls), but writing regex like that can (also) be time consuming/problematic.
Any ideas?
There is a way to do that. But you need to set the binding initially. Later on you can set in one go to all the controls of your choice. This might be not your exact answer but this is how we define styles like as in WPF
(1) Click the Application Property Bindings
(2) Define Binding
(3)Now for textbox you can set the initial default
(4) Now You can set the property binding of every textbox intially or you can code which will set the application settings at starup or on form load.
This will you help to change the default propety at once later on

Make Custom WinForms Control Work with Narrator (Accessibility)

I have a custom list control derived from Control class.
I need to make it accessible to people with disabilities through MSAA (Microsoft Active Accessibility).
So far I understand that I need to create class that inherits from ControlAccessibleObject and then return its instance in Control.CreateAccessibilityInstance method override.
The problem is that I have implemented this and it seems not work with Windows Narrator tool.
For example, when I click on an item in standard ListView, the Narrator speaks out the selected item text.
But when I click on item in my control, nothing happens (although the item text is requested in my ControlAccessibleObject implementation)
I thought I need to implement IAccessible as well, but I looked on .NET refrence source code and the ListView does not implement this interface. I thought maybe this is implemented in the wrapped Win32 control, so I took a look on similar control - DataGridView - but this does not implement IAccessible as well.
DataGridView have accessibility support, but although I copied all the important code of DataGridViewAccessibleObject, it still does not work in my control.
Do anyone have more experience with custom control accessibility in WinForms?
Okay, I found it: The Control.AccessibilityNotifyClients method does the magic. One have to override this method in a derived control.
However, to make screen readers speak the text, I had to call:
AccessibilityNotifyClients(AccessibleEvents.Focus, index);
AccessibilityNotifyClients(AccessibleEvents.Selection, index);
Here the index is an index of newly selected item.
I found this code in the .NET reference source of CheckedListBox. When I used Focus or Selection event solely, the screen reader have not reacted. The spoken text also depend on the AccessibleObject state that corresponds to a newly selected item.

Can I create equivalent of C# attribute or metadata for WPF child controls in XAML code?

Can I add some kind of custom attributes in XAML ?
Perhaps you mean Attached properties (scroll down for info on how to create a custom one)? For examle "DockPanel.Dock" is an Attached property.
Or maybe Markup extensions? (though that is something a little bit different)
You can also place Attributes on custom controls and read them in code behind - just like any other .NET class, but that is not possible to do from XAML (since those are set per-type/method/..., not per-instance).
You can, using Attatched Properties. This lets you add your own and data to the xaml on any control you choose.

Show/hide controls based on user role in WPF

I need to do a quick sample WPF application where the controls on the forms should be made visible or hidden based on the user roles.
Something like this will be great,
How to manipulate WPF GUI based on user roles
I am not sure where to put the XAML defined in the thread(<Control ) so that the every control in the form uses RoleToVisibilityConverter to show or hide the controls.
i am very new to windows dev..could you please help me ?
Regards
Bala
You could solve this by binding the Visibility to a corresponding property in your code-behind/ViewModel.
This is an example from a binding in one of my testcontrols using WPF (in combination with Caliburn):
Visibility="{Binding Path=IsAdmin}"
Here I have a bool property in my ViewModel called IsAdmin.
For me, the easiest way was creating a global variable in my app. For that, go to your Project->Properties->Settings.settings and create a new variable (called, for example, Administrator), and set it to True of False depending on what you want to do. Something like this:
Then, at your MainWindow, when its initialized, you can write this:
if (!Properties.Settings.Default.Administrator)
{
DisableSettings();
}
And then, disable the fields you want to.

Resources