无法在MDI表单上画线

无法在MDI表单上画线

本文介绍了无法在MDI表单上画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Visual Basic2010.我创建了一个父窗体,在其中有两个子窗体.我在第一个子窗体上放置一个按钮(BtnLine),并想在按下按钮(BtnLine)时在第二个子窗体(FrmPlot)上画一条线.我已经编写了以下代码,但没有用.

I have Visual Basic 2010. I have created a parent Form and inside it I have two child forms. I place a button (BtnLine) on the first child form and want to draw a line on the second child form (FrmPlot) when I press the button (BtnLine). I have written the following code but doesn''t work.

Private Sub BtnLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLine.Click
 Dim G As Graphics
        G = FrmPlot.CreateGraphics
        Dim blackPen As New Pen(Color.Black, 5)
        '' Create points that define line.
        Dim point1 As New Point(100, 100)
        Dim point2 As New Point(500, 100)
        G.DrawLine(blackPen, point1, point2)
End Sub


有人可以帮我吗?

[edit]标签,添加了代码块-OriginalGriff [/edit]


Could anybody help me ?

[edit]Tags, code block added - OriginalGriff[/edit]

推荐答案

Public Class Form1
    Private _MDIChildren() As Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load
        ReDim _MDIChildren(1)

        _MDIChildren(0) = New MDIChild2
        _MDIChildren(0).MdiParent = Me
        _MDIChildren(0).Show()

        _MDIChildren(1) = New MDIChild1(_MDIChildren(0))
        _MDIChildren(1).MdiParent = Me
        _MDIChildren(1).Show()
    End Sub
End Class



在MDI子项中,我写道:



and in the MDI child, I wrote:

Public Class MDIChild1
    Private _target As Form

    Public Sub New(ByRef TargetForm As Form)
        InitializeComponent()
        _target = TargetForm
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using g As Graphics = _target.CreateGraphics
            g.DrawLine(New Pen(Brushes.Black), New Point(5, 5), New Point(25, 25))
        End Using
    End Sub
End Class



并按预期工作.

作为附带说明,您确实应该使用using语句创建图形.当它到达using语句的末尾时,它将自动处理该对象.



and it worked as expected.

As a side note, you really should use the using statement for creating your graphics. When it reaches the end of the using statement, it automatically disposes of the object.


这篇关于无法在MDI表单上画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:35