Center text in a dynamically created textbox in VB.NET web application

I want to center the text within a text box, but I'm having problems in finding how to do this.



Thanks,
a1785
a1785Asked:
Who is Participating?
 
bigjim2000Connect With a Mentor Commented:
Here's what you do:

First, make sure that in your HTML code, you have a form tag similar to the following:
<form id="aspxform" runat="server"></form>

Then, make sure you have the following variable declared in your page:
protected System.Web.UI.HtmlControls.HtmlForm aspxform;

Then, when you dynamically create your textbox, the code should look similar to the following:
System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();
tb.Style.Add("text-align", "center");
aspxform.Controls.Add(tb);

The problem you were encountering before was the TextBox.Style collection is read-only.  You can only add items to the collection via the Add() method.

Hope this helped!

-Eric
0
 
adw060398Commented:
It should be a matter of setting the alignment property after you have created the textbox and before you add the text (ie txtTextBox.alignment=<value> where value is 0 for left, 1 for Right and 2 for Centre).  I f you do this after adding the text, you have to call the refresh method (txttextbox.refresh).

This should sort the problem, but I am doing this from memory.  So if it doesnot  send me a sample of your code.

Alan Williams
0
 
a1785Author Commented:
I do not see that property at design time.

Dim txtbxNewText As New TextBox
                            txtbxNewText.Width = System.Web.UI.WebControls.Unit.Percentage(100)
                            txtbxNewText.Font.Name = "Microsoft Sans Serif"
                            txtbxNewText.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall
                            txtbxNewText.BackColor = System.Drawing.Color.Lavender
                            txtbxNewText.Height = System.Web.UI.WebControls.Unit.Pixel(18)
                            tmpCell.Controls.Add(txtbxNewText)
                            AddHandler txtbxNewText.TextChanged, AddressOf txtbxNewText_EventHandler


0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
adw060398Commented:
I looked it up and the property is TextAlign (it is align in VB6)
0
 
a1785Author Commented:
in asp.net there is no textalign property
0
 
Jerry_PangCommented:
Text alignment in Textbox on ASP page
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21066139.html

you have to add styles to the textbox

in vb.net its textbox.textAlign
0
 
a1785Author Commented:
I'm unfamiliar at adding styles at runtime.
0
 
Jerry_PangCommented:
Try this, add this to your declaration..

txtbxNewText.style = "text-align:center"
0
 
a1785Author Commented:
When I add txtbxNewText.style = "text-align:center" to my code it states that the property of txtbxNewText.style is read only.




0
 
Jerry_PangCommented:
hmm.. the other way is using CSS.
edit the asp page.
create a class on the html
insert this before the header.
<style type="css/style">
.CenterAlign {text-align : center;}
</style>

then
tbxNewText.class = "CenterAlign"

I dont have vb.net here so i cant experiment this now, i'll post back when i get back from my office on monday.

Im also studying ASP.net but using C#.

anyways, if there are any experts here who could. we would appreciate it.
i'll post back maybe tomorrow or the day after.

0
 
bigjim2000Commented:
Sorry, you wanted VB.Net ;-)

The HTML is the same, the form declaration would look like
Dim aspxform as System.Web.UI.HtmlControls.HtmlForm

And the code would be:
Dim tb as System.Web.UI.WebControls.TextBox()
tb.Style.Add("text-align", "center")
aspxform.Controls.Add(tb)

Hope THIS helped ;-)

-Eric
0
 
bigjim2000Commented:
Gah, I'm just messing this all up... you need:
Protected aspxform as System.Web.UI.HtmlControls.HtmlForm, not Dim aspxform as System.Web.UI.HtmlControls.HtmlForm.... sorry...

-Eric
0
 
bigjim2000Commented:
I am sorry, I think I have been more hurt than help... as I was not commenting from a computer with visual studio on it.  I don't regularly use VB, and I feel I have made yet another small error.  You need to use the New keyword when initializing tb.

The code would be as such:
Dim tb As New System.Web.UI.WebControls.TextBox()
tb.Style.Add("text-align", "center")
aspxform.Controls.Add(tb)

Sorry again,

-Eric
0
 
a1785Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.