Solved

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

Posted on 2004-08-10
14
419 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countPairs challenge 7 58
topping3 challenge 14 52
Image decoding from Camera 3 48
When i run adoquery my application freezes 26 102
This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now