正在开发Visual Studio通用项目-如何从for循环中顺序命名的TextBoxes中提取文本

问题描述 投票:0回答:2

我对以下代码有问题,我认为这将从表单上的顺序TextBoxes中提取用户输入:

#include "pch.h"
#include "MainPage.xaml.h"
#include <iostream>  // for std::cout and std::cin
#include <sstream>
#include <string>
{
    int grid[9][9] = { 0 }; // virtual array filled with zeros
        //put numbers in array
    for (int row = 0; row < 9; ++row)//step through all rows
        for (int col = 0; col < 9; ++col) //step through all columns
        {
            row = row + 1; // text box names suffixes are 11 to 19
            col - col + 1;
                std::string r_str = std::to_string(row);// turn row number into text
            std::string c_str = std::to_string(col);//turn column nuber into text
            std::string texnum = "Tex" + r_str + c_str;// eg "Tex11" //compile textbox name
            String^ str_input = texnum->Text; //get Platform::String from textBox
            std::wstring wsstr(str_input->Data());//Convert Platform::String to String
            int n = std::stoi(wsstr);//Convert String to Integer
            grid[row][col] = n; //put text from texbox in array as a number
        }
}

在线字符串^ str_input = texnum->文本;

在texnum的TexBox标识符处,它给出一个错误,表示Expression必须具有指针或句柄类型。如果我将其替换为实际的文本框名称Tex11,则不会出现“错误”,但它只会从一个框中提取文本。我需要一种使用字符串变量而不是实际字符串从TextBox中获取文本的方法。任何帮助,将不胜感激。

visual-c++ win-universal-app
2个回答
0
投票

您不能直接使用字符串来尝试获取Text属性,在这种情况下,它只是一个字符串而不是TextBox对象。因此,您需要首先通过获取的名称字符串获取TextBox控件,然后再通过TextBox获取Text属性。

您可以尝试以下代码来使用FindName方法获取TextBox对象,“ MyPage”是TextBox的父视图(例如StackPanel)。另外,需要将FindName方法传递给PlatForm :: String ^类型,将std :: wstring转换为PlatForm :: String ^很容易,因此最好只使用std :: wstring类型而不是std :: string 。

int grid[9][9] = { 0 }; // virtual array filled with zeros
//put numbers in array
for (int row = 0; row < 9; ++row)//step through all rows
    for (int col = 0; col < 9; ++col) //step through all columns
    {
        row = row + 1; // text box names suffixes are 11 to 19
        col - col + 1;

        std::wstring r_str = std::to_wstring(row);// turn row number into text
        std::wstring c_str = std::to_wstring(col);//turn column nuber into text
        std::wstring texnum = L"texnum" + r_str + c_str;
        Platform::String^ aa = ref new Platform::String(texnum.c_str());
        TextBox^ elment = (TextBox ^)MyPage->FindName(ref new Platform::String(texnum.c_str()));
        String^ str_input = elment->Text;
        std::wstring wsstr(str_input->Data());
        int n = std::stoi(wsstr);
    }

0
投票

这是我的最终代码(StackPanel命名为FirstLine)

void universal::MainPage::Solve_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
int grid[9][9] = { 0 }; // virtual array filled with zeros
//put numbers in array
for (int row = 0; row < 9; ++row)//step through all rows
    for (int col = 0; col < 9; ++col) //step through all columns
    {
        row = row + 1; // text box names suffixes are 11 to 19
        col = col + 1;

        std::wstring r_str = std::to_wstring(row);// turn row number into text
        std::wstring c_str = std::to_wstring(col);//turn column nuber into text
        std::wstring texnum = L"texnum" + r_str + c_str;
        Platform::String^ aa = ref new Platform::String(texnum.c_str());
        TextBox^ elment = (TextBox^)FirstLine->FindName(aa);
        String^ str_input = elment->Text;
        std::wstring wsstr(str_input->Data());
        int n = std::stoi(wsstr);

    }
© www.soinside.com 2019 - 2024. All rights reserved.