首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

200分 C# RichTextBox 高度自适应有关问题

2013-09-07 
200分 C# RichTextBox 高度自适应问题解决200分全送纠结2天了。。。。动态创建RichTextBox,不要滚动条,高度随

200分 C# RichTextBox 高度自适应问题
解决200分全送
纠结2天了。。。。200分 C# RichTextBox 高度自适应有关问题
动态创建RichTextBox,不要滚动条,高度随内容变化而变化
代码:
    

    [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

        RichTextBox richTextBox = null;
        int sf = 0;
        int pnlHight = 0;
        void richTextBox_Resize(object sender, EventArgs e)
        {
            int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
            int lc = SendMessage(richTextBox.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
            sf = richTextBox.Font.Height * (lc + 1) + richTextBox.Location.Y;
            richTextBox.Height = sf-30;
            richTextBox.Size = new System.Drawing.Size(912, richTextBox.Height);        
        }

private void createControls(string Message, string SendName, string SendTime, int replyId, int i)
        {
            Panel pnl = new Panel();
            pnl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            richTextBox = new RichTextBox();
            richTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;       
            richTextBox.Name = "richTextBox" + "_" + i;
            richTextBox.TabIndex = 7;
            richTextBox.Text = Message;


            richTextBox.ReadOnly = true;      
            
            richTextBox.ScrollBars = RichTextBoxScrollBars.None;
            richTextBox.Anchor = (AnchorStyles.Right | AnchorStyles.Left);
            //得到RichTextBox高度
            int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
            int lc = SendMessage(richTextBox.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
            int sf = richTextBox.Font.Height * (lc + 1) + richTextBox.Location.Y;
            richTextBox.Height = sf;
            pnlHight = richTextBox.Height;

            richTextBox.Resize += new EventHandler(richTextBox_Resize);
            richTextBox.Location = new System.Drawing.Point(20, 30);
            pnl.Controls.Add(richTextBox);

            richTextBox.Text+="\r\n"+richTextBox.Location.X+"__Y:"+richTextBox.Location.Y;
            richTextBox.Text += "\r\n" +"Heihgt:"+ richTextBox.Height;

           pnl.Controls.Add(richTextBox);
            pnl.Location = new System.Drawing.Point(0,size);
            pnl.Name = "panel" + "_" + i;
            pnl.Size = new System.Drawing.Size(1200, richTextBox.Height+40);
            pnl.AutoScroll = true;
            this.pnlMessage.Controls.Add(pnl);
            size += richTextBox.Height + 40;
}



每个RichTextBox
20__Y:30  坐标,X:20 Y:30 
Heihgt:28  高度:28


200分 C# RichTextBox 高度自适应有关问题
RichTextBox的坐标会上下变化,上图中第一个位置偏下了,第二个开始都是正常的,

200分 C# RichTextBox 高度自适应有关问题
这2个图RichTextBox的位置会随RichTextBox的高度变化而向下面偏移很多,不知道什么原因,
200分 C# RichTextBox 高度自适应有关问题


还请大家看下上面代码有什么问题,纠结2天了,,
或者有什么更好的办法提供下,,,谢谢大家了
[解决办法]
RichTextBox外面套一个panel,定位pannel,RichTextBox定位0,0
[解决办法]
试试这样吧,AppendText("内容"+"\r"),后面加上\r换行

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            int lineheight = richTextBox1.Font.Height;
            int linescount = richTextBox1.Lines.Length;
            if (linescount * lineheight > richTextBox1.Height)
                richTextBox1.Height = lineheight * linescount;
        }

        private void timer1_Tick(object sender, EventArgs e)//定时插入数据
        {
            richTextBox1.AppendText("你好大恢复啊哈赶快哈个卡号干哈是\r");
        }

[解决办法]
肯定是你的size算法有问题,建设你把发关人,发送时间及richTextBox这所有的控件集成一个用户控件,richTextBox直接去用户控件里调整,外面位置就像你调整一个单独的控件一样调整了,或者装在一个panel控件里,直接调整panel也可以
[解决办法]
楼主解决了?恭喜楼主

热点排行