0

一种控制模拟的形式。一种表示彩色玻璃窗的形式。

我有一个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

结束类

4

2 回答 2

0

some_variable = firstform.NumericUpdown.YourObjectHere

于 2013-11-14T07:32:17.327 回答
0

首先,不确定您在NumericUpDown1_ValueChanged代码中在做什么。每次值更改时,您都在设置最小值/最大值,然后将其重新添加到控件集合中???

其次,我看不到您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.

现在您有了第二种形式的实例,您可以在第一种形式中随时使用。

--第二种形式的变量,注意这是在第一种形式中设置的(上图)

Public MyFirstForm as FirstForm

--在您的绘画活动中

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)
于 2013-11-14T15:18:30.410 回答