Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

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
0
a1785
Asked:
a1785
  • 5
  • 4
  • 3
  • +1
1 Solution
 
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
 
adw060398Commented:
I looked it up and the property is TextAlign (it is align in VB6)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now