Results 1 to 16 of 16

Thread: How to dynamically create TextBox in VB.NET

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    How to dynamically create TextBox in VB.NET

    How to dynamically create text box with format in run time???
    I wanted to create a text box like in Microsoft Office whereby user click on the insert text box button and then click again on the specific area they want the text box position to be...

    I found several coding for this but the result I get is only the normal text box and not like rich text box...
    Finally, is it possible to create picture box dynamically???

    Please pardon my ignorance, I really have no experience at all in VB.NET
    Last edited by tearsculprit; Feb 7th, 2010 at 10:51 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to dynamically create TextBox in VB.NET

    All controls are created with code. When you use the WinForms designer, it simply generates the code for you. You can see that designer-generated code by clicking the Show All Files button in the Solution Explorer and then expanding the node for your form. The easiest way to learn how to work with controls in code is to set one up in the designer and then look at the code it generates.

    Apart from that I can't really say because I'm not sure what you're asking for. If you're saying that you want a RichTextBox then that's exactly what you should create, not a TextBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to dynamically create TextBox in VB.NET

    try this:

    vb Code:
    1. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    2.     'this adds a new 100*100 pixel RichTextBox at the position clicked
    3.     Dim rtb As New RichTextBox
    4.     rtb.Bounds = New Rectangle(e.Location, New Size(100, 100))
    5.     rtb.Text = "this is a dynamic richtextbox"
    6.     Me.Controls.Add(rtb)
    7. End Sub

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to dynamically create TextBox in VB.NET

    a dynamic picturebox is created in the same way

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    2.     'this adds a new 100*100 pixel RichTextBox at the position clicked
    3.     Dim rtb As New RichTextBox
    4.     rtb.Bounds = New Rectangle(e.Location, New Size(100, 100))
    5.     rtb.Text = "this is a dynamic richtextbox"
    6.     Me.Controls.Add(rtb)
    7. End Sub
    Thanks for replying...

    I have run the coding...
    But the text box appears directly on specific area when i press the button "insert text box"

    How can i make it to appear at the location define by the user???
    How to resize the text box and relocate its position?
    and is it possible to make the text box's border to dashed or dotted line?
    Last edited by tearsculprit; Feb 7th, 2010 at 11:52 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by jmcilhinney View Post
    Apart from that I can't really say because I'm not sure what you're asking for. If you're saying that you want a RichTextBox then that's exactly what you should create, not a TextBox.
    I was looking for coding to dynamically create text box on user define locations.
    The text box I mentioned is actually a rich text box which can have format text (Bold, Italic, Underline, Color text)

    also it is re-sizable and the text box is able to relocate(drag and drop to another location)

    I really have no idea how to do all this things

    BTW thanks for replying my post
    Last edited by tearsculprit; Feb 7th, 2010 at 11:45 PM.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by tearsculprit View Post
    also it is re-sizable and the text box is able to relocate(drag and drop to another location)
    have a look at the movable / resizable controls link in my signature

    put this in your form, between public class form1, + end class:

    vb Code:
    1. Private allowNewTextbox As Boolean = False
    2.  
    3. Private Sub btnInsertTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertTextbox.Click
    4.     allowNewTextbox = True
    5.     Me.Cursor = Cursors.Cross
    6. End Sub
    7.  
    8. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    9.     If allowNewTextbox Then
    10.         'this adds a new 100*100 pixel RichTextBox at the position clicked
    11.         Dim rtb As New RichTextBox
    12.         rtb.Bounds = New Rectangle(e.Location, New Size(100, 100))
    13.         rtb.Text = "this is a dynamic richtextbox"
    14.         Me.Controls.Add(rtb)
    15.         allowNewTextbox = False
    16.         Me.Cursor = Cursors.Default
    17.     End If
    18. End Sub

    to create a new textbox, click your button, the cursor will change to a cross, then when you click the form it'll put a new textbox there

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by .paul. View Post
    have a look at the movable / resizable controls link in my signature

    to create a new textbox, click your button, the cursor will change to a cross, then when you click the form it'll put a new textbox there
    Its working~~!!! Thanks
    Will look into the thread...If I have any problem, pls help me too

    Thank you~~

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by tearsculprit View Post
    Its working~~!!! Thanks
    Will look into the thread...If I have any problem, pls help me too

    Thank you~~
    Is it possible to give a name to the text box I dynamically created on run time???
    Cause I need to link it with other functions...Like move/resize the specific textbox...???

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by tearsculprit View Post
    Is it possible to give a name to the text box I dynamically created on run time???

    Hmmmm,
    Code:
                Dim rtb As New RichTextBox
                rtb.Name = "rtbWhateverName"
                ......
                ......

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by tearsculprit View Post
    Is it possible to give a name to the text box I dynamically created on run time???
    Cause I need to link it with other functions...Like move/resize the specific textbox...???
    It's possible, as has been shown, but there's no need or reason to do so. You wouldn't identify the controls by name. What you would do is write a single event handler for the same event of every control. When you create the control you use AddHandler to attach the event handler. In the event handler, you use the 'sender' parameter to access the object that raised the event.

    The way to go about this is to start by NOT creating the controls at run time. Just start with a blank form and add a single control in the designer. You then add the appropriate event handlers and all the code to make that control behave the way you want. In all the event handlers, make sure you access the control via the 'sender' parameter rather than by its member variable. Once that's all done, you simply remove all the Handles clauses and copy the event handlers into your real project. You then use AddHandler to connect your dynamic controls to your event handlers and it will all just work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by jmcilhinney View Post
    It's possible, as has been shown, but there's no need or reason to do so. You wouldn't identify the controls by name. What you would do is write a single event handler for the same event of every control. When you create the control you use AddHandler to attach the event handler. In the event handler, you use the 'sender' parameter to access the object that raised the event.

    The way to go about this is to start by NOT creating the controls at run time. Just start with a blank form and add a single control in the designer. You then add the appropriate event handlers and all the code to make that control behave the way you want. In all the event handlers, make sure you access the control via the 'sender' parameter rather than by its member variable. Once that's all done, you simply remove all the Handles clauses and copy the event handlers into your real project. You then use AddHandler to connect your dynamic controls to your event handlers and it will all just work.

    Can you show me some example...@@ ???

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to dynamically create TextBox in VB.NET

    How about you try to follow the instructions I provided and then ask a specific question when you hit a snag?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by jmcilhinney View Post
    How about you try to follow the instructions I provided and then ask a specific question when you hit a snag?
    ok...Ill try...
    but I do not know what is an event handler???

    Sorry...Im such a newbie...

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by tearsculprit View Post
    ok...Ill try...
    but I do not know what is an event handler???

    Sorry...Im such a newbie...
    When you double click on the form and it takes you to the code window and puts the cursor inside a method that it generated for you, that's an event handler. It's a method that handles the Load event of the form. Likewise, when you double click on a Button it creates a Click event handler for the Button, or you can create a TextChanged event handler for a TextBox. There are various other ways to create event handlers too.

    I have to say, if you don't know what an event handler is then your grasp of the fundamentals is not good. That's no crime but I can't help feeling that you'd be better served working your way through a beginners tutorial to get a good grasp of the basics before you try to create, move and resize controls dynamically. That depends very heavily on handling events if you don't even know basic terminology then that makes it difficult.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: How to dynamically create TextBox in VB.NET

    Quote Originally Posted by jmcilhinney View Post
    When you double click on the form and it takes you to the code window and puts the cursor inside a method that it generated for you, that's an event handler. It's a method that handles the Load event of the form. Likewise, when you double click on a Button it creates a Click event handler for the Button, or you can create a TextChanged event handler for a TextBox. There are various other ways to create event handlers too.

    I have to say, if you don't know what an event handler is then your grasp of the fundamentals is not good. That's no crime but I can't help feeling that you'd be better served working your way through a beginners tutorial to get a good grasp of the basics before you try to create, move and resize controls dynamically. That depends very heavily on handling events if you don't even know basic terminology then that makes it difficult.
    Okiez... Thank you for replying

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width