B.2 Lösungen der Übungsaufgaben aus Kapitel 2
B.2.1 Lösung ÜDatentypen
Public Class Form1
Private Sub cmdAnzeigen_Click(...) Handles ...
Dim nachname, vorname As String
Dim strasse, plz, ort As String
Dim alter As Integer
Dim geburtsdatum As Date
nachname = "Basic"
vorname = "Bodo"
strasse = "Bergstraße 34"
plz = "09445"
ort = "Brunnstadt"
alter = 32
geburtsdatum = "18.05.1985"
lblA.Text = "Adresse: " & vbCrLf & vorname &
" " & nachname & vbCrLf & strasse &
vbCrLf & plz & " " & ort & vbCrLf &
vbCrLf & "geb.: " & geburtsdatum &
vbCrLf & "Alter: " & alter
End Sub
End Class
B.2.2 Lösung ÜGültigkeitsbereich
Public Class Form1
Dim x As Double
Private Sub cmdAnzeigen1_Click(sender As Object,
e As EventArgs) Handles cmdAnzeigen1.Click
Dim y As Double
y = y + 0.1
x = x + 0.1
lblA.Text = "x: " & x & vbCrLf & "y: " & y
End Sub
Private Sub cmdAnzeigen2_Click(sender As Object,
e As EventArgs) Handles cmdAnzeigen2.Click
Dim z As Double
z = z + 0.1
x = x + 0.1
lblA.Text = "x: " & x & vbCrLf & "z: " & z
End Sub
End Class
B.2.3 Lösung ÜRechenoperatoren
Public Class Form1
Private Sub cmdAnzeigen1_Click(...) Handles ...
Dim x As Double
x = 3 * –2.5 + 4 * 2
lblA.Text = x
End Sub
Private Sub cmdAnzeigen2_Click(...) Handles ...
Dim x As Double
x = 3 * (-2.5 + 4) * 2
lblA.Text = x
End Sub
End Class
B.2.4 Lösung ÜVergleichsoperatoren
Public Class Form1
Private Sub cmdAnzeigen1_Click(...) Handles ...
Dim p As Boolean
p = 12 – 3 >= 4 * 2.5
lblA.Text = p
End Sub
Private Sub cmdAnzeigen2_Click(...) Handles ...
Dim p As Boolean
p = "Maier" Like "M??er"
lblA.Text = p
End Sub
End Class
B.2.5 Lösung ÜLogischeOperatoren
Public Class Form1
Private Sub cmdAnzeigen1_Click(...) Handles ...
Dim p As Boolean
p = 4 > 3 And –4 > –3
lblA.Text = p
End Sub
Private Sub cmdAnzeigen2_Click(...) Handles ...
Dim p As Boolean
p = 4 > 3 Or –4 > –3
lblA.Text = p
End Sub
End Class
B.2.6 Lösung ÜOperatoren
1: False, 2: True, 3: True, 4: True, 5: True, 6: False, 7: True, 8: False
B.2.7 Lösung ÜPanelZeitgeber
Public Class Form1
Private Sub cmdStart_Click(...) Handles ...
tim1.Enabled = True
End Sub
Private Sub tim1_Tick(...
) Handles tim1.Tick
pan1.Location = New Point(
pan1.Location.X – 5, pan1.Location.Y – 5)
pan2.Location = New Point(
pan2.Location.X + 5, pan2.Location.Y – 5)
pan3.Location = New Point(
pan3.Location.X – 5, pan3.Location.Y + 5)
pan4.Location = New Point(
pan4.Location.X + 5, pan4.Location.Y + 5)
End Sub
End Class
B.2.8 Lösung ÜKran
Bezeichnungen:
- f: Fundament
- s: senkrechtes Hauptelement
- a: waagerechter Ausleger
- h: senkrechter Haken am Ausleger
Public Class Form1
Private Sub cmdHakenAus_Click(...) Handles ...
h.Height = h.Height + 10
End Sub
Private Sub cmdHakenEin_Click(...) Handles ...
h.Height = h.Height – 10
End Sub
Private Sub cmdAuslegerAus_Click(...) Handles ...
a.Width = a.Width + 10
a.Location = New Point(
a.Location.X – 10, a.Location.Y)
h.Location = New Point(
h.Location.X – 10, h.Location.Y)
End Sub
Private Sub cmdAuslegerEin_Click(...) Handles ...
a.Width = a.Width – 10
a.Location = New Point(
a.Location.X + 10, a.Location.Y)
h.Location = New Point(
h.Location.X + 10, h.Location.Y)
End Sub
Private Sub cmdKranRechts_Click(...) Handles ...
f.Location = New Point(
f.Location.X + 10, f.Location.Y)
s.Location = New Point(
s.Location.X + 10, s.Location.Y)
a.Location = New Point(
a.Location.X + 10, a.Location.Y)
h.Location = New Point(
h.Location.X + 10, h.Location.Y)
End Sub
Private Sub cmdKranLinks_Click(...) Handles ...
f.Location = New Point(
f.Location.X – 10, f.Location.Y)
s.Location = New Point(
s.Location.X – 10, s.Location.Y)
a.Location = New Point(
a.Location.X – 10, a.Location.Y)
h.Location = New Point(
h.Location.X – 10, h.Location.Y)
End Sub
Private Sub cmdKranAus_Click(...) Handles ...
s.Height = s.Height + 10
s.Location = New Point(
s.Location.X, s.Location.Y – 10)
a.Location = New Point(
a.Location.X, a.Location.Y – 10)
h.Location = New Point(
h.Location.X, h.Location.Y – 10)
End Sub
Private Sub cmdKranEin_Click(...) Handles ...
s.Height = s.Height – 10
s.Location = New Point(
s.Location.X, s.Location.Y + 10)
a.Location = New Point(
a.Location.X, a.Location.Y + 10)
h.Location = New Point(
h.Location.X, h.Location.Y + 10)
End Sub
End Class
B.2.9 Lösung ÜSteuerbetrag, zwei Alternativen
Public Class Form1
Private Sub cmdBerechnen1_Click(...) Handles ...
Dim gehalt, steuersatz, steuerbetrag As Double
gehalt = Convert.ToDouble(txtGehalt.Text)
If gehalt <= 12000 Then
steuersatz = 12
ElseIf gehalt <= 20000 Then
steuersatz = 15
ElseIf gehalt <= 30000 Then
steuersatz = 20
Else
steuersatz = 25
End If
steuerbetrag = gehalt * steuersatz / 100
lblSteuerbetrag.Text = steuerbetrag
End Sub
Private Sub cmdBerechnen2_Click(...) Handles ...
Dim gehalt, steuersatz, steuerbetrag As Double
gehalt = Convert.ToDouble(txtGehalt.Text)
Select Case gehalt
Case Is <= 12000
steuersatz = 12
Case Is <= 20000
steuersatz = 15
Case Is <= 30000
steuersatz = 20
Case Else
steuersatz = 25
End Select
steuerbetrag = gehalt * steuersatz / 100
lblSteuerbetrag.Text = steuerbetrag
End Sub
End Class
B.2.10 Lösung ÜKranVerzweigung
Bezeichnungen:
- f: Fundament
- s: senkrechtes Hauptelement
- a: waagerechter Ausleger
- h: senkrechter Haken am Ausleger
Public Class Form1
Private Sub cmdHakenAus_Click(...) Handles ...
If h.Location.Y + h.Height + 5 <
f.Location.Y Then
h.Height = h.Height + 10
End If
End Sub
Private Sub cmdHakenEin_Click(...) Handles ...
If h.Height > 15 Then
h.Height = h.Height – 10
End If
End Sub
Private Sub cmdAuslegerAus_Click(...) Handles ...
If a.Location.X > 15 Then
a.Width = a.Width + 10
a.Location = New Point(
a.Location.X – 10, a.Location.Y)
h.Location = New Point(
h.Location.X – 10, h.Location.Y)
End If
End Sub
Private Sub cmdAuslegerEin_Click(...) Handles ...
If a.Width > 30 Then
a.Width = a.Width – 10
a.Location = New Point(
a.Location.X + 10, a.Location.Y)
h.Location = New Point(
h.Location.X + 10, h.Location.Y)
End If
End Sub
Private Sub cmdKranRechts_Click(...) Handles ...
If f.Location.X < 215 Then
f.Location = New Point(
f.Location.X + 10, f.Location.Y)
s.Location = New Point(
s.Location.X + 10, s.Location.Y)
a.Location = New Point(
a.Location.X + 10, a.Location.Y)
h.Location = New Point(
h.Location.X + 10, h.Location.Y)
End If
End Sub
Private Sub cmdKranLinks_Click(...) Handles ...
If f.Location.X > 15 And
a.Location.X > 15 Then
f.Location = New Point(
f.Location.X – 10, f.Location.Y)
s.Location = New Point(
s.Location.X – 10, s.Location.Y)
a.Location = New Point(
a.Location.X – 10, a.Location.Y)
h.Location = New Point(
h.Location.X – 10, h.Location.Y)
End If
End Sub
Private Sub cmdKranAus_Click(...) Handles ...
If s.Location.Y > 15 Then
s.Height = s.Height + 10
s.Location = New Point(
s.Location.X, s.Location.Y – 10)
a.Location = New Point(
a.Location.X, a.Location.Y – 10)
h.Location = New Point(
h.Location.X, h.Location.Y – 10)
End If
End Sub
Private Sub cmdKranEin_Click(...) Handles ...
If h.Location.Y + h.Height + 5 <
f.Location.Y Then
s.Height = s.Height – 10
s.Location = New Point(
s.Location.X, s.Location.Y + 10)
a.Location = New Point(
a.Location.X, a.Location.Y + 10)
h.Location = New Point(
h.Location.X, h.Location.Y + 10)
End If
End Sub
End Class
B.2.11 Lösung ÜKranOptionen
Bezeichnungen:
- f: Fundament
- s: senkrechtes Hauptelement
- a: waagerechter Ausleger
- h: senkrechter Haken am Ausleger
Public Class Form1
Private Sub cmdStart_Click(...) Handles ...
tim1.Enabled = True
End Sub
Private Sub cmdStop_Click(...) Handles ...
tim1.Enabled = False
End Sub
Private Sub tim1_Tick(...
) Handles tim1.Tick
If optHakenAus.Checked Then
If h.Location.Y + h.Height + 5 <
f.Location.Y Then
h.Height = h.Height + 10
Else
tim1.Enabled = False
End If
ElseIf optHakenEin.Checked Then
If h.Height > 15 Then
h.Height = h.Height – 10
Else
tim1.Enabled = False
End If
ElseIf optAuslegerAus.Checked Then
If a.Location.X > 15 Then
a.Width = a.Width + 10
a.Location = New Point(
a.Location.X – 10, a.Location.Y)
h.Location = New Point(
h.Location.X – 10, h.Location.Y)
Else
tim1.Enabled = False
End If
ElseIf optAuslegerEin.Checked Then
If a.Width > 25 Then
a.Width = a.Width – 10
a.Location = New Point(
a.Location.X + 10, a.Location.Y)
h.Location = New Point(
h.Location.X + 10, h.Location.Y)
Else
tim1.Enabled = False
End If
ElseIf optKranRechts.Checked Then
If f.Location.X < 215 Then
f.Location = New Point(
f.Location.X + 10, f.Location.Y)
s.Location = New Point(
s.Location.X + 10, s.Location.Y)
a.Location = New Point(
a.Location.X + 10, a.Location.Y)
h.Location = New Point(
h.Location.X + 10, h.Location.Y)
Else
tim1.Enabled = False
End If
ElseIf optKranLinks.Checked Then
If f.Location.X > 15 And
a.Location.X > 15 Then
f.Location = New Point(
f.Location.X – 10, f.Location.Y)
s.Location = New Point(
s.Location.X – 10, s.Location.Y)
a.Location = New Point(
a.Location.X – 10, a.Location.Y)
h.Location = New Point(
h.Location.X – 10, h.Location.Y)
Else
tim1.Enabled = False
End If
ElseIf optKranAus.Checked Then
If s.Location.Y > 15 Then
s.Height = s.Height + 10
s.Location = New Point(
s.Location.X, s.Location.Y – 10)
a.Location = New Point(
a.Location.X, a.Location.Y – 10)
h.Location = New Point(
h.Location.X, h.Location.Y – 10)
Else
tim1.Enabled = False
End If
ElseIf optKranEin.Checked Then
If h.Location.Y + h.Height + 5 <
f.Location.Y Then
s.Height = s.Height – 10
s.Location = New Point(
s.Location.X, s.Location.Y + 10)
a.Location = New Point(
a.Location.X, a.Location.Y + 10)
h.Location = New Point(
h.Location.X, h.Location.Y + 10)
Else
tim1.Enabled = False
End If
End If
End Sub
End Class
B.2.12 Lösung ÜForNext1
Public Class Form1
Private Sub cmdAnzeigen_Click(...) Handles ...
Dim d As Double
For d = 35 To 20 Step –2.5
lblA.Text &= d & vbCrLf
Next
End Sub
End Class
B.2.13 Lösung ÜForNext2
Public Class Form1
Private Sub cmdAnzeigen_Click(...) Handles ...
Dim d, summe, zähler, mittelwert As Double
summe = 0
zähler = 0
For d = 35 To 20 Step –2.5
lblA.Text &= d & vbCrLf
summe += d
zähler += 1
Next
mittelwert = summe / zähler
lblA.Text &= vbCrLf &
"Summe: " & summe & vbCrLf &
"Mittelwert: " & mittelwert & vbCrLf
End Sub
End Class
B.2.14 Lösung ÜDoLoop
Public Class Form1
Private Sub cmdHalbieren_Click(...) Handles ...
Dim d As Double
d = Convert.ToDouble(txtEingabe.Text)
lblA.Text = ""
Do While d > 0.001
d = d / 2
lblA.Text &= d & vbCrLf
Loop
End Sub
End Class
B.2.15 Lösung ÜZahlenraten
Public Class Form1
Dim zahl As Integer
Private Sub Form1_Load(...) Handles MyBase.Load
Randomize()
zahl = Rnd() * 100 + 1
End Sub
Private Sub cmdPrüfen_Click(...) Handles ...
Dim eingabe As Integer
eingabe = Convert.ToDouble(txtEingabe.Text)
If eingabe > zahl Then
lblA.Text = "Die Zahl " & eingabe &
" ist zu groß"
ElseIf eingabe < zahl Then
lblA.Text = "Die Zahl " & eingabe &
" ist zu klein"
Else
lblA.Text = eingabe &
" ist die richtige Zahl, gratuliere"
End If
End Sub
End Class
B.2.16 Lösung ÜSteuertabelle
Public Class Form1
Private Sub cmdAnzeigen_Click(...) Handles ...
Dim gehalt As Double
Dim steuersatz As Double
Dim steuerbetrag As Double
Dim netto As Double
For gehalt = 5000 To 35000 Step 3000
If gehalt <= 12000 Then
steuersatz = 12
ElseIf gehalt <= 20000 Then
steuersatz = 15
ElseIf gehalt <= 30000 Then
steuersatz = 20
Else
steuersatz = 25
End If
steuerbetrag = gehalt * steuersatz / 100
netto = gehalt – steuerbetrag
lblA.Text &= gehalt & " €, " &
steuersatz & " %, " &
steuerbetrag & " €, " &
netto & " €" & vbCrLf
Next
End Sub
End Class
B.2.17 Lösung ÜListenfeld
Public Class Form1
Private Sub Form1_Load(...) Handles MyBase.Load
lstLinks.Items.Add("Malta")
lstLinks.Items.Add("Zypern")
lstLinks.Items.Add("Slowenien")
lstLinks.Items.Add("Estland")
lstLinks.Items.Add("Rumänien")
lstRechts.Items.Add("Belgien")
lstRechts.Items.Add("Spanien")
lstRechts.Items.Add("Italien")
lstRechts.Items.Add("Portugal")
lstRechts.Items.Add("Dänemark")
End Sub
Private Sub cmdRechts_Click(...) Handles ...
Dim i As Integer
For i = 0 To lstLinks.SelectedItems.Count – 1
lstRechts.Items.Add(
lstLinks.SelectedItems(i))
Next
For i = lstLinks.SelectedItems.
Count – 1 To 0 Step –1
lstLinks.Items.RemoveAt(
lstLinks.SelectedIndices(i))
Next
End Sub
Private Sub cmdLinks_Click(...) Handles ...
Dim i As Integer
For i = 0 To lstRechts.SelectedItems.Count – 1
lstLinks.Items.Add(
lstRechts.SelectedItems(i))
Next
For i = lstRechts.SelectedItems.
Count – 1 To 0 Step –1
lstRechts.Items.RemoveAt(
lstRechts.SelectedIndices(i))
Next
End Sub
End Class
Ihre Meinung
Wie hat Ihnen das Openbook gefallen? Wir freuen uns immer über Ihre Rückmeldung. Schreiben Sie uns gerne Ihr Feedback als E-Mail an kommunikation@rheinwerk-verlag.de.