Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:55:15+00:00 2026-05-16T20:55:15+00:00

I have a caption for a GUI control, and I want to convert it

  • 0

I have a caption for a GUI control, and I want to convert it to a simple text string. Specifically, I want to remove the accelerator metacharacters.

For example (examples assume WinForms):

  • Strip off single occurrences of the metacharacter: &Yes becomes Yes
  • Convert double occurrences to single: Income && Expense becomes Income & Expense

My code will know whether it’s dealing with the Windows Forms syntax (where the accelerator metacharacter is &) or WPF (where it’s _). However, this is in back-end code, so I don’t want to take a dependency on any WinForms or WPF library functions — I need to do this using the core (non-GUI) BCL assemblies. (And at that point, I figure that anything that works for WinForms would be trivial to modify for WPF.)

I can think of several ways to do this, but it’s not clear which is simplest.

What’s the simplest way to implement this “remove metacharacters if single, de-double if doubled”?

Update: I had assumed that WinForms and WPF both handled these basically the same, but it turns out they don’t. WinForms will strip a lone metacharacter at the end of the string (Foo& becomes Foo), but WPF will not (Foo_ remains Foo_). Bonus points for an answer that addresses both.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:55:15+00:00Added an answer on May 16, 2026 at 8:55 pm

    I’ve edited (removed) my previous answer. I think the simplest way would be this regular expression:

    string input = "s&trings && stuf&f &";
    input = Regex.Replace(input, "&(.)", "$1");
    

    That correctly handles repeated ampersands as well as the case where the ampersand is the last character.

    EDIT, based on additional provided information:

    So the WinForms expression would be "&(.?)", and the WPF expression would be "_(.)". You ask for a solution that addresses both cases, but I’m not sure what you’re asking. Your original question said that the code knows whether it’s processing WPF format or WinForms format. So I would envision a method:

    string StripAccelerators(string s, bool isWinForms)
    {
        string pat = (isWinForms) ? "&(.?)" : "_(.)";
        return Regex.Replace(s, pat, "$1");
    }
    

    And, yes, I realize that using a Boolean flag in the interface is less than ideal. Ideally, you’d use an enumerated type, or perhaps have two separate methods.

    I don’t think you want to have a single regular expression that will perform both. It’s possible, but then you’ll end up removing underlines from WinForms strings, or ampersands from WPF strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following methods: ShowMessage (string text, string caption) {...} ShowMessage (string formatText,
I have a structured file with hierarchical text which describes a GUI in Delphi
I have a form without caption, using on double click to maximize : Code
I have a table with rowID, longitude, latitude, businessName, url, caption. This might look
I have a third-party GUI program that I'm wrapping with a Python class (using
In our application we sometimes have to make minor GUI modifications for different customers:
I have a custom table-environment defined with \newenvironment. I have a caption in this
I've got the WP Slimbox2 plugin installed and I'm trying to have the caption's
I have several child windows, all with the same parent. I want to cascade/tile
i have a Rad Upload control to upload more than one image ,,the problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.