1

I'm currently learning how to make windows programs in C++ with CLR in Visual Studio 2022. I'm very new to C++ (Started 2 days ago and I barely know what a struct is), and I just can't seem to find out how I can get the name of an object (A label) on my form as a string variable to log to the developer's console.

Here's my current code:

    private:
        System::Void Btn1_Click(System::Object^ sender, System::EventArgs^ e)
        {
            string label1name = label1->AccessibleName->ToString();
            string label1text = label1->Text->ToString();

            cout << "[INPUT] " << "Was pressed" << endl;
            cout << "[DEBUG] Changed " << label1name << " to " << label1text << endl;
        }

and here's a screenshot of my current form: Windows form screenshot

Coakey
  • 41
  • 3
  • It's just `label1->Name`. Already a string (as is `AccessibleName`). – Jimi Jan 18 '22 at 13:10
  • 2
    That didn't work for me either, it would give me the error `No suitable constructor exists to convert from "System::String ^" to "std::basic_string>"` – Coakey Jan 18 '22 at 13:12
  • It appears you are programming in **C++/CLI**, which is a similar-but-different programming language from **C++**. (Much like how Objective-C++ is a similar-but-different programming language from C++.) You ought to tag the question with `c++-cli` rather than `c++`, which will attract the attention of people experienced with that topic. – Eljay Jan 18 '22 at 13:13
  • `String^ theName = label1->Name;` -- Where do you want your output to appear? – Jimi Jan 18 '22 at 13:17
  • 2
    I want my output to be printed to the console, hence the `cout` expression. Also, the expression you gave, gives the errror `'initializing' cannot convert from System::String ^ to std::basic_string,std::allocator>'` – Coakey Jan 18 '22 at 13:19
  • What Console? You have a Windows Forms app there. – Jimi Jan 18 '22 at 13:20
  • 1
    I start it up with Local Windows Debugger and it logs everything to the console that appears with the form. I don't know why it's such a mystery to you that there's a console when I start it – Coakey Jan 18 '22 at 13:21
  • It's not a Console, it's the Output pane. `using namespace System::Diagnostics; /* other */ Debug::WriteLine(label1->Name);` – Jimi Jan 18 '22 at 13:30
  • That doesn't seem to output anything for me. I've used `cout` in the public void MyForm ` public: MyForm(void) { InitializeComponent(); cout << "[DEBUG] Program started" << endl; }` like so and it worked – Coakey Jan 18 '22 at 13:32
  • Did you create a WIndows Forms application or a Console application that happens to show a Form? – Jimi Jan 18 '22 at 13:38
  • I honestly do not remember – Coakey Jan 18 '22 at 13:52
  • A WinForms app doesn't have a Console, unless you attach one in code, that is. – Jimi Jan 18 '22 at 14:06

0 Answers0