How to dynamically create TextBox in VB.NET
How to dynamically create text box with format in run time???:confused:
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 :blush:
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.
Re: How to dynamically create TextBox in VB.NET
try this:
vb Code:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
'this adds a new 100*100 pixel RichTextBox at the position clicked
Dim rtb As New RichTextBox
rtb.Bounds = New Rectangle(e.Location, New Size(100, 100))
rtb.Text = "this is a dynamic richtextbox"
Me.Controls.Add(rtb)
End Sub
Re: How to dynamically create TextBox in VB.NET
a dynamic picturebox is created in the same way
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
.paul.
try this:
vb Code:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
'this adds a new 100*100 pixel RichTextBox at the position clicked
Dim rtb As New RichTextBox
rtb.Bounds = New Rectangle(e.Location, New Size(100, 100))
rtb.Text = "this is a dynamic richtextbox"
Me.Controls.Add(rtb)
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?
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
jmcilhinney
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 :o
BTW thanks for replying my post :)
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
tearsculprit
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:
Private allowNewTextbox As Boolean = False
Private Sub btnInsertTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertTextbox.Click
allowNewTextbox = True
Me.Cursor = Cursors.Cross
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If allowNewTextbox Then
'this adds a new 100*100 pixel RichTextBox at the position clicked
Dim rtb As New RichTextBox
rtb.Bounds = New Rectangle(e.Location, New Size(100, 100))
rtb.Text = "this is a dynamic richtextbox"
Me.Controls.Add(rtb)
allowNewTextbox = False
Me.Cursor = Cursors.Default
End If
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
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
.paul.
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~~
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
tearsculprit
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...???
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
tearsculprit
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"
......
......
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
tearsculprit
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.
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
jmcilhinney
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...@@ ???
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?
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
jmcilhinney
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???:blush:
Sorry...Im such a newbie...:o
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
tearsculprit
ok...Ill try...:)
but I do not know what is an event handler???:blush:
Sorry...Im such a newbie...:o
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.
Re: How to dynamically create TextBox in VB.NET
Quote:
Originally Posted by
jmcilhinney
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 :o