Solved

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

Posted on 2004-08-10
14
445 Views
Last Modified: 2010-05-18
I want to center the text within a text box, but I'm having problems in finding how to do this.



Thanks,
a1785
0
Comment
Question by:a1785
  • 5
  • 4
  • 3
  • +1
14 Comments
 

Expert Comment

by:adw060398
ID: 11768036
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
 

Author Comment

by:a1785
ID: 11768061
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
 

Expert Comment

by:adw060398
ID: 11768125
I looked it up and the property is TextAlign (it is align in VB6)
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:a1785
ID: 11768151
in asp.net there is no textalign property
0
 
LVL 9

Expert Comment

by:Jerry_Pang
ID: 11769811
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
 

Author Comment

by:a1785
ID: 11783562
I'm unfamiliar at adding styles at runtime.
0
 
LVL 9

Expert Comment

by:Jerry_Pang
ID: 11789973
Try this, add this to your declaration..

txtbxNewText.style = "text-align:center"
0
 

Author Comment

by:a1785
ID: 11792808
When I add txtbxNewText.style = "text-align:center" to my code it states that the property of txtbxNewText.style is read only.




0
 
LVL 9

Expert Comment

by:Jerry_Pang
ID: 11798435
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
 
LVL 3

Accepted Solution

by:
bigjim2000 earned 500 total points
ID: 11808143
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11808162
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11808188
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11808245
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
 

Author Comment

by:a1785
ID: 11811575
Thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question