我想在private void button1_Click(object sender, EventArgs e)里面调用 private void pictureBox1_Paint(object sender, PaintEventArgs e)将float型的参数a,b,c传递给后者,请问高手如何实现?

解决方案 »

  1.   

    扩展PaintEventArgs类型参数,传入pictureBox1_Paint方法,也是一种办法。
      

  2.   

    全局变量传递不过去,一运行就加载了,如何扩展PaintEventArgs类型参数,传入pictureBox1_Paint方法,也是一种办法。?
      

  3.   

    float型的参数a,b,c 从何而来,不可能凭空冒出3个数吧,楼主说明白点
      

  4.   

    从ACCESS数据库中统计相应的列的总和,然后赋给a,b,c三个变量,这一步我已经实现,现在就是将这三个变量的值传递给 private   void   pictureBox1_Paint(object   sender,   PaintEventArgs   e),用这三个值的数据绘制饼图。现在就是传递不过去,其他的我已经实现。
          顺便问一下5楼,如何扩展PaintEventArgs类型参数,传入pictureBox1_Paint方法。
      

  5.   

    在页面的类里,所有函数之外,定义全局变量。
    可以用static来定义(VB里是Shared)
      

  6.   

    在类里定义全局或静态变量,将要传递的参数的值赋给全局(或静态)变量,OK
    ——————————————————————————————————————————————————
    可以的。你非要麻烦,也可以的
    public XPaintEventArgs:PaintEventArgs
    {
    public XPaintEventArgs():base()
    {}
     public float a = 0;
    public float b = 0;
    public float c = 0;
    }
    XPaintEventArgs  vgs= new XPaintEventArgs();
    vgs.a=12;
    vgs.b =23;
    vgs.c=34;pictureBox1_Paint(sender,vgs);在pictureBox1_Paint里在转换为XPaintEventArgs 类型。就可以获取到了。
      

  7.   

    定义一个类,包含a、b、c、三个变量,通过private void pictureBox1_Paint(object sender, null)中的sender传过去
      

  8.   

    15楼的解释靠铺,就用sender的参数就可以传过去。
      

  9.   

    可能是我的问题没有描述清楚吧,每次执行程序的时候private   void   pictureBox1_Paint(object sender,PaintEventArgs e)函数就已经执行了 ,再单击按钮的时候,虽然参数通过全局变量传递过去了但是由于没有从新执行,所以图像仍然没有变化。 我只是想在 单击按钮的 时候从新执行private   void   pictureBox1_Paint(object sender, PaintEventArgs e),全局变量我试用过了,不能在单击按钮的时候从新执行上面的函数,如果哪位高手可以帮我解决这个问题,我将120分全部给他,并万分感激。
      

  10.   

    第一次执行pictureBox1_Paint时,有没有用到你说的3个参数?估计那3个参数都是0吧?
    用重载就可以了
    private void pictureBox1_Paint(object sender,PaintEventArgs e)
    {
    pictureBox1_Paint(sender,e,0f,0f,0f);
    }private void pictureBox1_Paint(object sender,PaintEventArgs e,float a,float b,float c)
    {
      做你的操作
    }
      

  11.   

    使用重载如何在private   void   button1_Click(object   sender,   EventArgs   e)中调用?PaintEventArgs e在button1_Click里没有相应的定义啊 .
      

  12.   

    在button1_Click中调用时,直接:
    pictureBox1_Paint(null,null,1f,2f,3f)
    就可以了。
      

  13.   

    你好,我按 你说的在button1_Click中调用时,直接: pictureBox1_Paint(null,null,1f,2f,3f) ,但是单击按钮的时候提示说没有将引用设置到对象的实例
      

  14.   

    那么你的
    pictureBox1_Paint(null,null,1f,2f,3f)
    方法里是不是使用了第一个或第二个参数?如果用到了,当然会报这个错误了,
    你最好把你的代码帖出来看看。
      

  15.   

    一般情况,我是这样做的:
    不使用PictureBox控件,而是建一个新的控件。在这个控件中定义这3个参数的属性,然后Override这个控件的OnPaint事件,在这里进行画图。
    在主程序中加这个控件,并在button1_Click中将得到的a,b,c值设置到这个控件的3个参数属性,再调用这个控件的Invalidate()方法即可。public class MyPicture: System.Windows.Forms.UserControl
    {
        public MyPicture()
        {
             SetStyle(ControlStyles.AllPaintingInWmPaint
    |ControlStyles.DoubleBuffer
    |ControlStyles.ResizeRedraw 
    |ControlStyles.UserPaint,true);
        }
        ...    //定义3个属性     protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged (e);
            this.Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            //在这里画图
        }
    }在主程序的button1_Click中计算出a,b,c值,然后
    MyPicture1.属性a=a;
    MyPicture1.属性b=b;
    MyPicture1.属性x=c;
    MyPicture1.Invalidate();
    就可以了。这样做,还 有一个好处就是可以在控件改变尺寸时,自动刷新,并且可以减少画图时的闪烁。
      

  16.   

    我能不能再button1_Click中定义一个PaintEventArgs   e?
      

  17.   

    我得代码:
     float tcp=1;
            float udp2=1;
            float gbb=1;
            float sum=0;        private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {//绘制饼图
                sum = tcp + udp2 + gbb;
                tcp = tcp*360 / sum;
                udp2 = udp2*360 / sum;
                gbb = gbb*360 / sum;
                Graphics g;
                g = e.Graphics;
                g.FillPie(new SolidBrush(Color.Blue), 80, 25, 200, 200, 0, tcp);
                Graphics g1;
                g1 = e.Graphics;
                g1.FillPie(new SolidBrush(Color.Red), 80, 25, 200, 200, tcp, udp2); 
                Graphics g2;
                g2 = e.Graphics;
                g2.FillPie(new SolidBrush(Color.Yellow), 80, 25, 200, 200, tcp + udp2, gbb);
                g.FillRectangle(new SolidBrush(Color.Blue), 45, 240, 20, 20);
                g.FillRectangle(new SolidBrush(Color.Red), 145, 240, 20, 20);
                g.FillRectangle(new SolidBrush(Color.Yellow), 245, 240, 20, 20);
                System.Drawing.Point myTCP = new System.Drawing.Point(65, 243);
                System.Drawing.Point myUDP = new System.Drawing.Point(165, 243);
                System.Drawing.Point myGBB = new System.Drawing.Point(265, 243);
                g.DrawString("TCP比例", new System.Drawing.Font("宋体", 10), Brushes.Black, myTCP);
                g.DrawString("UDP比例", new System.Drawing.Font("宋体", 10), Brushes.Black, myUDP);
                g.DrawString("广播比例", new System.Drawing.Font("宋体", 10), Brushes.Black, myGBB);
            }
           
            private void button1_Click(object sender, EventArgs e)
            {//图象分析按钮
                //清空TIP表内的内容
                string StrSQLC = "delete from ZL  ";
                this.oleDbCommand1.CommandText = StrSQLC;
                this.oleDbCommand1.Connection = this.oleDbConnection1;
                this.oleDbConnection1.Open();
                this.oleDbCommand1.ExecuteNonQuery();
                this.oleDbConnection1.Close();
                //统计TIP表中的数据并写入ZL表中
               // string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ipdata.mdb";
                string StrSQL = "insert into ZL(TCP总量,UDP总量,广播总量) select sum(TCP包总量) ,sum(UDP包总量) ,sum(广播包总量)  from TIP  ";
                this.oleDbCommand1.CommandText = StrSQL;
                this.oleDbCommand1.Connection = this.oleDbConnection1;
                this.oleDbConnection1.Open();
                this.oleDbCommand1.ExecuteNonQuery();
                this.oleDbConnection1.Close();            //从ZL表中选择所有记录显示
                string ConnString2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ipdata.mdb";
                string SQLString = "select * from ZL";
                System.Data.OleDb.OleDbConnection OleDBConn1 = new System.Data.OleDb.OleDbConnection(ConnString2);
                DataSet DataSet1 = new DataSet();
                System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1);
                OleDBConn1.Open();
                OleDbDataAdapter1.Fill(DataSet1, "ZL");
                //设置DataGridView的行高度
                DataGridViewRow row = this.dataGridView1.RowTemplate;
                row.Height = 15;
                row.MinimumHeight = 12;            
                dataGridView2.DataSource = DataSet1.Tables["ZL"];
                tcp = 2;
                udp2 = 1;
                gbb = 3;
                pictureBox1_Paint(null,null);
                //清空ZL表内的内容
                string StrSQLC2 = "delete from ZL ";
                this.oleDbCommand1.CommandText = StrSQLC2;
                this.oleDbCommand1.Connection = this.oleDbConnection1;
                this.oleDbConnection1.Open();
                this.oleDbCommand1.ExecuteNonQuery();
                this.oleDbConnection1.Close();
            }   
      

  18.   

    你在按扭的事件里调用如下的方法就会使pictureBox1重画了:
    pictureBox1.InvaliDate();
      

  19.   

    楼上说的完全可行,已经试验过了,字母D要小写。
    pictureBox1.Invalidate();
      

  20.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace 画图_如何传参数到paint
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            int x = 0, y = 0, x1 = 15, y1 = 15;
            private void button1_Click(object sender, EventArgs e)
            {
                x = 4;
                y = 4;
                x1 = 100;
                y1 = 100;
                panel1.Invalidate();
            }        private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Pen myPen = new Pen(Color.Red );
                Graphics g = e.Graphics;
                g.DrawLine(myPen , x, y, x1, y1);        }
        }
    }