Anwendungseinstellungen in My.Settings speichern

  1. Generell
    • bietet die Möglichkeit Anwendungseinstellungen anstatt in ini-Dateien direkt in der Anwendung zu speichern
  2. Anlegen/Editieren
    • Menü->Projekt->MeinProjektname-Eigenschaften… oder im Projektbaum->MeinProjektname->My Project->Settings.settings
    • Name: Propertyname
    • Typ: Propertytyp (String, Bool …)
    • Bereich: Benutzer
    • Wert: voreingestellter Wert
  3. Zugriff im Code
  4. ' Settings-Objekt erzeugen
    Private HCSettings As New MeinProjektname.My.MySettings
    
    Dim sText As String = HCSettings("Propertyname")
    HCSettings("Propertyname") = "Wert"
    
    ' im Projekt speichern
    HCSettings.Save()
    

Cast-Funktionen

TryCast(QuellObjekt, ZielTyp)

  • Try … Catch ist implizit
  • löst keine Exception aus
  • gibt ‘Nothing’ zurück, falls der Cast nicht möglich war
FMonthData = TryCast(frmMain.MdiChildren(i), frmMonth)

If Not FMonthData Is Nothing Then
  ...
End If

DirectCast(QuellObjekt, ZielTyp)

  • muß evtl. in Try … Catch
  • löst bei Fehler eine Exception aus
Private Sub tbQ_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbQ.KeyPress, tbV.KeyPress
   Try
      Dim tbSender As System.Windows.Forms.TextBox = DirectCast(sender, System.Windows.Forms.TextBox)
      ...
   Catch ex As Exception
      MessageBox.Show("Fehler: " & ControlChars.CrLf & ex.ToString())
   End Try
End Sub

Typ-Konvertierungen

CType

  • wandelt einen Typ in einen angegebenen anderen Typ um
  • ' wandelt die Zahl 123.45 in einen String um
    Dim n As Decimal = 123.45
    Dim s As String
    s = CType(n, String)
    

spezielle Typumwandlungen, welche immer einen übergebenes Objekt in den festgelegten Typ wandeln

  • CBool – Bool
  • Dim bBoolean As Boolean
    bBoolean = CBool(0) ' False
    bBoolean = CBool(1) ' True
    
  • CByte – Byte
  • CChar – Char
  • CDate – Datumsangaben
  • CDbl – Double
  • Dim i1 As Integer = 56
    Dim d As Double = CDbl(i1) / 3.14
    
  • CDec – Decimal
  • Dim i1 As Integer = 5000
    Dim d As Decimal = CDec(i1) / 34)
    
  • CInt – Integer
  • CLng – Long
  • Dim i1 As Integer = 1005
    Dim i2 As Integer = 2100
    Dim l As Long = CLng(i1) * CLng(i2)
    
  • CObj – Object
  • CSByte – SByte
  • CShort – Short
  • CSng – Single
  • Dim i1 As Integer = 56
    Dim z As Single = CSng(i1) / 3.4
    
  • CStr – String
  • CUInt – unsigned Integer
  • CULng – unsigned Long
  • CUShort – unsigned Short

Wichtige Zeichen für Stringoperationen

' Dezimaltrennzeichen
Public DecimalSeparator As String = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
' Gruppentrennzeichen
Public GroupSeparator As String = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator
' Negativzeichen
Public NegativeSign As String = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NegativeSign

String zu Typ wandeln (Parse, TryParse)

Dim i As Integer = Integer.Parse("100") 
Dim l As Long = Long.Parse("100") 
Dim f As Single = Single.Parse("100.1") 
Dim d As Double = Double.Parse("100.1") 
Dim b As Boolean = Boolean.Parse("True")

Dim dValue As Double

If (Double.TryParse("0,45", dValue) Then
    ...
End If

Zeitspanne messen

Dim start_time As DateTime
Dim stop_time As DateTime
Dim elapsed_time As TimeSpan

start_time = Now

Funktionsaufruf()

stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine(elapsed_time.TotalSeconds().ToString)

dynamisches Array

Private Function GetMyArray() As SomeClass()
     Dim MyArray As New ArrayList
     Dim CurrentItem As SomeClass ' hat Eigenschaften ID, Name

     For i As Integer = 0 To MyData.Count - 1
          CurrentItem = New SomeClass
          With CurrentItem
               .ID = i
               .Name = "blah " & i.ToString
          End With

          MyArray.Add(CurrentItem)
     Next

     Return DirectCast(MyArray.ToArray(GetType(SomeClass)), SomeClass())
End Function

Text vertikal in einer Grafik ausgeben

Dim fntScaleFont As New System.Drawing.Font("Arial", 10, FontStyle.Regular)

Dim layout_rect As New RectangleF(0, 0, 18, 64)

Dim string_format As New StringFormat

string_format.Alignment = StringAlignment.Far
string_format.LineAlignment = StringAlignment.Center
string_format.FormatFlags = StringFormatFlags.DirectionVertical
        
m_Graphics.DrawString("Teststring", fntScaleFont, Brushes.Black, layout_rect, string_format)