我有两个表单Form1和Form 2.我传递一个函数,其中包含Form1中的三个值,并希望form2的load事件触发此函数。基本上我想"画" form2上的qseq,midline和hseq的值
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As New Form2
Form2.Visible=True
f2.DrawString(qseq, midline, hseq)
End Sub
End Class
窗体2:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub DrawString(ByVal qseq As String, ByVal midline As String, ByVal hseq As String)
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim drawString As String = qseq 'and similarly for midline and hseq
Dim drawFont As New System.Drawing.Font("Arial", 16)
Dim drawBrush As New _
System.Drawing.SolidBrush(System.Drawing.Color.Black)
Dim x As Single = 200.0
Dim y As Single = 100.0
Dim drawFormat As New System.Drawing.StringFormat
formGraphics.DrawString(drawString, drawFont, drawBrush, _
x, y, drawFormat)
drawFont.Dispose()
drawBrush.Dispose()
formGraphics.Dispose()
End Sub
End Class
当我运行它时,在form2上没有任何内容被打印,因为当触发load事件时,不会调用drawtring方法。我如何从load方法调用抽象字符串,因为抽象字符串接受参数并从Form1类调用。
答案 0 :(得分:2)
您应该重写form2,使得要绘制的项目在表单的构造函数中传递,然后用于表单的Paint事件中的实际绘图。
例如:
Public Class Form2
Private QSeq As String
Private Midline As String
Private HSeq As String
Protected Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Sub New(ByVal sQseq As String, ByVal sMidline As String, ByVal sHseq As String)
Me.New()
QSeq = sQseq
Midline = sMidline
HSeq = sHseq
End Sub
Private Sub Form2_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim drawString As String = QSeq 'and similarly for midline and hseq
Dim x As Single = 200.0
Dim y As Single = 100.0
Dim drawFormat As New System.Drawing.StringFormat
Using drawFont As New System.Drawing.Font("Arial", 16)
Using drawBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat)
End Using
End Using
End Sub
End Class
然后form1将使用新的构造函数调用表单2。另请注意,您必须使新创建的实例(f2)可见,而不是您问题中的默认实例(Form2)。
例如:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As New Form2("test", "test2", "test3")
f2.Visible = True
End Sub