2012年5月21日 星期一

[C#]幫我換行謝謝

今天不才小弟接到一個需求,是要將一串樂樂長的字串印到小小標籤上
每行22個字元,gotta be twenty two,twenty two lucky
不喇賽,換行而已嘛,很簡單,大家都會換行,不就是\n\r這麼簡單
不過後續來了,因為有人反應,單純用22這個幸運數字來抓index切字串的話
英文單字會被腰斬屍首分散兩地,所以必須判斷相連的英文字太長就去下一行
這功能用過word的人都知道這功能
就是這樣的感覺

遺憾的是標籤列印的程式不在我這邊
所以不能挑一個會自動換行的列印程式來印,我必須幫他換行
這時問題來了
我要去判斷連接的字和空白嗎?
字串裡包含中英文數字和特殊符號,分析這個會不會太"搞剛"?
因此我心頭一轉,C#的RichTextBox不是也會自動換行
如果宣告一個RichTextBox設定寬度讓他幫我換行不好嗎?
身為一個極力幫自己偷空閒時間的programmer,我想這是一個最快速的解法
以此方向為基準產生了第一次嘗試
        private void button1_Click(object sender, EventArgs e)
        {
            //要換行的字串
            string sourcetext = "Who knows what tomorrow brings In a world where few hearts survive All I know is the way I feel When it's real I keep it alive The road is long There are mountains in the way But we climb a  step every day Love lift us up where we belong Where the eagles cry On a mountain high Love lift us up where we belong Far from the world we know Up where the clear winds blow Some hang on to used to be Live their lives looking behind When all we have is here and now All our lives Out there to find The road is long There are mountains in the way But we climb a step every day Love lift us up where we belong Where the eagles cry On a mountain high Love lift us up where we belong Far from the world we know Up where the clear winds blow";
            //宣告一個RichTextBox來用
            RichTextBox rtbWrap = new RichTextBox();
            //設定自動換行
            rtbWrap.WordWrap = true;
            //設定寬度
            rtbWrap.Width = 140;
            //字串給rtb
            rtbWrap.Text = sourcetext;
            string resultstr = "";
            foreach (string line in rtbWrap.Lines)
            {
                resultstr += line + "\n";
            }
          MessageBox.Show(resultstr);
         }

看起來一切都很順利,但是出來的結果....
說好的換行呢?
遺憾的,rtbWrap.Lines裡面只有一個字串,也就是原本的樣子
這下可好,我要怎麼抓他的每一行呢?
不用怕,微軟有他貼心的一面RichTextBox中有這麼一個method
GetFirstCharIndexFromLine
這東西會根據你給他的行號回傳那一行第一個字的index
當沒有那一行的時候他則會回傳-1
有了這東西就一切好辦了啦
這是成果
        private void button1_Click(object sender, EventArgs e)
        {
            //要換行的字串
            string sourcetext = "Who knows what tomorrow brings In a world where few hearts survive All I know is the way I feel When it's real I keep it alive The road is long There are mountains in the way But we climb a  step every day Love lift us up where we belong Where the eagles cry On a mountain high Love lift us up where we belong Far from the world we know Up where the clear winds blow Some hang on to used to be Live their lives looking behind When all we have is here and now All our lives Out there to find The road is long There are mountains in the way But we climb a step every day Love lift us up where we belong Where the eagles cry On a mountain high Love lift us up where we belong Far from the world we know Up where the clear winds blow";

            //宣告一個RichTextBox來用
            RichTextBox rtbWrap = new RichTextBox();
            //設定自動換行
            rtbWrap.WordWrap = true;
            //設定寬度
            rtbWrap.Width = 140;
            //字串給rtb
            rtbWrap.Text = sourcetext;

            string resultstr = "";
            int i = 0;
            while (rtbWrap.GetFirstCharIndexFromLine(i) != -1)
            {
                int thislineindex = rtbWrap.GetFirstCharIndexFromLine(i);
                int nextlineindex = rtbWrap.GetFirstCharIndexFromLine(i + 1);
                if (nextlineindex == -1)
                {
                    resultstr += sourcetext.Substring(thislineindex) + "\n";
                }
                else
                {
                    resultstr += sourcetext.Substring(thislineindex, nextlineindex - thislineindex) + "\n";
                }
                i++;
            }

            MessageBox.Show(resultstr);
        }

就是這樣
換行的標準取決於RichTextBox的寬度,寫活一點的話可以隨著紙張大小去做調整
各位請自行運用

沒有留言:

張貼留言