The content of the codes given below;
3 panels and 2 Splitter are required. However, the 2nd splitter (Green) must be located between the gray and brown panel. But not in the right place. Are there any suggestions?
Thanks.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Show()
Dim tmpSplitter As Splitter
Dim tmpPanel As Panel
Dim MainPanel As Panel
' This is main panel
'
MainPanel = New Panel
With MainPanel
.Name = "MainPanel"
.Dock = DockStyle.Fill
.BackColor = Color.LightGray
.BorderStyle = BorderStyle.Fixed3D
.Visible = True
End With
Me.Controls.Add(MainPanel)
' 1: First Top Panel and Splitter
'
tmpPanel = New Panel
With tmpPanel
.Name = "Panel1"
.Dock = DockStyle.Top
.BackColor = Color.Red
.Visible = True
End With
tmpSplitter = New Splitter
With tmpSplitter
.Name = "Split1"
.Dock = DockStyle.Top
.BackColor = Color.Blue
.Cursor = Cursors.HSplit
End With
Me.Controls.Add(tmpSplitter)
Me.Controls.Add(tmpPanel)
' 2: Second Panel added to the left side of the main panel
'
MainPanel.Dock = DockStyle.Right
MainPanel.Size = New Size(MainPanel.Width * 0.5, MainPanel.Height)
Dim btn As New Button
btn.Size = New Size(10, 50)
btn.Location = New Point(MainPanel.Location.X - btn.Width, MainPanel.Location.Y)
Me.Controls.Add(btn)
tmpPanel = New Panel
With tmpPanel
.Size = New Size(10, MainPanel.Height)
.Location = New Point(MainPanel.Location.X - .Width - 5, MainPanel.Location.Y)
.Name = "Panel2"
.Dock = DockStyle.Fill
.BackColor = Color.Brown
End With
' THIS SPLITTER IS NOT IN THE RIGHT PLACE. Must be between brown and gray panel
'
tmpSplitter = New Splitter
With tmpSplitter
.Size = New Size(5, MainPanel.Height)
.Location = New Point(MainPanel.Location.X - .Width, MainPanel.Location.Y)
.Name = "Split2"
.Dock = DockStyle.Right
.BackColor = Color.Green
.Cursor = Cursors.VSplit
End With
Me.Controls.Add(tmpSplitter)
Me.Controls.Add(tmpPanel)
End Sub
End Class
a little more research and try to solve the solution
Me.Controls.SetChildIndex (tmpSplitter, 0)
is enough to write on the last line. Thank you for your contributions.
Related
https://imgur.com/a/gTuKRnf
Video example in link above. (Short, 30 seconds)
listBox works fine until scrolling is needed.
When scrolling, listBox changes background, turns black, glitches out.
Have attempted OwnerDrawFixed mode but then listBox shows empty.
Has anyone else seen this before?
After Scroll:
After drag select/click:
Code Snippets:
[Form1 Designer.vb]
<Global.Microsoft.VisualBasic.CompilerServices.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)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.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.components = New System.ComponentModel.Container()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.PictureBox2 = New System.Windows.Forms.PictureBox()
Me.PictureBox3 = New System.Windows.Forms.PictureBox()
Me.PictureBox4 = New System.Windows.Forms.PictureBox()
Me.LinkLabel1 = New System.Windows.Forms.LinkLabel()
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
Me.Button2 = New System.Windows.Forms.Button()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 200
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Courier New", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.TextBox1.Location = New System.Drawing.Point(12, 77)
Me.TextBox1.Margin = New System.Windows.Forms.Padding(6, 5, 6, 5)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(293, 31)
Me.TextBox1.TabIndex = 0
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Courier New", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.Button1.Location = New System.Drawing.Point(153, 118)
Me.Button1.Margin = New System.Windows.Forms.Padding(6, 5, 6, 5)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(192, 35)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'PictureBox1
'
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(12, 12)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(44, 44)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox1.TabIndex = 2
Me.PictureBox1.TabStop = False
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Courier New", 15.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.Label1.Location = New System.Drawing.Point(62, 23)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(283, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "My App"
'
'ListBox1
'
Me.ListBox1.Font = New System.Drawing.Font("Courier New", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.ItemHeight = 16
Me.ListBox1.Location = New System.Drawing.Point(12, 161)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(333, 180)
Me.ListBox1.TabIndex = 4
'
'OpenFileDialog1
'
Me.OpenFileDialog1.AddExtension = False
Me.OpenFileDialog1.FileName = "OpenFileDialog1"
Me.OpenFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
Me.OpenFileDialog1.FilterIndex = 2
Me.OpenFileDialog1.RestoreDirectory = True
'
'PictureBox2
'
Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(314, 77)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(31, 31)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox2.TabIndex = 5
Me.PictureBox2.TabStop = False
'
'PictureBox3
'
Me.PictureBox3.Image = CType(resources.GetObject("PictureBox3.Image"), System.Drawing.Image)
Me.PictureBox3.Location = New System.Drawing.Point(12, 118)
Me.PictureBox3.Name = "PictureBox3"
Me.PictureBox3.Size = New System.Drawing.Size(31, 31)
Me.PictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox3.TabIndex = 6
Me.PictureBox3.TabStop = False
'
'PictureBox4
'
Me.PictureBox4.Image = CType(resources.GetObject("PictureBox4.Image"), System.Drawing.Image)
Me.PictureBox4.Location = New System.Drawing.Point(314, 355)
Me.PictureBox4.Name = "PictureBox4"
Me.PictureBox4.Size = New System.Drawing.Size(31, 31)
Me.PictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox4.TabIndex = 7
Me.PictureBox4.TabStop = False
'
'LinkLabel1
'
Me.LinkLabel1.AutoSize = True
Me.LinkLabel1.Font = New System.Drawing.Font("Courier New", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.LinkLabel1.LinkColor = System.Drawing.Color.Black
Me.LinkLabel1.Location = New System.Drawing.Point(12, 373)
Me.LinkLabel1.Name = "LinkLabel1"
Me.LinkLabel1.Size = New System.Drawing.Size(126, 15)
Me.LinkLabel1.TabIndex = 8
Me.LinkLabel1.TabStop = True
Me.LinkLabel1.Text = "label"
Me.LinkLabel1.VisitedLinkColor = System.Drawing.Color.Black
'
'NotifyIcon1
'
Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
Me.NotifyIcon1.Text = "My App"
Me.NotifyIcon1.Visible = True
'
'Button2
'
Me.Button2.Font = New System.Drawing.Font("Courier New", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.Button2.Location = New System.Drawing.Point(153, 355)
Me.Button2.Margin = New System.Windows.Forms.Padding(6, 5, 6, 5)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(152, 31)
Me.Button2.TabIndex = 9
Me.Button2.Text = "Button"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(13.0!, 23.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(357, 397)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.LinkLabel1)
Me.Controls.Add(Me.PictureBox4)
Me.Controls.Add(Me.PictureBox3)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Font = New System.Drawing.Font("Courier New", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "My App"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Timer1 As Timer
Friend WithEvents TextBox1 As TextBox
Friend WithEvents Button1 As Button
Friend WithEvents PictureBox1 As PictureBox
Friend WithEvents Label1 As Label
Friend WithEvents ListBox1 As ListBox
Friend WithEvents OpenFileDialog1 As OpenFileDialog
Friend WithEvents PictureBox2 As PictureBox
Friend WithEvents PictureBox3 As PictureBox
Friend WithEvents PictureBox4 As PictureBox
Friend WithEvents LinkLabel1 As LinkLabel
Friend WithEvents NotifyIcon1 As NotifyIcon
Friend WithEvents Button2 As Button
End Class
[Form1.vb]
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If System.IO.File.Exists("myfile.txt") Then
Dim lines = File.ReadAllLines("myfile.txt")
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lines)
End If
End Sub
Example of 'myfile.txt' contents:
iexplore1
iexplore2
iexplore3
iexplore4
... [And so on]
I defined toolstrip & toolstrip buttons within the form cording. Its working fine. I want to know how to access click event each & every button.
When I'm run the form, ToolStrip enable too. But when I click TooStripButtons it didn't access.
Variable Declaration
Dim tsToolStrip As ToolStrip
Dim tsbNew, _
tsbEdit, _
tsbDelete, _
tsbSave, _
tsbPrint, _
tsbCancel As ToolStripButton
Dim tssSepe01, _
tssSepe02, _
tssSepe03 As ToolStripSeparator
Tool Strip Setup
Sub ToolStripSetup()
' ToolStrip Inizializing
tsToolStrip = New ToolStrip
' ToolStrip Seperators Inizializing
tssSepe01 = New ToolStripSeparator
tssSepe02 = New ToolStripSeparator
tssSepe03 = New ToolStripSeparator
' ToolStrip Buttons Inizializing
tsbNew = New ToolStripButton
tsbEdit = New ToolStripButton
tsbDelete = New ToolStripButton
tsbSave = New ToolStripButton
tsbPrint = New ToolStripButton
tsbCancel = New ToolStripButton
With tsbNew
.Name = "tsbNew"
.Image = My.Resources.NewFile_16x
.Text = "&New"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbEdit
.Name = "tsbEdit"
.Image = My.Resources.Edit_16x
.Text = "&Edit"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbDelete
.Name = "tsbDelete"
.Image = My.Resources.DeleteDatabase_16x
.Text = "&Delete"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbSave
.Name = "tsbSave"
.Image = My.Resources.Save_16x
.Text = "&Save"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbPrint
.Name = "tsbPrint"
.Image = My.Resources.Print_16x
.Text = "&Print"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbCancel
.Name = "tsbCancel"
.Image = My.Resources.action_Cancel_16xLG
.Text = "&Cancel"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
End With
' Adding Buttons To ToolStrip
With tsToolStrip
.Name = "tsToolStrip"
.GripStyle = ToolStripGripStyle.Hidden
.RenderMode = ToolStripRenderMode.System
With .Items
.Add(tsbNew) ' New Button
.Add(tsbEdit) ' Edit Button
.Add(tsbDelete) ' Delete Button
.Add(tssSepe01)
.Add(tsbSave) ' Save Button
.Add(tssSepe02)
.Add(tsbPrint) ' Print Button
.Add(tssSepe03)
.Add(tsbCancel) ' Cancel Button
End With
End With
Controls.Add(tsToolStrip)
End Sub
I above posted my sample code. Can anyone please help me how to do this
Thank For Answer
This is how I done my coding
Dim WithEvents tsbNew, _
tsbEdit, _
tsbDelete, _
tsbSave, _
tsbPrint, _
tsbCancel As ToolStripButton
Private Sub tsbCancel_Click(sender As Object, e As EventArgs) Handles tsbCancel.Click
FormInizalization()
End Sub
I want get value 1 in all line for get path string, and programmatically add cover to flowlayoutpanel.
in Resource/Game List.ini (from drag n drop)
Apex Legends,Resource/Cover/Apex Legends.jpg,Resource/Game Info/Apex Legends.txt
Fortnite,Resource/Cover/Fortnite.jpg,Resource/Game Info/Fortnite.txt
PUBG,Resource/Cover/PUBG.jpg,Resource/Game Info/PUBG.txt
here my code :
Private Sub LabelSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelSetting.Click
FlpAddItem.Controls.Clear()
'I am confused in this part to get value 1 in all line for get path string
'Directory.GetFiles(Path) will be replace with streamreader from lines(i) value 1
Dim Path = '???
Dim ImageX As Image = Nothing
Dim x As Int32 = Directory.GetFiles(Path).Count - 1
Dim Img(x) As PictureBox
Dim ImgText(x) As Label
Dim ImgPanel As Panel
For i = 0 To Directory.GetFiles(Path).Count - 1
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = Directory.GetFiles(Path)(i).Replace(Path, "").Replace(".jpg", "").Replace(".png", "")
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(Directory.GetFiles(Path)(i), True)
Img(i).Image = Image.FromFile(Directory.GetFiles(Path)(i))
ImgText(i).Text = Directory.GetFiles(Path)(i)
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
I suggest creating a class for the game name and path information
Public Class GamePath
Public Property GameName As String
Property Path As String
Public Overrides Function ToString() As String
Return GameName
End Function
End Class
I have overridden ToString, so that the game name will automatically be displayed in a ListBox.
When loading the form, I read this information from the INI-file and set it as data source of a listbox, where you will be able to select a game.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim games =
From line In File.ReadLines(IniFilePath)
Let parts = line.Split(","c)
Select New GamePath With {.GameName = parts(0), .Path = parts(1)}
GameListBox.DataSource = games.ToList()
GameListBox.SelectedIndex = 0 'Select first game
End Sub
Note that it is easier to use File.ReadLines than a StreamReader. You will have to add Imports System.IO to the top of the code. Then we use the LINQ syntax to split each line at the comma and to create the game path information.
The user selects a game in the ListBox and then clicks a button. You can get the file path information from the ListBox like this:
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Then read the files only once and assign the result to a variable
Dim files As String() = Directory.GetFiles(gamePath.Path)
Get the file count
Dim fileCount As Integer = files.Count
The whole Click method:
Private Sub StartGameButton_Click(sender As Object, e As EventArgs) Handles StartGameButton.Click
FlpAddItem.Controls.Clear()
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Dim files As String() = Directory.GetFiles(gamePath.Path)
Dim fileCount As Integer = files.Count
Dim ImageX As Image = Nothing
Dim Img(fileCount) As PictureBox
Dim ImgText(fileCount) As Label
Dim ImgPanel As Panel
For i = 0 To fileCount - 1
Dim filePath = files(i)
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
Some details:
In the For-loop you can get the path of an image file with
Dim filePath = files(i)
You can get the name of the image with
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
This automatically removes the directory name and the extension.
Later on, you don't call Directory.GetFiles again:
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
If you only want to read the file paths into a list, you could write
Dim games =
(From line In File.ReadLines(IniFilePath)
Select line.Split(","c)(1)).ToList()
I'm confused as to why this isn't working...
Here's a brief description of what I've declared my buttons dynamically as the form loads because the form resizes itself depending on whats selected. I've named the buttons button1 and button2. I've added a handler to button2 to handle when the button gets clicked. GetButtonColl is called after the buttons are created and I verified it's populated with the buttons on the form. Why doesn't a simple .enable property work on these dynamically created buttons? Is there something I need to do different?
Here's a condensed version of my code:
Public buttonColl As New List(Of Button)
'---------------------------------------------------------------------------------------------------------------
' Function: GetButtonColl
' Parameters: none
' Returns: none
' Description: Loops through all the controls on the form and searches for buttons that were created after the form has been resized
' and adds them to a List which will be used to quickly control the buttons properties throughout the session
'----------------------------------------------------------------------------------------------------------------
Public Sub GetButtonColl()
For x As Integer = 0 To Me.Controls.Count - 1
Dim b As Control = TryCast(Me.Controls(x), Control)
If b IsNot Nothing AndAlso b.Tag Is Nothing Then
If TypeOf (b) Is Button Then
buttonColl.Add(b)
End If
End If
Next
End Sub
Public Sub button1_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
Try
For Each bttn As Button In buttonColl
If bttn.Name = "button2" Then
bttn.Enabled = False
End If
Next
Catch ex As Exception
End Try
End Sub
Here's how I'm creating my buttons through the code:
btn = New System.Windows.Forms.Button
With btn
.Enabled = true
.Location = New System.Drawing.Point(9, 332)
.Size = New System.Drawing.Size(122, 26)
.Name = "button1"
.Text = "TestButton"
.BackColor = System.Drawing.SystemColors.Control
.Cursor = System.Windows.Forms.Cursors.Default
.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
.ForeColor = System.Drawing.SystemColors.ControlText
.RightToLeft = System.Windows.Forms.RightToLeft.No
.TabIndex = TabIndex
.UseVisualStyleBackColor = False
End With
Me.Controls.Add(btn)
Simplify. The sender object is the button clicked on just cast it back.
Public Sub button1_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "button2" Then
btn.Enabled = False
End If
End Sub
btn = New System.Windows.Forms.Button
With btn
.Enabled = true
.Location = New System.Drawing.Point(9, 332)
.Size = New System.Drawing.Size(122, 26)
.Name = "button2" 'wouldn't this be button2?
.Text = "TestButton"
.BackColor = System.Drawing.SystemColors.Control
.Cursor = System.Windows.Forms.Cursors.Default
.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
.ForeColor = System.Drawing.SystemColors.ControlText
.RightToLeft = System.Windows.Forms.RightToLeft.No
.TabIndex = TabIndex
.UseVisualStyleBackColor = False
Addhandler btn.Click, AddressOf button1_Handler 'add the delegate
End With
Me.Controls.Add(btn)
Also add the Button to the List when you create it, if you need it in a List.
Are the buttons directly contained within the form, or are they inside another panel? Looping through me.controls will not get grandchild controls, it isn't recursive.
Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.
This is the code i have for that button:
Dim intXX As Integer = 0
Do Until intXX = intX
userAvatar(intXX).Enabled = False
intXX = intXX + 1
Loop
The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?
How i create the buttons are like this:
private sub createButton()
Dim personAvatar As New PictureBox
With personAvatar
.AutoSize = False '> ^
If intX = 7 Then
thePrviousAvatarImg = 0
End If
If intX <= 6 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
ElseIf intX >= 7 And intX <= 14 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
Else
Exit Sub
End If
.Name = "cmd" & nameOfPerson
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BackgroundImage = Image.FromFile(theAvatarDir)
.Tag = nameOfPerson
.BringToFront()
End With
AddHandler personAvatar.Click, AddressOf personAvatar_Click
Me.Controls.Add(personAvatar)
userAvatar(intX) = personAvatar
intX = intX + 1
End With
End Sub
Thanks for your time and help!
David
You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.
So before you create each picture box, declare an array to store them:
Dim MyPictureBoxes() as PictureBox
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.enabled = False
Next
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Public Class Form1
Dim MyPictureBoxes(4) As PictureBox
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim personAvatar As New PictureBox
Dim intX As Integer = 0
While intX < 5
personAvatar = New PictureBox
With personAvatar
.AutoSize = False
.Left = intX * 100
.Top = 100
.Name = "cmd" & intX
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BringToFront()
End With
Me.Controls.Add(personAvatar)
MyPictureBoxes(intX) = personAvatar
intX = intX + 1
End While
End Sub
Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.Enabled = False
Next
End Sub
End Class
Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.