将许多控件移动到一起以表示VB.NET中的座位图

时间:2017-12-11 13:54:33

标签: vb.net

问候语, 我想创建一个座位表的图形表示(可能在婚礼场地或汽车......)。我使用了分割面板,在splittedPanel1中我按下按钮来创建汽车,而在拆分面板2里面我想放置图形表示。在SplittedPanel2中,我创建了一个面板和一个pictureBox(代表现实世界中的一些固定区域)。 我还创建了一个名为SimpleCar的类。 SimpleCar由一个面板中的5个TextBox和一个PictureBox组成来表示汽车:textBoses表示乘客姓名和汽车标签,以及用于放置代表汽车的图像的pictureBox(或表格。我还创建了一个子动态添加动态SimpleCar。 当我想使用MouseDown和MouseUp事件移动这个新面板(动态创建)时会出现2个问题: - 第一个pb:移动现有面板时,屏幕闪烁移动不顺畅 - 第二个pb:我无法移动动态创建的面板

请注意,通过此代码移动PictureBox非常流畅,但移动面板不是用户友好的。

我希望顺利地动态创建一个面板或者我应该重新考虑以另一种方式显示汽车而不是面板? 知道代码的最终目的是导出场地中所有创建的表的图片。 我还使用groupBox 测试了代码,但结果并不好。

simpleCar类在以下代码中描述:

Class SimpleCar
    Public carNameBox, passengerNameBox1, passengerNameBox2,
            passengerNameBox3, passengerNameBox4 As TextBox
    Public carPictureBox As PictureBox
    Public carGroup As Panel
    Public Sub New()

        carGroup = New Panel

        carNameBox = New TextBox With {.Text = "carNmBx",
                                              .BackColor = Color.Yellow,
                                             .Name = "carNmBx"}
        passengerNameBox1 = New TextBox With {.Text = "txtPassNmBx1",
                                              .BackColor = Color.BlanchedAlmond,
                                             .Name = "TextBox1"}
        passengerNameBox2 = New TextBox With {.Text = "txtPassNmBx2",
                                               .BackColor = Color.AliceBlue,
                                             .Name = "TextBox2"}
        passengerNameBox3 = New TextBox With {.Text = "txtPassNmBx3",
                                             .BackColor = Color.Azure,
                                             .Name = "TextBox3"}
        passengerNameBox4 = New TextBox With {.Text = "txtPassNmBx4",
                                              .BackColor = Color.Cyan,
                                             .Name = "TextBox4"}
        carPictureBox = New PictureBox With {.Text = "picBx1",
                                             .BackColor = Color.BlanchedAlmond,
                                             .Name = "picBox1"}

        Dim fdialog As New OpenFileDialog()
        fdialog.FileName = String.Empty
        fdialog.Multiselect = True

        If fdialog.ShowDialog = DialogResult.OK Then
            If fdialog.FileNames.Length = 2 Then
                carPictureBox.Image = Image.FromFile(fdialog.FileNames(0))

            ElseIf fdialog.FileNames.Length = 1 Then
                carPictureBox.Image = Image.FromFile(fdialog.FileName)

            End If
        End If
        carGroup.Controls.Add(carPictureBox)
        carGroup.Controls.Add(carNameBox)
        carGroup.Controls.Add(passengerNameBox1)
        carGroup.Controls.Add(passengerNameBox2)
        carGroup.Controls.Add(passengerNameBox3)
        carGroup.Controls.Add(passengerNameBox4)

    End Sub

End Class

要在下面的代码中动态添加SimpleCar:

 Public Sub Add_car()
    Dim carType As SimpleCar
    carType = New SimpleCar
    Dim carPs1 = carType.passengerNameBox1
    Dim carPs2 = carType.passengerNameBox2
    Dim carPs3 = carType.passengerNameBox3
    Dim carPs4 = carType.passengerNameBox4
    Dim carNm = carType.carNameBox
    Dim carPic = carType.carPictureBox
    Dim carGroupBox = carType.carGroup
    SplitContainer1.Panel2.Controls.Add(carGroupBox)
  End Sub

因此,当我使用此代码移动面板时(如果您使用Panel替换PictureBox,甚至是GroupBox)会出现问题(当我想移动一个控件时,它工作正常:此示例中的PictureBox1) :

'Drag To move PictureBox1 along with mouse-------------------------------------------
Dim oldX As Short
Dim oldY As Short

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
    Dim X As Single = e.X
    Dim Y As Single = e.Y
    PictureBox1.Cursor = Cursors.SizeAll
    oldX = CShort(X)
    oldY = CShort(Y)
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
    Dim X As Single = e.X
    Dim Y As Single = e.Y
    PictureBox1.Cursor = Cursors.Default
End Sub
' to limit the movement within the app----------------------------------
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
    If e.Button = MouseButtons.Left Then
        Dim ProposedLocation As New Point(PictureBox1.Left - (oldX - e.X), PictureBox1.Top - (oldY - e.Y))
        PictureBox1.Left = CInt(IIf(ProposedLocation.X < 0, 0, IIf(ProposedLocation.X > SplitContainer1.Panel2.Width - PictureBox1.Width, SplitContainer1.Panel2.Width - PictureBox1.Width, ProposedLocation.X)))
        PictureBox1.Top = CInt(IIf(ProposedLocation.Y < 0, 0, IIf(ProposedLocation.Y > SplitContainer1.Panel2.Height - PictureBox1.Height, SplitContainer1.Panel2.Height - PictureBox1.Height, ProposedLocation.Y)))
    End If
End Sub

0 个答案:

没有答案