Solved

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

Posted on 2004-08-10
14
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Starting up a Project

707 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