c ++如何向组框中添加标签并设置其位置

时间:2018-09-13 10:30:17

标签: forms visual-studio controls position c++-cli

大家好,我希望用户选择文件列表(txt文件),我想在组框中显示这些文件的路径,我可以在c#中完美地做到这一点,但是c ++ \ cli出现了一些问题 我在Visual Studio 2017社区工作

这是我的代码:

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);
}

使用此代码,标签被添加到了分组框,但我无法在其中正确设置其位置

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

System.Drawing.Point是值类型(在C#中为struct,在C ++ / CLI中为value class / value struct),因此属性Location返回的是< em>复制其当前位置。为了更新它,您需要立即设置整个值类型。

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