Public Class Form1
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'Program Version Check...
label1.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
End Sub
End Class
- 사용한 컨트롤 : Button 2개, TextBox 2개, Label 3개, 프로그래스바 1개, Timer 1개
전체 소스 코드
Form1.vb
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
timer1.Interval = 1000 '1초마다...
End Sub
Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
MyBase.OnClosed(e)
timer1.Stop()
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
timer1.Start()
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
timer1.Stop()
End Sub
Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
Dim psStatus As PowerStatus = SystemInformation.PowerStatus
'충전 상태
txtChargeStatus.Text = psStatus.BatteryChargeStatus.ToString()
'전원 상태
txtPoerStatus.Text = psStatus.PowerLineStatus.ToString()
'충전 비율
If psStatus.BatteryLifePercent <> 255 Then
pbCharge.Value = Convert.ToInt32(psStatus.BatteryLifePercent * 100)
Else
pbCharge.Value = 0
End If
'잔여 사용 시간
If psStatus.BatteryLifeRemaining <> -1 Then
textBox1.Text = TimeSpan.FromSeconds(psStatus.BatteryLifeRemaining).ToString()
Else
textBox1.Text = "-------"
End If
'완충시 사용 시간
If psStatus.BatteryFullLifetime <> -1 Then
textBox2.Text = psStatus.BatteryFullLifetime.ToString()
Else
textBox2.Text = "-------"
End If
End Sub
End Class
-WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 Imports System.Management
- 사용한 컨트롤 : Button 1개, Label 1개
전체 소스 코드
Form1.vb
Imports System.Management
Public Class Form1
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'조회...
Using mos As ManagementObjectSearcher = New ManagementObjectSearcher("Select * From Win32_DisplayConfiguration")
'그래픽 카드 정보 얻기...
For Each moj As ManagementObject In mos.Get()
label1.Text = moj("Description").ToString()
Next
End Using
End Sub
End Class
* 예제 결과
버튼 클릭 시 위와 같이 그래픽카드 정보를 얻어 올 수 있습니다.
아래 마이크로소프트 문서를 참조 하시면 Win32_DisplayConfiguration 테이블에 필드들이 무엇이 있는지 알 수 있습니다.
- 사용한 컨트롤: Panel 3개, Label 1개, TextBox 1개, Button 1개, richTextBox 1개
전체 소스 코드
Form1.vb
Public Class Form1
Dim iFindStartIndex As Integer = 0
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'찾는 문자열 길이
Dim iFindLength As Integer = textBox1.Text.Length
iFindStartIndex = FindMyText(textBox1.Text, iFindStartIndex, richTextBox1.Text.Length)
If iFindStartIndex = -1 Then
iFindStartIndex = 0
Return
End If
'찾은 문자열 선택해서 붉은색으로 바꾸기
richTextBox1.SelectionColor = Color.Red
richTextBox1.Select(iFindStartIndex, iFindLength)
'다음 찾기를 위해 찾은 문자열 위치 저장
iFindStartIndex += iFindLength
End Sub
Private Function FindMyText(ByVal strSearchText As String, _
ByVal iSearchStart As Integer, _
ByVal iSearchEnd As Integer) As Integer
' Initialize the return value to false by default.
Dim ReturnValue As Integer = -1
' Ensure that a search string and a valid starting point are specified.
If strSearchText.Length > 0 And iSearchStart >= 0 Then
' Ensure that a valid ending value is provided.
If iSearchEnd > iSearchStart Or iSearchEnd = -1 Then
' Obtain the location of the search string in richTextBox1.
Dim indexToText As Integer = richTextBox1.Find(strSearchText, iSearchStart, iSearchStart, RichTextBoxFinds.MatchCase)
' Determine whether the text was found in richTextBox1.
If indexToText >= 0 Then
' Return the index to the specified search text.
ReturnValue = indexToText
End If
End If
End If
Return ReturnValue
End Function
End Class
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Dim iCount As Integer
For iCount = 0 To 10 - 1 Step iCount + 1
Dim lvi As ListViewItem = New ListViewItem()
lvi.Text = (iCount + 1).ToString()
lvi.SubItems.Add("Col1")
lvi.SubItems.Add("Col2")
lvi.SubItems.Add("Col3")
lvi.SubItems.Add("Col4")
listView1.Items.Add(lvi)
Next
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'CSV File Save
Dim sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "CSV File(*.csv) | *.csv"
If sfd.ShowDialog() = DialogResult.OK Then
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(sfd.FileName, False, System.Text.Encoding.GetEncoding(949))
'데이터
Dim i As Integer
For i = 0 To listView1.Items.Count - 1 Step i + 1
Dim strTmp As String = ""
strTmp += listView1.Items(i).SubItems(0).Text + "," + _
listView1.Items(i).SubItems(1).Text + "," + _
listView1.Items(i).SubItems(2).Text + "," + _
listView1.Items(i).SubItems(3).Text + "," + _
listView1.Items(i).SubItems(4).Text
sw.Write(strTmp + "\r\n")
Next
sw.Flush()
sw.Close()
MessageBox.Show("CSV 파일로 저장이 완료 되었습니다.")
End If
End Sub
End Class
* VBNET API 를 이용한 화면 캡쳐 방지 (Screen Capture Prevention) 예제...
-사용한 컨트롤 : Button 1개
전체 소스 코드
Form1.vb
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> Private Shared Function SetWindowDisplayAffinity(ByVal hWnd As IntPtr, _
ByVal dwAffinity As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Const ui_NONE As UInteger = &H0
Private Const ui_SET As UInteger = &H1
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
If button1.Text = "캡처 방지 설정하기" Then
SetWindowDisplayAffinity(Me.Handle, ui_SET)
button1.Text = "캡처 방지 해제하기"
Else
SetWindowDisplayAffinity(Me.Handle, ui_NONE)
button1.Text = "캡처 방지 설정하기"
End If
End Sub
End Class
* VBNET Listview 데이터 조회 BeginUpdate , EndUpdate 예제
-사용한 컨트롤: Button 2개, Listview 1개
전체 소스 코드
Form1.vb
Public Class Form1
Dim dtStart As DateTime
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'Listview Search
listView1.Items.Clear()
dtStart = DateTime.Now
Dim i As Integer
For i = 0 To 30000 - 1 Step i + 1
Dim lvi As ListViewItem = New ListViewItem()
lvi.Text = (i + 1).ToString()
lvi.SubItems.Add("TEST " + (i + 1).ToString())
listView1.Items.Add(lvi)
Next
MessageBox.Show(After_Time(DateTime.Now, dtStart).ToString() + " 초")
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'ListView Search Begin End Update
listView1.Items.Clear()
dtStart = DateTime.Now
listView1.BeginUpdate()
Dim i As Integer
For i = 0 To 30000 - 1 Step i + 1
Dim lvi As ListViewItem = New ListViewItem()
lvi.Text = (i + 1).ToString()
lvi.SubItems.Add("TEST " + (i + 1).ToString())
listView1.Items.Add(lvi)
Next
listView1.EndUpdate()
MessageBox.Show(After_Time(DateTime.Now, dtStart).ToString() + " 초")
End Sub
Private Function After_Time(ByVal dtNow As DateTime, ByVal dtBefore As DateTime) As Double
Dim ts As TimeSpan = dtNow - dtBefore
Return ts.TotalSeconds
End Function
End Class
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