从代码添加文本框 [英] Adding a textbox from code

查看:108
本文介绍了从代码添加文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2010中使用Visual Basic.如何从代码内将文本框添加到表单?

I''m using Visual Basic in Visual Studio 2010. How do I add a textbox to a form from within the code?

推荐答案

如果您不做任何操作来查看控件,您将如何看待它?到目前为止,我看到的答案"也缺少您需要的主要代码.

要查看新创建的控件,您还需要调用parentControl.Controls.Add(newlyCreatedControl);,其中parentControl是成为父项的某个控件,Form或任何其他控件,而newlyCreatedControl是您的文本框或您创建的任何其他控件. br/>
要知道如何解决诸如此类的许多问题,您需要了解一件事.所有控件始终在运行时随代码一起添加";没有别的办法了.设计人员仅帮助您创建此代码.不要被设计时"这个术语所迷惑,它只是意味着控件的代码应该支持与设计者一起工作.

因此,如果您知道如何与设计者进行某些事情,但不确定如何用代码编写,请先与设计者打交道,然后查看自动生成的代码,该代码位于文件中作为子节点的文件中.解决方案资源管理器中的主表单节点.通过这段代码,您将了解如何做到这一点.

-SA
How could you see your control if you don''t do anything to see it? The "answers" I''ve seen so far also lack the main piece of code you need.

To see your newly created control, you also need to call parentControl.Controls.Add(newlyCreatedControl); where parentControl is some control to become a parent, Form or any other, and newlyCreatedControl is your text box or any other control you create.

To know how to solve many problems like this one, you need to understand one thing. All controls are added "withing the code", always in run time; there is no other way. The designer merely helps you to create this code. Don''t be mystified by the term "design-time" — it simply means that the control''s code should support working with the designer.

So, if you know how to make certain things with the designer but not sure how to write it in code, do wit the designer first and then look at the auto-generated code, which is placed in the file shown as a child node of the main form node in the Solution Explorer. From this code, you will understand how to do it.

—SA


Dim TextBox1 As TextBox = New TextBox

With TextBox1
    .Top = 8
    .Left = 16
    'set the other properties
End With



有关更多信息,请参见 MSDN [



More at MSDN[^] site.


Public Class Form1
    Dim lowerBound As Double
    Dim textBoxes(4) As TextBox
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TextBox1.AppendText("sales")
        textBoxes(1) = New TextBox()
        With textBoxes(1)
            .Top = 60
            .Left = 17
            .Height = 20
            .Width = 104
            .Visible = True
            .ForeColor = Color.Black
            .BackColor = Color.Transparent
            .AcceptsReturn = False
        End With
    End Sub


这篇关于从代码添加文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆