Asked  7 Months ago    Answers:  5   Viewed   54 times

I have a ToolStripMenuItem called myMenu. How can I access this like so:

/* Normally, I would do: */
this.myMenu... etc.

/* But how do I access it like this: */
String name = myMenu;

This is because I am dynamically generating ToolStripMenuItems from an XML file and need to reference MenuItems by their dynamically generated names.



Use the Control.ControlCollection.Find method.

Try this:

Tuesday, June 1, 2021
answered 7 Months ago

You could use reflection. For example if PropertyName is a public property on MyClass and you have an instance of this class you could:

MyClass myClassInstance = ...
double temp = (double)typeof(MyClass).GetProperty("PropertyName").GetValue(myClassInstance, null);

If it's a public field:

MyClass myClassInstance = ...
double temp = (double)typeof(MyClass).GetField("FieldName").GetValue(myClassInstance);

Of course you should be aware that reflection doesn't come free of cost. There could be a performance penalty compared to direct property/field access.

Monday, June 14, 2021
answered 6 Months ago

string.Format() considers each '{' or '}' to be part of a placeholder (like '{0}' you already use). You need to escape each literal occurrence by doubling it.

So in your case do:

 string tmp = @"
    if (UseImageFiles) {{
Thursday, June 17, 2021
answered 6 Months ago

As it's nearly Christmas, How about this:

#include <Windows.h>
#include <string>

using namespace std;

string GetMimeType(const string &szExtension)
    // return mime type for extension
    HKEY hKey = NULL;
    string szResult = "application/unknown";

    // open registry key
    if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szExtension.c_str(), 
                       0, KEY_READ, &hKey) == ERROR_SUCCESS)
        // define buffer
        char szBuffer[256] = {0};
        DWORD dwBuffSize = sizeof(szBuffer);

        // get content type
        if (RegQueryValueEx(hKey, "Content Type", NULL, NULL, 
                       (LPBYTE)szBuffer, &dwBuffSize) == ERROR_SUCCESS)
            // success
            szResult = szBuffer;

        // close key

    // return result
    return szResult;

int main(int argc, char* argv[])
    string szExt1 = ".pdf";
    string szExt2 = ".avi";

    string szMime1 = GetMimeType(szExt1);
    string szMime2 = GetMimeType(szExt2);

    printf("%s = %sn%s = %sn", szExt1.c_str(), szMime1.c_str(), 
        szExt2.c_str(), szMime2.c_str());

    return 0;

On my system, it gives the following output:

.pdf = application/pdf
.avi = video/avi

Saturday, August 21, 2021
Maxime Porté
answered 4 Months ago

Tell the designer to serialize the object itself:

  public OpenFileDialog DialogOpen {
    get { return m_dlgOpen; }
Tuesday, October 26, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :