0

我正在使用 VC++ 2008(Windows 窗体应用程序 C++\CLR),我创建了文本框的动态数组(用户定义了他想要创建的文本框数量),并且我想创建一个 KeyPress 事件处理程序,以防止字符(我希望这些文本框只是数字,并且只接受一个点“十进制数字”)。那么如何引用用户正在使用的文本框(例如光标所在的文本框)有什么办法可以做到这一点吗?该函数如下所示:

private: System::Void textBox_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
         if(e->KeyChar == '.')
         {
             if(this->/*the textbox in use*/->Text->Contains(".") && !this->/*the textbox in use*/->SelectedText->Contains("."))
                 e->Handled = true;
         }
         else if(!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)
             e->Handled = true;
     }
4

1 回答 1

0
    private void CreateTextBoxControls()
    {
        intColCount= Convert.ToInt32(txtColumnNo.Text.ToString().Trim());
        int rowCount =0;
        Table tblHead = new Table();
        if (tblHead.GetType().ToString().Equals("System.Web.UI.WebControls.Table") && PHOptions.FindControl("tblHead") == null )
        {
            tblHead.ID  = "tblHead";
            tblHead.EnableViewState = true;
            tblHead.BorderWidth=Unit.Pixel(0);
            tblHead.CellSpacing = 0;
            tblHead.CellPadding = 1;
            tblHead.Width = Unit.Percentage(96);
            TableRow rH     = new TableRow();
            TableCell cH    = new TableCell();
            cH.Text= "Table Heading" ;
            cH.Font.Bold = true;
            rH.Cells.Add(cH);
            tblHead.Rows.Add(rH);
            PHOptions.Controls.Add(tblHead);
            if(intColCount>0)
                rH.Visible =true;
            else
                rH.Visible =false;
        }


        Table tblHelp = new Table();
        if (tblHelp.GetType().ToString().Equals("System.Web.UI.WebControls.Table") && PHOptions.FindControl("tblHelp") == null )
        {
            tblHelp.ID  = "tblHelp";
        }
        tblHelp.EnableViewState = true;
        tblHelp.BorderWidth=Unit.Pixel(1);
        tblHelp.CellSpacing = 0;
        tblHelp.CellPadding = 1;
        tblHelp.BorderWidth = Unit.Pixel(1);
        tblHelp.Width = Unit.Percentage(96);
        for (int rowIndex=0; rowIndex<=rowCount; rowIndex++) 
        {
            TableRow r      = new TableRow();
            TableRow rWeight= new TableRow();
            //r.ID          = "rLabel";
            TableRow rID    = new TableRow();


                for (int clIndex=0; clIndex<intColCount; clIndex++) 
                {
                    TableCell c = new TableCell();
                    txtBox = new TextBox();
                    if (txtBox.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && PHOptions.FindControl("txtOption"+(clIndex+1).ToString()) == null )
                    {
                        txtBox.ID ="txtOption"+(clIndex+1).ToString();
                        //txtBox.Text = (clIndex+1).ToString();
                        txtBox.Width= Unit.Pixel(45);
                        txtBox.MaxLength = 2;
                        c.BorderWidth=Unit.Pixel(1);
                        c.Width=Unit.Pixel(80);
                        c.Controls.Add(txtBox);
                        r.Cells.Add(c);     
                        txtBox.PreRender += new System.EventHandler(this.txtBox_PreRender); 
                    }
                }

            tblHelp.Rows.Add(r);
        }
        TableRow rSubmit        = new TableRow();
        TableCell cSubmit = new TableCell();
        cSubmit.ColumnSpan = intColCount ;
        btnSubmitButton = new Button();
        btnSubmitButton.ID  ="btnSubmit";
        btnSubmitButton.Text= "Submit";

        if( PHOptions.FindControl("btnSubmit") == null )
            cSubmit.Controls.Add(btnSubmitButton);
        cSubmit.Attributes.Add("Align","Center");
        rSubmit.Cells.Add(cSubmit); 
        tblHelp.Rows.Add(rSubmit);
        PHOptions.Controls.Add(tblHelp);
        this.btnSubmitButton.PreRender += new System.EventHandler(this.btnSubmitButton_PreRender);
        this.btnSubmitButton.Click += new System.EventHandler(this.btnSubmitButton_Click);
    }


    private void btnSubmitButton_Click(object sender, System.EventArgs e)
    {
        for (int clIndex=0; clIndex<intColCount; clIndex++) 
        {
            string boxName = "txtOption" + (clIndex+1).ToString();      
            TextBox tb = PHOptions.FindControl(boxName) as TextBox;
            if( lblDisplay.Text != "" )
                lblDisplay.Text+=","+tb.Text ;
            else
                lblDisplay.Text=tb.Text ;
        }
    }

只需参考这段代码......(这包含动态创建文本框和从文本框读取)

于 2012-04-26T12:50:47.090 回答