AJITH
AJITH

Reputation: 1175

Getting the Label text without Hotkey('&') in C#

In my C# Winforms application, I have few labels control with hotkey set.(eg: &Name). We are using JAWS screen reader. The Label.Text always gives "&Name". Is there any way to get the label text without hotkeys('&')? I cant replace the '&' with String.empty because there are few labels with actual '&' required in it.

Upvotes: 1

Views: 355

Answers (3)

RJV
RJV

Reputation: 55

Easy way is to use AccessibleObject.Name property.

Ex : String text = label1.AccessibleObject.Name;

Upvotes: 3

TnTinMn
TnTinMn

Reputation: 11801

As I mentioned in my earlier comment, this is an accessibility issue. The following custom Label exposes a new string property AccessibleValue that can be set to the text that the AccessibleObject will return as its value.

I do not know whether or not this works with the JAWS screen reader, but it did show the custom value when using the Inspect tool.

public class AccessibleLabel : Label
    {
    protected override AccessibleObject CreateAccessibilityInstance()
        {
        return new LabelAccessibleObject(this);
        }

    public string AccessibleValue { get; set; }

    private class LabelAccessibleObject : ControlAccessibleObject
        {
        private AccessibleLabel myowner;
        public LabelAccessibleObject(AccessibleLabel owner)
            : base(owner)
            {
            this.myowner = owner;
            }

        public override AccessibleRole Role
            {
            get
                {
                AccessibleRole accessibleRole = base.Owner.AccessibleRole;
                if (accessibleRole != AccessibleRole.Default)
                    {
                    return accessibleRole;
                    }
                return AccessibleRole.StaticText;
                }
            }

        public override string Value
            {
            get
                {
                if (string.IsNullOrEmpty(myowner.AccessibleValue))
                    {
                    return base.Value;
                    }
                else
                    {
                    return myowner.AccessibleValue;
                    }
                }
            set
                {
                base.Value = value;
                }
            }

        }

    }

Upvotes: 0

Ben
Ben

Reputation: 514

Can you give an example where '&' is required?

TrimStart will remove characters form the beginning of a string https://msdn.microsoft.com/en-us/library/system.string.trimstart(v=vs.110).aspx

Upvotes: 0

Related Questions