|
-
Apr 27th, 2018, 09:14 PM
#1
Thread Starter
Fanatic Member
Loading and choosing pictures at runtime
I need some advice on how to do something.
I need to tile a form with several pictures which can be chosen.
The pictures will be loaded from a directory.
Can someone explain (or even help me with a little code) how to do this.
I'm fine to do this myself in the IDE but not at runtime.
Do I load the pictures into buttons or pictureboxes?
And how do I then add code that tells me which picture was chosen?
-
Apr 27th, 2018, 09:49 PM
#2
Re: Loading and choosing pictures at runtime
 Originally Posted by sgrya1
I'm fine to do this myself in the IDE but not at runtime.
Do I load the pictures into buttons or pictureboxes?
If you know how you would do this in the designer them I'm not sure how that question makes sense. Would you use Buttons or PictureBoxes in the designer? There's no reason for it to be different if you're doing it at run time.
 Originally Posted by sgrya1
And how do I then add code that tells me which picture was chosen?
Again, the same way you would do it if you were setting it up in the designer. You would handle the appropriate event and appropriate code to the handler. If you're using one method to handle events for multiple objects, which you will in this case but you can certainly do with designer-generated controls too, then you use the 'sender' parameter of the event handler, which is always a reference to the object that raised the event.
I would suggest that you take a look at some designer-generated code to understand that it's pretty much lust like code you write yourself. In the Solution Explorer, click the Show All Files button and then expand the node for a form. You'll see an item for the designer code file listed, so double-click that to open it. There you'll see the code that declares a member variable for each control you added in the designer. You'll also see the code that creates those controls, configures them by setting their properties and then adds them to the Controls collection of their parent.
The only significant differences between that and how you create controls at run time is that you'll use a local variable instead of a field and you'll use an AddHandler statement to register an event handler instead of relying on the field being declared WithEvents and the event handler having a Handles clause. For instance, if you were to add a Button to your form in the designer and handle its Click event, you'd end up with designer code like this:
Code:
'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.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As Button
and user code like this:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
If you were going to do it yourself, it would look like this:
Code:
Dim btn As New Button
'Set properties here
AddHandler btn.Click, AddressOf ButtonClicked
Controls.Add(btn)
Code:
Private Sub ButtonClicked(sender As Object, e As EventArgs)
End Sub
As you can see, the code has essentially the same elements.
As I said, if you want to handle events for multiple objects with the same method, you access the object that raised the event using the 'sender' parameter, e.g.
Code:
Private Sub ButtonClicked(sender As Object, e As EventArgs)
Dim clickedButton = DirectCast(sender, Button)
MessageBox.Show($"You clicked the '{clickedButton.Text}' button.)
End Sub
If you want to add an unknown number of controls at run time, I'd suggest that you add a TableLayoutPanel or FlowLayoutPanel to your form and then add the controls to that. That will automate the layout for you, so you don't have to calculate exact positions for each control.
If you are going to use PictureBoxes, you can set the Image property if you have Image objects or, if you have file paths, you can set the ImageLocation property or call the Load method.
-
Apr 28th, 2018, 05:13 PM
#3
Thread Starter
Fanatic Member
Re: Loading and choosing pictures at runtime
Thankyou again. That really helped.
That really wasn't so difficult in the end.
This is what I ended up with if it helps anyone.
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Row As Integer = 1, Column As Integer = 1
Dim ShapeList As New List(Of KeyValuePair(Of String, Bitmap))
ShapeList.Add(New KeyValuePair(Of String, Bitmap)("00", My.Resources._00))
ShapeList.Add(New KeyValuePair(Of String, Bitmap)("01", My.Resources._01))
For Each Shape In ShapeList
If Column > 5 Then
Column = 1
Row += 1
End If
Dim btn As New Button
btn.Tag = Shape.Key
btn.BackgroundImage = Shape.Value
btn.Height = 125
btn.Width = 250
btn.Left = 50 + btn.Width * Column
btn.Top = 50 + btn.Height * Row
btn.BackgroundImageLayout = ImageLayout.Stretch
AddHandler btn.Click, AddressOf ButtonClicked
Controls.Add(btn)
Column += 1
Next
End Sub
Private Sub ButtonClicked(sender As Object, e As EventArgs)
Dim clickedButton = DirectCast(sender, Button)
MessageBox.Show($"You clicked the '{clickedButton.Tag}' button.")
End Sub
Last edited by sgrya1; Apr 28th, 2018 at 06:03 PM.
-
Apr 28th, 2018, 08:31 PM
#4
Re: Loading and choosing pictures at runtime
You really ought to use a TableLayoutPanel if you want the controls displayed in rows and columns.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|