Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using Visual Basic in Visual Studio 2010. How do I add a textbox to a form from within the code?
Posted

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
 
Share this answer
 
Comments
VJ Reddy 29-Apr-12 19:49pm    
Good answer. parentControl.Controls.Add is vital. 5!
Sergey Alexandrovich Kryukov 29-Apr-12 20:35pm    
Thank you, VJ.
--SA
Fred Andres 29-Apr-12 20:29pm    
Thanks SA. I figured it was something to do with connecting it to the form.
Sergey Alexandrovich Kryukov 29-Apr-12 20:36pm    
Sure. You are very welcome.
Good luck, call again.
--SA
VB
Dim TextBox1 As TextBox = New TextBox

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


More at MSDN[^] site.
 
Share this answer
 
Comments
Fred Andres 29-Apr-12 18:20pm    
Thanks so much.
Maciej Los 29-Apr-12 18:22pm    
Welcome ;)
Fred Andres 29-Apr-12 18:49pm    
I'm making progress, but I'm still not seeing my new textbox. My code is below.
Maciej Los 30-Apr-12 8:16am    
Did you read carefully article on msdn site? In my example i have show you how to set properites. I can't read in your mind... In my opinion the second rate of my solution (1) is not fairly, especcially when i haven't time to answer your question (in comment).
Fred Andres 30-Apr-12 10:25am    
Sorry, I missed the link on first reading. I just read it and it was very helpful. Thanks again.
VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900