当前位置: 代码迷 >> VB Dotnet >> 怎么在类中 使用控件
  详细解决方案

怎么在类中 使用控件

热度:119   发布时间:2016-04-25 02:15:10.0
如何在类中 使用控件
在form1中有个PictureBox1,我建一个图像类

我在类里建一个画图的函数,函数是在PictureBox1上作画

请问如何在类中得到PictureBox1控件

    Public Sub Draw()
        Dim g As Graphics
        Dim ipen As Pen = New Pen(Color.Red)
        ipen.Width = 1 '画图参数设置初始化
        g = PictureBox1.CreateGraphics ‘PictureBox1这个得不到
        g.TranslateTransform(0, PictureBox1.Height)
        g.ScaleTransform(1, -1)

    End Sub

------解决方案--------------------
在你的类里声明个PictureBox类型的句柄,然后指向你的PictureBox1对象,就行了
------解决方案--------------------
PictureBox pic = new PictureBox();
pic.Paint +=new PaintEventHandler(xxxxx);

然后再xxxx方法中写绘图代码
------解决方案--------------------
也可以用参数传递进来。
    Public Sub Draw(Byref picBox as System.Windows.Forms.PictureBox)
        Dim g As Graphics
        Dim ipen As Pen = New Pen(Color.Red)
        ipen.Width = 1 '画图参数设置初始化
        g = picBox.CreateGraphics ‘PictureBox1这个得不到
        g.TranslateTransform(0, PictureBox1.Height)
        g.ScaleTransform(1, -1)
 
    End Sub

------解决方案--------------------
Public Class Class1


    Private PictureBox1 As PictureBox
    Public Property _PictureBox1() As PictureBox
        Get
            Return PictureBox1
        End Get
        Set(ByVal value As PictureBox)
            PictureBox1 = value
        End Set
    End Property
    Public Sub MySub()
        Dim g As Graphics
        Dim ipen As Pen = New Pen(Color.Red)
        ipen.Width = 1 '画图参数设置初始化
        g = PictureBox1.CreateGraphics 'PictureBox1这个得不到
        g.TranslateTransform(0, PictureBox1.Height)
        g.ScaleTransform(1, -1)

    End Sub


End Class

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim cla1 As New Class1
        cla1._PictureBox1 = Me.PictureBox1
        cla1.MySub()
    End Sub
End Class


具体实现什么画图自己改进代码就行。


  相关解决方案