Search code examples
formsvisual-studiocontrolspositionc++-cli

c++ How to add dinamically a label to a groupbox and set its postion


Hi guys I want the user to choose a list of files (txt file) and I wanna show the path of those file in a groupbox, I can do this perfectly in c# but I am having some problem with c++\cli p.s I working in visual studio 2017 community

this is my code:

openFileDialog1 = gcnew System::Windows::Forms::OpenFileDialog();
openFileDialog1->InitialDirectory = DefaultProgramPath;
openFileDialog1->FileName = "";
openFileDialog1->Filter = "ESA Files (*.esa)|*.esa|txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 1;
openFileDialog1->RestoreDirectory = true;

if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
    String^ toBeAdded= openFileDialog1->FileName;
    Label^job = gcnew  Label();
    job->Text = toBeAdded;

    job->Location.X = 150;
    job->Location.Y = 250;
    groupBox1->Controls->Add(job);
}

with this code the label is added to the groupbox But I can't set its position properly within it

Thanks for your help


Solution

  • System.Drawing.Point is a value type (struct in C#, value class/value struct in C++/CLI), therefore the property Location is returning a copy of its current location. In order to update it, you need to set the entire value type at once.

    Point newLocation;
    newLocation.X = 150;
    newLocation.Y = 250;
    job->Location = newLocation;