我在一个textBox中画一条直线,输入文字之后直线就消失了,如果先写文字再画下的话,鼠标在别处点一下直线也会消失,我想要那种又能画图又能写文本的效果,怎么做
代码如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool paint=false;
        Point point1;
        Pen redpen = new Pen(Color.Red, 3);
        int y;
        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            paint = true;
            if (paint == true)
            {
                point1 = new Point(e.X, e.Y);
                y = e.Y;
                
            }
        }        private void textBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (paint == true)
            {
                Point point2 = new Point(e.X, y);
                Graphics g = this.CreateGraphics();
                textBox1.CreateGraphics().DrawLine(redpen, point1, point2);
            }
        }        private void textBox1_MouseUp(object sender, MouseEventArgs e)
        {
            paint = false;
        }
    }
}

解决方案 »

  1.   

    textbox自己有刷新啊,你画线当然会在他刷新后被抹掉了,c#我是小白,不过我做过delphi类似的例子,思路就是自定义一个text控件,重载他的刷新方法即可,或者你重载form的刷新方法,不过这样逻辑会很乱,不好维护而且还容易产生界面刷新不完全,闪烁等bug
      

  2.   

    http://topic.csdn.net/t/20060720/15/4893528.html
    这里有个自定义控件的c#例子,可以参考下/
      

  3.   

    TextBox中TextChanged()事件触发后,会引发TextBox的重绘,你应该同时重绘你的这根线。
      

  4.   

    补充一下4Ltextbox的重绘可不只在textchange,所以根本的解决方法还是在textchange控件自身的刷新方法
      

  5.   

    有倒是有,你每次画线的时候,把线条记录下来,然后重载 OnPaint 函数 ,重绘这些线条
      

  6.   

    直接重载 窗体 的
    protected override void OnPaint(PaintEventArgs e)
    函数