Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to to get justify functionality in richtextbox on a button click ?
Posted

You can use the selectionalignment property[^].
 
Share this answer
 
Comments
Firo Atrum Ventus 5-Jul-11 1:23am    
No, you can't. There's no justify. Just Left, Right and Center.
Abhinav S 5-Jul-11 1:26am    
Yeah sorry I misunderstood the questioni - maybe a custom control can help.
sanjay jangid 5-Jul-11 1:34am    
Yes the justification property in richtextbox in not available and
according your solution it's not possible ..please if u have any othe solution please send ..
thanks in advance ...
I don't think that is possible in a RichTextBox, old or new. You have to find or create an algorithm that manually justifies the text.
 
Share this answer
 
Comments
sanjay jangid 5-Jul-11 1:50am    
You can give some idea about algorithm by which i can get justify text functionality in richtextbox control ...
Just a on the spot thought for algorithm to justify the text.

1) Find the length of your richtextbox (rtb_Length) and lengh of text (text_Length).
2) IF text_Length > rtb_Length, Subtract the diff = rtb_Length - text_Length. [This will give you how many space is blank].
3) If you want to Justify RIGHT, then add spaces of diff value in the begining of text, else to the end of text.

Lets say text = "JustifyMyText"
So if rtb_Length = 20, text_Length = 13, then diff = 7.
FOR Justify RIGHT,
text = " JustifyMyText"
FOR Justify LEFT,
text = "JustifyMyText " OR yuo can just make text.trim() => remove all spaces.

Set the text :
richtextbox.text = text;

Hope this helps!

UPDATED SOLUTION

Try this one, I have tested and it works :
C#
private System.Windows.Forms.RichTextBox richTextBox1;

// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(171, 13);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.richTextBox1.Size = new System.Drawing.Size(139, 53);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "Rich Text Box\nJustify Left & Right\nDoes it work ?";


private void button4_Click(object sender, EventArgs e)
{
    // Check if it is Left aligned
    if (richTextBox1.RightToLeft == RightToLeft.No)
        richTextBox1.RightToLeft = RightToLeft.Yes; // If so, turn it to Right Aligned
    else
        richTextBox1.RightToLeft = RightToLeft.No;  // If Right aligned, turn it to Left aligned
}


Hope this helps!
 
Share this answer
 
v2
Comments
sanjay jangid 6-Jul-11 1:46am    
i have used your algorithm but not get right result if you can any other solution please tell ..

thank you ...
All Time Programming 6-Jul-11 3:26am    
By default the RichTextBox's "RightToLeft" property is set to No which makes it left align.
On your button click event change "RightToLeft" property to "Yes" and your text will be right aligned.

Have updated the solution and also provided the code. Check out.
sanjay jangid 7-Jul-11 10:14am    
I Have used your given solution it's work to near that's may i request to you .
this cant not provide a complete justify alignment.
functionality that i requested as follow :-

after clicking the align button for justify the all text in each line the gap between word increased and
the each lines first word remain left align and each lines last words are right aligned

if you can provide your Email ID may i send printscreen shoots by which you can easly
what i requested .
thank you ...
All Time Programming 7-Jul-11 10:39am    
Are you saying is : This is the text
Rich Text Box
Justify Left & Right
Does it work ?
AND ON button click, you want is :
Rich Text Box
Justify Left & Right
Does it work ?
Something like this ? If not, send at : terry_513@yahoo.com
Confirm your requirements.
All Time Programming 8-Jul-11 6:12am    
Sanjay, the following links will help you achieve your goal :

Background Highlighting with the RichTextBox the System way:
http://www.codeproject.com/KB/cpp/tarrorichtextbox.aspx

Richer RichTextBox (Part 2):
http://www.codeproject.com/KB/edit/RicherRichTextBox.aspx

Look for Justification in below link
http://geekswithblogs.net/pvidler/archive/2003/10/15/182.aspx

Am sure the above links will provide you what you are looking for. I found the last one most helpful. Check out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900