简体   繁体   English

有没有办法从头开始在 Visual Studio 2017 C++/CLI 中创建一组按钮?

[英]Is there any way to create an array of buttons in Visual Studio 2017 C++/CLI from scratch?

I´m making a project in which I´m connecting two different devices via UDP and I need to create an array of buttons from scratch to represent bits, so if a recieve for example the number 0x8A (1000 1010b) my array of 8 buttons will display 1´s and 0´s depending on the number.我正在制作一个项目,其中我通过 UDP 连接两个不同的设备,我需要从头开始创建一组按钮来表示位,所以如果收到例如数字 0x8A (1000 1010b) 我的 8 个按钮阵列将根据数字显示 1 和 0。

I´ve been reading but most of the tutorials about c++ and GUI uses drag and drop for the elements, but I need the array of buttons for an easy access to each button.我一直在阅读,但大多数关于 c++ 和 GUI 的教程使用拖放元素,但我需要按钮数组以便轻松访问每个按钮。

I´m using Visual Studio 2017, the C++/CLI project.我正在使用 Visual Studio 2017,C++/CLI 项目。

Thanks for your time.谢谢你的时间。

I'm assuming that "from scratch" means "dynamically at runtime".我假设“从头开始”意味着“在运行时动态地”。

Based on your example of receiving the number 0x8A, it sounds like you only need 8 buttons.根据您接收数字 0x8A 的示例,听起来您只需要 8 个按钮。 If that's the case, then I'd create the buttons at design time, not at runtime.如果是这样,那么我会在设计时创建按钮,而不是在运行时。 Creation at runtime is most appropriate when you don't know ahead of time how many there will be, but if you know it will be exactly 8, then creation at design time is most appropriate.当您事先不知道会有多少个时,在运行时创建是最合适的,但如果您知道正好是 8 个,那么在设计时创建是最合适的。

If my inference is incorrect, and you do not know how many buttons until runtime, then we'd need to know which GUI toolkit you're using to give a concrete answer.如果我的推断不正确,并且您直到运行时才知道有多少按钮,那么我们需要知道您使用的是哪个 GUI 工具包来给出具体答案。 Other than that, all we can do is give a general outline.除此之外,我们所能做的就是给出一个大纲。

// Keep this around somewhere so you can access the buttons later.
List<ButtonOfSomeSort^>^ buttons = gcnew List<ButtonOfSomeSort^>();

for (int i = 0; i < numButtonsToCreate; i++)
{
    ButtonOfSomeSort button = gcnew ButtonOfSomeSort();
    // set some properties on the button, such as text, size & position, and click handler.
    someContainerControlInYourGUI.Add(button);
    buttons.Add(button);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM