Wpf 如何连接两个表单
一种控制模拟的形式。 一种表示彩色玻璃窗的形式 我有一个Wpf 如何连接两个表单,wpf,vb.net,Wpf,Vb.net,一种控制模拟的形式。 一种表示彩色玻璃窗的形式 我有一个NumericUpDown1,其中包含一个我想在SecondForm上画线的数字列表,但是当我尝试运行该程序时,什么都没有显示 我就是这样编码的 Dim Redpen As New Pen(Color.Red) Dim i As Integer Dim MyGraphicsClass As Graphics = Me.CreateGraphics Dim a, b, c, d As Integer Priv
NumericUpDown1
,其中包含一个我想在SecondForm
上画线的数字列表,但是当我尝试运行该程序时,什么都没有显示
我就是这样编码的
Dim Redpen As New Pen(Color.Red)
Dim i As Integer
Dim MyGraphicsClass As Graphics = Me.CreateGraphics
Dim a, b, c, d As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
a = 0
b = 20
c = 30
d = 50
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
NumericUpDown1.Dock = System.Windows.Forms.DockStyle.None
NumericUpDown1.Maximum = 7
NumericUpDown1.Minimum = 1
Controls.Add(NumericUpDown1)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SecondForm.Show()
If NumericUpDown1.DecimalPlaces = 1 Then
MyGraphicsClass.DrawLine(Pens.Red, a, b, a, b)
End If
Number_Of_Lines = 2
For i = 1 Number_Of_Lines Step 1
If NumericUpDown1.DecimalPlaces = 2 Then
MyGraphicsClass.DrawLine(Pens.Red, a, c, a, c)
End If
Next i
Number_Of_Lines = 3
For i = 1 Number_Of_Lines Step 1
If NumericUpDown1.DecimalPlaces = 3 Then
MyGraphicsClass.DrawLine(Pens.Red, a, d, a, d)
End If
Next i
Number_Of_Lines = 4
For i = 1 Number_Of_Lines Step 1
If NumericUpDown1.DecimalPlaces = 4 Then
MyGraphicsClass.DrawLine(Pens.Red, a, c, a, c)
End If
Next i
Number_Of_Lines = 5
For i = 1 Number_Of_Lines Step 1
If NumericUpDown1.DecimalPlaces = 5 Then
MyGraphicsClass.DrawLine(Pens.Red, a, b, a, b)
End If
Next i
Number_Of_Lines = 6
For i = 1 Number_Of_Lines Step 1
If NumericUpDown1.DecimalPlaces = 6 Then
MyGraphicsClass.DrawLine(Pens.Red, a, c, a, c)
End If
Next i
Number_Of_Lines = 7
For i = 1 Number_Of_Lines Step 1
If NumericUpDown1.DecimalPlaces = 7 Then
MyGraphicsClass.DrawLine(Pens.Red, a, c, a, c)
End If
Next i
End Sub
结束类某些变量=firstform.NumericUpdown.YourObjectHere首先,不确定在您的
NumericUpdown 1\u值已更改
code中执行的操作。每次值更改时都要设置最小/最大值,然后将其重新添加到控件集合
其次,我看不出您的SecondForm
是如何声明/初始化的,或者是在哪里声明/初始化的,或者MyGraphicsClass
是什么,我认为这是回答您的问题所需要的两个主要部分,但是,以下是我所知道的您正在尝试做的事情
因为需要在第二个表单的paint
事件中绘制线条,所以需要能够读取第一个表单中的值。最好的方法是将第一种形式的实例分配给第二种形式的变量。(有许多不同的方法可以做到这一点。公共变量是最简单的)然后在第二个窗体的绘制事件中,可以读取第一个窗体控件的值。您需要将第二个表单的实例也保留在第一个表单中,以便在需要时告诉第二个表单重新绘制
--第一形式变量
Private _SecondForm as SecondForm
--当您第一次显示第二个表单时,如下所示:
If _SecondForm is nothing then
_SecondForm = new SecondForm
_SecondForm.MyFirstForm = me
End If
_SecondForm.show(me) 'You could also do this which sets the owner, then you can ask for the owner in the second form, instead of setting a variable.
现在,在第二个表单的实例中,您可以在第一个表单中随时使用它
--第二种形式的变量,注意这是在第一种形式中设置的(上面)
--在你的绘画活动中
e.Graphics.DrawLine(Pens.Red, MyFirstForm.NumericUpDown1.Value, MyFirstForm.NumericUpDown2.Value, MyFirstForm.NumericUpDown3.Value, MyFirstForm.NumericUpDown4.Value)
或者,如果您使用的是owner方法,则可以执行以下操作:
Dim x As FirstForm = Me.Owner
e.Graphics.DrawLine(Pens.Red, x.NumericUpDown1.Value, x.NumericUpDown2.Value, x.NumericUpDown3.Value, x.NumericUpDown4.Value)
为了提出这样的问题,你必须对自己的要求有一个基本的了解。我们不是从一开始就在这里教你,我们在这里是为了帮助你,当你陷入某些特殊的事情。请给我们看一些代码。我编辑了我的帖子,希望我们能解释一下。
Dim x As FirstForm = Me.Owner
e.Graphics.DrawLine(Pens.Red, x.NumericUpDown1.Value, x.NumericUpDown2.Value, x.NumericUpDown3.Value, x.NumericUpDown4.Value)