Skip to main content

Align Text in WinForm

  • August 29, 2019
  • 4 replies
  • 2 views

Alexander Castro

I am trying to align the text in a text box to the right. I realize I can just set the text align property to the right on the form but I want to be able to do this in my code. The C# command is

textBox1.TextAlign = HorizontalAlignment.Right;

 

I have tried the following:

set txt-TOTAL-BALANCE::TextAlign to new type System.Windows.Forms.HorizontalAlignment

 

But this does not specify left, center or right. I have also tried

 

set txt-TOTAL-BALANCE::TextAlign to new type System.Windows.Forms.TextBox, TextAlign(Right)

 

But this code is incorrect and will not compile.

4 replies

Chris Glazier
Forum|alt.badge.img+2

I am trying to align the text in a text box to the right. I realize I can just set the text align property to the right on the form but I want to be able to do this in my code. The C# command is

textBox1.TextAlign = HorizontalAlignment.Right;

 

I have tried the following:

set txt-TOTAL-BALANCE::TextAlign to new type System.Windows.Forms.HorizontalAlignment

 

But this does not specify left, center or right. I have also tried

 

set txt-TOTAL-BALANCE::TextAlign to new type System.Windows.Forms.TextBox, TextAlign(Right)

 

But this code is incorrect and will not compile.

The following works for me:

       set textBox1::TextAlign to type System.Windows.Forms.HorizontalAlignment::Right

If you add the following namespace directive to the top of the program:

      $set ilusing"System.Windows.Forms"

Then you can specify:

set textBox1::TextAlign to type HorizontalAlignment::Right


Alexander Castro
  • Author
  • Participating Frequently
  • August 29, 2019

The following works for me:

       set textBox1::TextAlign to type System.Windows.Forms.HorizontalAlignment::Right

If you add the following namespace directive to the top of the program:

      $set ilusing"System.Windows.Forms"

Then you can specify:

set textBox1::TextAlign to type HorizontalAlignment::Right

I originally had

set textBox1::TextAlign to NEW type System.Windows.Forms.HorizontalAlignment::Right 

and I was getting an error because of the "NEW'

I took it out and set it like yours and it worked!

Thank you!


Chris Glazier
Forum|alt.badge.img+2

I originally had

set textBox1::TextAlign to NEW type System.Windows.Forms.HorizontalAlignment::Right 

and I was getting an error because of the "NEW'

I took it out and set it like yours and it worked!

Thank you!

If you look at the MSDN docs for this here you will see that HorizontalAlignment is defined as an Enum and not a class. An enum is a value type which defines a set of constants so that you cannot instantiate it using the constructor New. 

Documentation of an Enum type can be found here.

Instances of classes are created using the New operator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and returns a reference to the instance.

 

 

 


  • September 7, 2019

I originally had

set textBox1::TextAlign to NEW type System.Windows.Forms.HorizontalAlignment::Right 

and I was getting an error because of the "NEW'

I took it out and set it like yours and it worked!

Thank you!

 If I understand your intent, this might also work:

set txt-TOTAL-BALANCE::RightToLeft to type System.Windows.Forms.RightToLeft::Yes