VB.Net Tutorial/Statements/With statement

Материал из VB Эксперт
Перейти к: навигация, поиск

Insert statement into With statement

Imports System.Drawing
Imports System.Windows.Forms
Public Class WithForm : Inherits Form
   Public Shared Sub Main()
      Dim frm as New Form
      With frm
         .BackColor = Color.Gray
         .ForeColor = Color.Red
         .Text = "The With Statement"
          Dim fnt As Font = .Font
          MsgBox(fnt.Name)
         .Enabled = True
         .Topmost = True
         .MinimizeBox = False
         .ShowDialog()
      End With
   End Sub
End Class

Nested With statement

Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1 : Inherits Form
   Public Shared Sub Main()
      Dim frm As New Form1()
      Application.Run(frm)
   End Sub
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      With Me
         .BackColor = Color.Gray
         .ForeColor = Color.Red
         .Text = "The With Statement"
         .Enabled = True
         .TopMost = True
         .MinimizeBox = False
         For Each ctrl As Control In .Controls
            With ctrl
               .BackColor = SystemColors.Window
               .AutoSize = False
               .Cursor = Cursors.Hand
            End With
         Next
      End With
   End Sub
End Class

Use With statement with Form object

Imports System.Drawing
Imports System.Windows.Forms
Public Class WithForm : Inherits Form
   Public Shared Sub Main()
      Dim frm as New Form
      With frm
         .BackColor = Color.Gray
         .ForeColor = Color.Red
         .Text = "The With Statement"
         .Enabled = True
         .Topmost = True
         .MinimizeBox = False
         .ShowDialog()
      End With
   End Sub
End Class

Use With statement with OpenFileDialog

Imports System.Windows.Forms
public class OpenFileDialogWith
   public Shared Sub Main
        Dim OpenFileDialog1 As OpenFileDialog = New System.Windows.Forms.OpenFileDialog
        With OpenFileDialog1
            .FileName = "C:\My Documents\Doc1.txt"
            .DefaultExt = ".txt"
            .Filter = "Text Files|*.TXT"
            .Filter = "Bitmaps|*.BMP|GIF Images|*.GIF|" & _
               "JPG Images|*.JPG|All Images|*.BMP;*.GIF;*.JPG"
            .FilterIndex = 2
            .InitialDirectory = "C:\My Documents"
            .ShowReadOnly = True
            .ReadOnlyChecked = True
        End With
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Console.WriteLine(OpenFileDialog1.FileName)
            
        End If

   End Sub
End class