Listaa prosessit. Käyttäjällä voi päivittää prosessit sekä lopettaa niitä. Tämän vinkin on tarkoitus havannoillistaa ListBoxin sekä prosessien käyttöä.
'
' Koodi (c) Jussi "Juice" Kilpeläinen 2004
'
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Diagnostics
Namespace DefaultNamespace
Public Class MainForm
Inherits System.Windows.Forms.Form
Private label1 As System.Windows.Forms.Label
Private button1 As System.Windows.Forms.Button
Private listBox1 As System.Windows.Forms.ListBox
Private checkBox1 As System.Windows.Forms.CheckBox
Public Shared Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub
Public Sub New()
MyBase.New
'
' The Me.InitializeComponent call is required for Windows Forms designer support.
'
Me.InitializeComponent
'
' TODO : Add constructor code after InitializeComponents
'
End Sub
#Region " Windows Forms Designer generated code "
' This method is required for Windows Forms designer support.
' Do not change the method contents inside the source code editor. The Forms designer might
' not be able to load this method if it was changed manually.
Dim p As Process
Private Sub InitializeComponent()
Me.checkBox1 = New System.Windows.Forms.CheckBox
Me.listBox1 = New System.Windows.Forms.ListBox
Me.button1 = New System.Windows.Forms.Button
Me.label1 = New System.Windows.Forms.Label
Me.SuspendLayout
'
'checkBox1
'
Me.checkBox1.Location = New System.Drawing.Point(24, 208)
Me.checkBox1.Name = "checkBox1"
Me.checkBox1.TabIndex = 1
Me.checkBox1.Text = "Aakkosjärjestys"
AddHandler Me.checkBox1.CheckedChanged, AddressOf Me.CheckBox1CheckedChanged
'
'listBox1
'
Me.listBox1.Location = New System.Drawing.Point(32, 16)
Me.listBox1.Name = "listBox1"
Me.listBox1.Size = New System.Drawing.Size(280, 186)
Me.listBox1.TabIndex = 0
AddHandler Me.listBox1.SelectedIndexChanged, AddressOf Me.ListBox1SelectedIndexChanged
'
'button1
'
Me.button1.Location = New System.Drawing.Point(8, 48)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(16, 96)
Me.button1.TabIndex = 3
Me.button1.Text = "Päivitä"
AddHandler Me.button1.Click, AddressOf Me.Button1Click
'
'label1
'
Me.label1.Location = New System.Drawing.Point(152, 216)
Me.label1.Name = "label1"
Me.label1.TabIndex = 2
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(336, 238)
Me.Controls.Add(Me.button1)
Me.Controls.Add(Me.label1)
Me.Controls.Add(Me.checkBox1)
Me.Controls.Add(Me.listBox1)
Me.Name = "MainForm"
Me.Text = """Tehtävienhallinta"""
AddHandler Load, AddressOf Me.MainFormLoad
Me.ResumeLayout(false)
End Sub
#End Region
Private Sub MainFormLoad(sender As System.Object, e As System.EventArgs)
'Että näkyy nätisti
ListBox1.DisplayMember = "ProcessName"
'Päivittää listboxin heti aluksi
PaivitaListBox()
End Sub
Private Sub ListBox1SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
'Kysytään varmistus
Dim tulos As DialogResult = MessageBox.Show("Haluatko varmasti lopettaa prosessin " & ListBox1.Text & "?", "Varoitus", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If tulos = DialogResult.Yes Then
'Jos painettiin kyllä niin käydään prosessit silmukassa läpi.
'Jos löytyi oikea prosessi, lopeta se
For Each p In Process.GetProcesses()
If p.ToString = "System.Diagnostics.Process (" & ListBox1.Text & Chr(41) Then
p.Kill()
End If
Next
ElseIf tulos = DialogResult.No Then
Exit Sub
End If
End Sub
Private Sub PaivitaListBox()
'Tyhjennä entinen
ListBox1.Items.Clear
'Käydään prosessit loopissa läpi ja lisäillään
For Each p In Process.GetProcesses()
ListBox1.Items.Add(p)
Next
'Prosessien määrä labeliin
Label1.Text = "Prosesseja: " & ListBox1.Items.Count
End Sub
'Jos vaihdetaan aakkosjärjestys pois/päälle
Private Sub CheckBox1CheckedChanged(sender As System.Object, e As System.EventArgs)
If Checkbox1.Checked=True Then
ListBox1.Sorted = True
Else
ListBox1.Sorted = False
End If
End Sub
'Painettaessa päivitä-nappia päivitä listbox
'Alkujaan koodissa oli timer, vaan välkkyminen häiritsi
Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
PaivitaListBox()
End Sub
End Class
End NamespaceTohon jäi tuo " Windows Forms Designer generated code ", ettei kaikkia komponentteja tarvitse manuaalisesti lätkiä... Kommentoikaa reippaasti.
Aihe on jo aika vanha, joten et voi enää vastata siihen.