VB.NET Programming

[VBNET] 움직이는 라벨 만들기

ZerosKD 2020. 5. 20. 09:43
반응형

* VBNET 움직이는 라벨 만들기 예제...

 

Main

 

 

-사용한 컨트롤 : Label 1 개, Panel 1개, Timer 1개

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Dim iLocationX As Integer = 1
    Dim iLocationY As Integer = 0
    Dim bCheck As Boolean = True

    Dim iSpeed As Integer = 5
    Dim iOffset As Integer = 10

    Public Sub New()

        ' 이 호출은 Windows Form 디자이너에 필요합니다.
        InitializeComponent()

        ' InitializeComponent() 호출 뒤에 초기화 코드를 추가하십시오.

        '라벨 Location Y 지정
        iLocationY = (panel1.Height - panel1.Location.Y) / 2
        Dim p As Point = New Point(iLocationX, iLocationY)
        label1.Location = p

    End Sub

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        timer1.Start()
    End Sub

    Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
        MyBase.OnClosed(e)
        timer1.Stop()
    End Sub

    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
        '앞으로
        If bCheck Then
            Dim iMove As Integer = iLocationX + label1.Width + iOffset
            Dim iEnd As Integer = panel1.Width

            If iMove <= iEnd Then
                iLocationX += iSpeed
                Dim p As Point = New Point(iLocationX, iLocationY)
                label1.Location = p
            Else '끝지점에 도달 했으면...
                bCheck = False
            End If

        Else '뒤로
            '처음으로 다시 왔으면...
            If iLocationX <= panel1.Location.X Then
                bCheck = True

            Else
                iLocationX -= iSpeed
                Dim p As Point = New Point(iLocationX, iLocationY)
                label1.Location = p
            End If
        End If
    End Sub
End Class

 


*예제 결과

 



반응형