Articles

Membuat Marquee (teks berjalan) pada aplikasi desktop

In Programming on May 5, 2010 by userinterface11 Tagged:

Buat project form baru (VB). Tambahkan satu buah label, beri nama “lblFlyText”. kemudian isi property text pada lblFlyText dengan tulisan apa saja, misal “fly text”. Copy saja text sehingga menjadi panjang. Tambahkan Komponen sebuah timer dengan interval 100.

untuk lebih jelasnya perhatikan gambar berikut:

masuk ke bagian sourcode.

buat sebuah properti dan enum sebagai berikut:


Private mRunningText As String

Public Property RunningText() As String
Get
   Return mRunningText
End Get
   Set(ByVal value As String)
mRunningText = value
End Set
End Property

Enum SlideTypeEnum
   Right
   Left
End Enum

Private mSlideType As SlideTypeEnum
Public Property SlideType() As SlideTypeEnum
Get
   Return mSlideType
End Get
Set(ByVal value As SlideTypeEnum)
   mSlideType = value
End Set
End Property

lalu,

pada method form_load, tulis sourcode berikut:


Private Sub Form_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
   BuildRunningText()
   lblRunningText.Text = Me.RunningText
   Timer1.Enabled = True
End Sub

kemudian isi dari timer1_tick()


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles Timer1.Tick
   Me.RunningText = SlideText(Me.RunningText, SlideTypeEnum.Left, 1)
   Me.lblRunningText.Text = Me.RunningText
End Sub

adapun method pendukung lain sebagai berikut:


Private Sub BuildRunningText()
   Dim fly_text As String = "running_text"
   Dim build_text As New StringBuilder

For index As Integer = 0 To lblRunningText.Text.Count
   If index < fly_text.Count Then
   build_text.Append(fly_text(index))
Else
   build_text.Append("  ")
End If
Next

For index As Integer = 0 To fly_text.Count
   build_text.Append("  ")
Next

   Me.RunningText = build_text.ToString
End Sub

Private Function SlideText(ByVal str As String, ByVal slide_type _
As SlideTypeEnum, ByVal as_much_as As Integer) As String
   Dim str_temp As New StringBuilder

   If slide_type = SlideTypeEnum.Right Then

   For index As Integer = (str.Count - as_much_as) To (str.Count - 1)
      str_temp.Append(str(index))
   Next

   For index As Integer = 0 To (str.Count - as_much_as - 1)
      str_temp.Append(str(index))
   Next
   Else
   For index As Integer = as_much_as To (str.Count - 1)
      str_temp.Append(str(index))
   Next

   For index As Integer = 0 To (as_much_as - 1)
      str_temp.Append(str(index))
   Next

   End If

   Return str_temp.ToString
End Function
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: