VB.Net Tutorial/GUI/ComboBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:57, 26 мая 2010
Содержание
- 1 Add value to ComboBox
- 2 ComboBox Cell renderer
- 3 ComboBox Selected Index Changed event
- 4 ComboBox selection event and Add items
- 5 Get Text from ComboBox
- 6 HatchBrush Illustration by using ComboBox
- 7 Set text for ComboBox
- 8 Use ComboBox to control TextBox font color
- 9 Using ComboBox to select shape to draw
Add value to ComboBox
Imports System.Windows.Forms
public class ComboBoxAddSelection
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ruponentModel.IContainer
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
"
"ComboBox1
"
Me.ruboBox1.Items.AddRange(New Object() {"AAAAA", "BBBBB", "CCCCC"})
Me.ruboBox1.Location = New System.Drawing.Point(48, 16)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(144, 21)
Me.ruboBox1.TabIndex = 0
Me.ruboBox1.Text = "Select"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(24, 64)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(144, 64)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 24)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Add"
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(40, 120)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(168, 24)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Select"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(264, 182)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.ruboBox1)
Me.ResumeLayout(False)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedIndex
Case 0
Label1.Text = "A"
Case 1
Label1.Text = "B"
Case 2
Label1.Text = "C"
End Select
End Sub
End Class
ComboBox Cell renderer
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.XButton = New System.Windows.Forms.Button
Me.ColorList = New System.Windows.Forms.ruboBox
Me.SuspendLayout()
"
"XButton
"
Me.XButton.Location = New System.Drawing.Point(112, 24)
Me.XButton.Name = "XButton"
Me.XButton.Size = New System.Drawing.Size(75, 23)
Me.XButton.TabIndex = 0
Me.XButton.Text = "Button1"
Me.XButton.UseVisualStyleBackColor = True
"
"ColorList
"
Me.ColorList.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
Me.ColorList.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList
Me.ColorList.FormattingEnabled = True
Me.ColorList.Location = New System.Drawing.Point(88, 64)
Me.ColorList.Name = "ColorList"
Me.ColorList.Size = New System.Drawing.Size(121, 21)
Me.ColorList.TabIndex = 1
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 221)
Me.Controls.Add(Me.ColorList)
Me.Controls.Add(Me.XButton)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "Control Drawing"
Me.ResumeLayout(False)
End Sub
Friend WithEvents XButton As System.Windows.Forms.Button
Friend WithEvents ColorList As System.Windows.Forms.ruboBox
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ColorList.Items.Add("Red")
ColorList.Items.Add("Orange")
ColorList.Items.Add("Yellow")
ColorList.Items.Add("Green")
ColorList.Items.Add("Blue")
ColorList.Items.Add("Indigo")
ColorList.Items.Add("Violet")
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawEllipse(Pens.Black, 10, 10, Me.ClientRectangle.Width - 20, _
Me.ClientRectangle.Height - 20)
End Sub
Private Sub XButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles XButton.Paint
Dim usePen As Pen
e.Graphics.Clear(SystemColors.Control)
usePen = New Pen(SystemColors.ControlText, 3)
e.Graphics.DrawRectangle(usePen, XButton.ClientRectangle)
usePen.Dispose()
End Sub
Private Sub ColorList_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ColorList.DrawItem
Dim useBrush As Brush
If (e.Index = -1) Then Return
e.DrawBackground()
useBrush = New SolidBrush(Color.FromName(CStr(ColorList.Items(e.Index))))
e.Graphics.FillRectangle(useBrush, _
e.Bounds.Left + 2, e.Bounds.Top + 2, _
e.Bounds.Width - 4, e.Bounds.Height - 4)
useBrush.Dispose()
e.Graphics.DrawRectangle(Pens.Black, _
e.Bounds.Left + 2, e.Bounds.Top + 2, _
e.Bounds.Width - 4, e.Bounds.Height - 4)
e.DrawFocusRectangle()
End Sub
Private Sub XButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XButton.Click
MsgBox("Button clicked.")
End Sub
End Class
public class ComboBoxCellRenderer
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
ComboBox Selected Index Changed event
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ComboBoxSelectionEventAddValue
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
End Sub
"Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
"
"ComboBox1
"
Me.ruboBox1.Location = New System.Drawing.Point(72, 72)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(121, 21)
Me.ruboBox1.TabIndex = 0
Me.ruboBox1.Text = "ComboBox1"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(16, 168)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(256, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.ruboBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
ComboBox1.Items.Add("Item " + i.ToString())
Next
ComboBox1.Text = "Choose one..."
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim intSelectedIndex As Integer
intSelectedIndex = ComboBox1.SelectedIndex
Dim objSelectedItem As Object
objSelectedItem = ComboBox1.SelectedItem
TextBox1.Text = "Item"s index: " & intSelectedIndex & _
". Item"s text: " & objSelectedItem.ToString()
End Sub
End Class
ComboBox selection event and Add items
Imports System.Windows.Forms
public class ComboBoxAddSelection
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ruponentModel.IContainer
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
"
"ComboBox1
"
Me.ruboBox1.Items.AddRange(New Object() {"AAAAA", "BBBBB", "CCCCC"})
Me.ruboBox1.Location = New System.Drawing.Point(48, 16)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(144, 21)
Me.ruboBox1.TabIndex = 0
Me.ruboBox1.Text = "Select"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(24, 64)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(144, 64)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 24)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Add"
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(40, 120)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(168, 24)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Select"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(264, 182)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.ruboBox1)
Me.ResumeLayout(False)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedIndex
Case 0
Label1.Text = "A"
Case 1
Label1.Text = "B"
Case 2
Label1.Text = "C"
End Select
End Sub
End Class
Get Text from ComboBox
Imports System.Windows.Forms
public class GetComboBoxText
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim Language As String
Language = ComboBox1.Text
If Language = "Visual Basic" Then
MsgBox("We have a winner!")
Else
MsgBox(Language & " is not a bad language.")
End If
End Sub
End Class
Partial Public Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.SuspendLayout()
"
"Label1
"
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(13, 22)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(160, 14)
Me.Label1.TabIndex = 0
Me.Label1.Text = "What"s your favorite language?"
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(13, 70)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(267, 23)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Evaluate my choice"
"
"ComboBox1
"
Me.ruboBox1.FormattingEnabled = True
Me.ruboBox1.Items.AddRange(New Object() {"C++", "C#", "Java", "Visual Basic", "Cobol"})
Me.ruboBox1.Location = New System.Drawing.Point(13, 43)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(267, 21)
Me.ruboBox1.TabIndex = 1
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 117)
Me.Controls.Add(Me.ruboBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
End Class
HatchBrush Illustration by using ComboBox
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing.Drawing2D
public class HatchBrushStyleComboBox
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Private style As New HatchStyle
Private forClr As Color = Color.Blue
Private backClr As Color = Color.Red
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
End Sub
"Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ForColorBtn As System.Windows.Forms.Button
Friend WithEvents BackGroundBtn As System.Windows.Forms.Button
Friend WithEvents ApplyBtn As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.Label2 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.ForColorBtn = New System.Windows.Forms.Button
Me.BackGroundBtn = New System.Windows.Forms.Button
Me.ApplyBtn = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Select Style:"
"
"ComboBox1
"
Me.ruboBox1.Location = New System.Drawing.Point(96, 8)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(121, 21)
Me.ruboBox1.TabIndex = 1
Me.ruboBox1.Text = "ComboBox1"
"
"Label2
"
Me.Label2.Location = New System.Drawing.Point(8, 40)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 23)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Forground Color:"
"
"TextBox1
"
Me.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.TextBox1.Location = New System.Drawing.Point(112, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.Size = New System.Drawing.Size(32, 20)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
"
"Label3
"
Me.Label3.Location = New System.Drawing.Point(0, 72)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(104, 23)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Background Color:"
"
"TextBox2
"
Me.TextBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.TextBox2.Location = New System.Drawing.Point(112, 72)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ReadOnly = True
Me.TextBox2.Size = New System.Drawing.Size(32, 20)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = ""
"
"ForColorBtn
"
Me.ForColorBtn.Location = New System.Drawing.Point(152, 40)
Me.ForColorBtn.Name = "ForColorBtn"
Me.ForColorBtn.Size = New System.Drawing.Size(48, 23)
Me.ForColorBtn.TabIndex = 6
Me.ForColorBtn.Text = "Pick..."
"
"BackGroundBtn
"
Me.BackGroundBtn.Location = New System.Drawing.Point(152, 72)
Me.BackGroundBtn.Name = "BackGroundBtn"
Me.BackGroundBtn.Size = New System.Drawing.Size(48, 23)
Me.BackGroundBtn.TabIndex = 7
Me.BackGroundBtn.Text = "Pic..."
"
"ApplyBtn
"
Me.ApplyBtn.Location = New System.Drawing.Point(224, 72)
Me.ApplyBtn.Name = "ApplyBtn"
Me.ApplyBtn.TabIndex = 8
Me.ApplyBtn.Text = "Apply"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 325)
Me.Controls.Add(Me.ApplyBtn)
Me.Controls.Add(Me.BackGroundBtn)
Me.Controls.Add(Me.ForColorBtn)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.ruboBox1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Hatch Brushes"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillHatchStyles()
End Sub
Private Sub FillHatchStyles()
comboBox1.Items.Add(HatchStyle.BackwardDiagonal.ToString())
comboBox1.Items.Add(HatchStyle.Cross.ToString())
comboBox1.Items.Add(HatchStyle.DashedVertical.ToString())
comboBox1.Items.Add(HatchStyle.DiagonalCross.ToString())
comboBox1.Items.Add(HatchStyle.HorizontalBrick.ToString())
comboBox1.Items.Add(HatchStyle.LightDownwardDiagonal.ToString())
comboBox1.Items.Add(HatchStyle.LightUpwardDiagonal.ToString())
comboBox1.Text = HatchStyle.BackwardDiagonal.ToString()
End Sub "FillHatchStyles
Private Sub ApplyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyBtn.Click
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
Dim str As String = ComboBox1.Text
Select Case str
Case "BackwardDiagonal"
style = HatchStyle.BackwardDiagonal
Case "DashedVertical"
style = HatchStyle.DashedVertical
Case "Cross"
style = HatchStyle.Cross
Case "DiagonalCross"
style = HatchStyle.DiagonalCross
Case "HorizontalBrick"
style = HatchStyle.HorizontalBrick
Case "LightDownwardDiagonal"
style = HatchStyle.LightDownwardDiagonal
Case "LightUpwardDiagonal"
style = HatchStyle.LightUpwardDiagonal
Case Else
End Select
Dim brush As New HatchBrush(style, forClr, backClr)
g.FillRectangle(brush, 50, 100, 200, 200)
brush.Dispose()
g.Dispose()
End Sub
End Class
Set text for ComboBox
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ComboBoxSelectionEventAddValue
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
End Sub
"Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
"
"ComboBox1
"
Me.ruboBox1.Location = New System.Drawing.Point(72, 72)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(121, 21)
Me.ruboBox1.TabIndex = 0
Me.ruboBox1.Text = "ComboBox1"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(16, 168)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(256, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.ruboBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
ComboBox1.Items.Add("Item " + i.ToString())
Next
ComboBox1.Text = "Choose one..."
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim intSelectedIndex As Integer
intSelectedIndex = ComboBox1.SelectedIndex
Dim objSelectedItem As Object
objSelectedItem = ComboBox1.SelectedItem
TextBox1.Text = "Item"s index: " & intSelectedIndex & _
". Item"s text: " & objSelectedItem.ToString()
End Sub
End Class
Use ComboBox to control TextBox font color
Imports System.Windows.Forms
public class ComboBoxSelectionEvent
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedIndex
Case 0
TextBox1.ForeColor = System.Drawing.Color.Yellow
Case 1
TextBox1.ForeColor = System.Drawing.Color.Red
Case 2
TextBox1.ForeColor = System.Drawing.Color.Blue
Case 3
TextBox1.ForeColor = System.Drawing.Color.Black
End Select
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
"
"ComboBox1
"
Me.ruboBox1.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList
Me.ruboBox1.FormattingEnabled = True
Me.ruboBox1.Items.AddRange(New Object() {"Yellow", "Red", "Blue", "Black"})
Me.ruboBox1.Location = New System.Drawing.Point(19, 30)
Me.ruboBox1.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(476, 23)
Me.ruboBox1.TabIndex = 0
"
"Label1
"
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(16, 11)
Me.Label1.Margin = New System.Windows.Forms.Padding(4, 0, 4, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(67, 15)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Color"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(16, 62)
Me.TextBox1.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(479, 169)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "www.vbex.ru"
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(513, 242)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ruboBox1)
Me.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class
Using ComboBox to select shape to draw
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawShapes
public Shared Sub Main
Application.Run(New FrmComboBox)
End Sub
End class
Public Class FrmComboBox
Inherits Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
End Sub
"Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
" contains shape list (circle, square, ellipse, pie)
Friend WithEvents cboImage As System.Windows.Forms.ruboBox
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cboImage = New System.Windows.Forms.ruboBox()
Me.SuspendLayout()
"
"cboImage
"
Me.cboImage.DropDownWidth = 121
Me.cboImage.Items.AddRange(New Object() {"Circle", "Square", "Ellipse", "Pie", "Filled Circle", "Filled Square", "Filled Ellipse", "Filled Pie"})
Me.cboImage.Location = New System.Drawing.Point(24, 16)
Me.cboImage.Name = "cboImage"
Me.cboImage.Size = New System.Drawing.Size(121, 21)
Me.cboImage.TabIndex = 0
"
"FrmComboBox
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboImage})
Me.Name = "FrmComboBox"
Me.Text = "ComboBoxTest"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cboImage_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cboImage.SelectedIndexChanged
Dim myGraphics As Graphics = MyBase.CreateGraphics()
Dim myPen As New Pen(Color.DarkRed)
Dim mySolidBrush As New SolidBrush(Color.DarkRed)
myGraphics.Clear(Color.White)
Select Case cboImage.SelectedIndex
Case 0 " case circle is selected
myGraphics.DrawEllipse(myPen, 50, 50, 150, 150)
Case 1
myGraphics.DrawRectangle(myPen, 50, 50, 150, 150)
Case 2
myGraphics.DrawEllipse(myPen, 50, 85, 150, 115)
Case 3
myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45)
Case 4
myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150)
Case 5
myGraphics.FillRectangle(mySolidBrush, 50, 50, 150, 150)
Case 6
myGraphics.FillEllipse(mySolidBrush, 50, 85, 150, 115)
Case 7
myGraphics.FillPie(mySolidBrush, 50, 50, 150, 150, 0, 45)
End Select
End Sub
End Class