2014-10-10 2 views
0

어떻게 클래스 클래스 메서드에서 절을 사용할 수 있습니까? 예를 들어 내가 코드 picturebox1에 이미지를 그리려 : 내가VB.NET 클래스 메서드가 페인트 이벤트를 처리합니다.

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint 

PS를 사용하지 않고이 작업을 수행 할 수있는 방법

Handles clause requires a WithEvents variable defined in the containing type or one of its base types. 

:

Public Class cell 
    Public Sub draw_cell() Handles picturebox1.paint 
     code 
    End Sub 
End Class 

나는 오류가 있습니다. 나쁜 영어에 대해 유감스럽게 생각합니다.

+0

PictureBox1은 Cell 클래스의 멤버가 아닌 Form 클래스의 멤버입니다. 이 방법을 계속 사용하려면 PictureBox 인수를 사용하는 생성자가 필요하며 AddHandler 문을 사용하여 이벤트를 구독합니다. Windows Forms 프로그래밍 btw에 대한 입문서를 통해 많은 이점을 얻을 수 있습니다. 기본 사항을 모르는 경우에는 아주 멀리 할 수 ​​없습니다. –

+0

가능한 [다른 클래스/파일에 정의 된 개체 이벤트 처리] 중복 (http://stackoverflow.com/questions/24698988/handle-events-of-object-defined-in-another-class-file) – Plutonix

답변

0

캔버스에 그리는 루틴을 직접 만들 수 있습니다.

Option Strict On 
Option Explicit On 
Option Infer Off 
Public Class Form1 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim canvas As New Bitmap(PictureBox1.Width, PictureBox1.Height) 
     Dim canvasGraphics As Graphics = Graphics.FromImage(canvas) 
     Dim cellRect As New Rectangle(100, 100, 100, 100) 
     cell.draw(canvasGraphics, cellRect, Color.Red, New Pen(New SolidBrush(Color.Green), 1)) 
     PictureBox1.Image = canvas 
    End Sub 
    Public Class cell 
     Public Shared Sub draw(ByRef canvasGraphics As Graphics, ByVal cellRect As Rectangle, ByVal fillColor As Color, ByVal borderPen As Pen) 
      Dim renderedCell As New Bitmap(cellRect.Width, cellRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) 
      Dim borderRect As New Rectangle(0, 0, cellRect.Width - 1, cellRect.Height - 1) 
      Dim context As BufferedGraphicsContext 
      Dim bGfx As BufferedGraphics 
      context = BufferedGraphicsManager.Current 
      context.MaximumBuffer = New Size(cellRect.Width + 1, cellRect.Height + 1) 
      bGfx = context.Allocate(Graphics.FromImage(renderedCell), New Rectangle(0, 0, cellRect.Width, cellRect.Height)) 
      Dim g As Graphics = bGfx.Graphics 
      g.Clear(fillColor) 
      g.DrawRectangle(borderPen, borderRect) 
      bGfx.Render() 
      canvasGraphics.DrawImage(renderedCell, cellRect.Location) 
     End Sub 
    End Class 
End Class 
관련 문제