在C#中的richtextbox中对齐对齐 [英] Justify alignment in richtextbox in c#

查看:446
本文介绍了在C#中的richtextbox中对齐对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在按钮上单击以获取RichTextBox中的对齐功能?

How to to get justify functionality in richtextbox on a button click ?

推荐答案

您可以使用selectionalignment

我不认为可以在新旧的RichTextBox中使用.您必须找到或创建一种可以手动对文本进行对齐的算法.
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.


只是当场就想到了算法来对文本进行对齐.

1)找到您的富文本框的长度(rtb_Length)和文本的长度(text_Length).
2) IF text_Length> rtb_Length ,减去diff = rtb_Length-text_Length. [这将为您提供多少空格].
3)如果要对正,请在文本的开头添加差异值的空格,否则在文本的末尾添加.

可以说text ="JustifyMyText"
因此,如果rtb_Length = 20,text_Length = 13,则diff =7.
为了证明正确,
text ="JustifyMyText"
左对齐,
text ="JustifyMyText"否则您就可以使text.trim()=>删除所有空格.

设置文字:
richtextbox.text =文本;

希望这会有所帮助!

更新的解决方案

试试这个,我已经测试过了,它可以工作:
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 :
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!


这篇关于在C#中的richtextbox中对齐对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆