Picturebox change if button is clicked - vb.net

Hi good day guys i would just like to ask if how can I change a picture in a PictureBox if I clicked a button to be specific i have one picture box and one button which is Button1 and I want to change the picture everytime I click the Button1. Thanks in advance :)

In your button1_click event, you could add the following
PictureBox1.Image= Image.FromFile("c:\folder\file.gif")
Replace the path with the image you need.
EDIT
To change it everytime, you need to create a global variable 'counter'.
Add to that value everytime you click.
Also, create an array of pictures.
So everytime you click, you select the string in the array with the index of counter and you set that as the image in the code presented above.
Dim array() As String = {"c:\folder\file1.gif", "c:\folder\file2.gif", "c:\folder\file3.gif"}
PictureBox1.Image= Image.FromFile(array(counter))

You can create a List of Image contained into Somefilepath
Dim images As New List(Of Image)()
images.add(Image.FromFile(Somefilepath))
Dim imageindex as Integer
imageIndex = 0
Now in a separate function you can change your pictures every time the Button1 is clicked
Private Sub Button1_Click(sender As Object, e As EventArgs)
PictureBox1.Image = images(imageIndex)
imageIndex = imageIndex + 1
End Sub

Related

How to create any one dynamic controls with a for loop without using location property and the form should grow automatically

How to create multiple button controls with a for loop without getting the controls overlapped and without using location property in Vb.Net.
I have created 'n' number of vb controls dynamically but the created controls are getting overlapped to each other. When I use location property to each controls all the controls are getting displayed as per the location value.
The real problem is, I'm using a panel of width 300 and height 300, under that I need to display the dynamically created controls. I have figured it out which is tedious work and does take a lot of time. My idea is to find the panel width and height then need to check whether the new control which is getting created has ample of space to fit inside the panel.
I need to know few things,
1) How to display the controls dynamically using for loop without getting overlapped over each other and without using location property.
2) I need the container or the panel to grow as per the number of controls which gets created dynamically.
3) Accessing each controls which got displayed using an ID or educate or explain me any better idea.
I created a new WinForms project and added a Button to the top of the form. I added a FlowLayoutPanel under that and made it narrow enough to fit a single Button widthwise. I set the AutoSize property of the FLP to True and the FlowDirection to TopDown. I then added this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Create the new Button.
Dim btn As New Button
'Add it to the FLP
FlowLayoutPanel1.Controls.Add(btn)
'Get the position of the bottom, left of the Button relative to the form.
Dim pt = PointToClient(btn.PointToScreen(New Point(0, btn.Height)))
'Resize the form to provide clearance below the new Button.
ClientSize = New Size(ClientSize.Width, pt.Y + 10)
End Sub
I then ran the project and started clicking the Button I added. As expected, each click added a new Button to the FLP in a vertical column and the form resized to fit. In order to access such controls in code, you can simply index the Controls collection of the FLP.
try this helps you.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'Do Something
Else
'Do Something else
End If
Panel1.Controls.Clear()
For i As Integer = 0 To 10 Step 1
Dim b15 As New Button
b15.Text = "Test3"
b15.ID = "a" & i
AddHandler b15.Click, AddressOf updateFunc
Panel1.Controls.Add(b15)
Next
End Sub

Show dragged item while dragging vb.net

I'm wanting to show the dragged control while dragging in my form. I have a list of controls (textboxes and a picturebox) which are draggable. I would like to show the dragged control all the time, as long as the control is being dragged, even if the user cannot drop it. I also have an issue with dragging the picture box. It shows a dragged image of the control, however it flickers.
Code for drag feedback for picture box
Private Sub qrCode_GiveFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles QRCodeDrag.GiveFeedback
Dim bitmap As Bitmap
bitmap = New Bitmap(QRCodeDrag.Width, QRCodeDrag.Height)
QRCodeDrag.DrawToBitmap(bitmap, New Rectangle(Point.Empty, bitmap.Size))
'bitmap.MakeTransparent(Color.White)
Dim cursor As New Cursor(bitmap.GetHicon)
Cursor.Current = cursor
End Sub
Any ideas?
I forgot to add
e.UseDefaultCursors = False
This solved the issue of the flicker

On button click fill next picture box

I'm writing a little program in VB and I'm stuck at a point where I want to add a specific image on a button click to a picturebox. The tricky part for me is, that each time I click the button, I want the image (from same location, e.g "C:\Test.jpg") to appear in the next picture box.
I tried to use a variable in the picturebox name and increase it on each click but it kept giving errors (must have used it wrong, obviously).
So to make it more clear:
I click Button1
image from location "C:\Test.jpg" appears in PictureBox1
I click Button1 again
image from location "C:\Test.jpg" appears in PictureBox2
etc.
As you can imagine, I'm not an expert in VB.NET so if you good people have any suggestions, thank you in advance :D
Vahur
Here's another approach using Controls.Find():
Public Class Form1
Private counter As Integer = 0
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
counter = counter + 1
Dim matches() As Control = Me.Controls.Find("PictureBox" & counter, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
Dim pb As PictureBox = DirectCast(matches(0), PictureBox)
Using fs As New System.IO.FileStream("C:\Test.jpg", IO.FileMode.Open)
pb.Image = New Bitmap(Image.FromStream(fs))
End Using
End If
End Sub
End Class
Form level:
Private mPList As New List(of PictureBox)
Private pIndex as Integer = 0
Form Shown:
With mPlist
.Add(PictureBox1)
.Add(PictureBox2)
... continue as needed
Ens With
Button Click
' remove image from last picbox (????)
mPlist(pIndex).Image = Nothing
pIndex += 1
if pIndex > mPlist.Count-1 then pIndex = 0
mPlist(pIndex).Image.FromFile(filename)
The issue, I'm sure, is that you are trying to figure out how to increment a number and get a handle of the picture box to set the picture on. I am going to make a couple assumptions here, in that you are making a card game and you will have a set number of players. You have a couple options:
For the number, you can either set a PRIVATE variable in your form for holding the next number and on each button click, increment it by 1.
Private Player1NextImage as byte = 1
Private Player2NextImage as byte = 1
Private Player3NextImage as byte = 1
Private Player4NextImage as byte = 1
In the button click, for the specific player, you would want to get their picturebox that is next:
You can either keep the list of pictureboxes in a list, as show by #Plutonix, or you can just use a SELECT CASE statement and hardcode the pictureboxes.
Select Case Player1NextImage
Case 1
Player1Card1PictureBox.Image = ...
Case 2
Player1Card2PictureBox.Image = ...
Etc...
'Then increment the number
Player1NextImage += 1
There are multiple ways to do this. Let's say your variable is called Counter, and (assuming) that the parent of your pictureboxes is the main form / control, add this to your button click event:
Counter += 1
Dim Pic As PictureBox = CType(Me.Controls("PictureBox" & Counter.ToString), PictureBox)
If Pic Is Nothing Then
MsgBox("Could not find PictureBox")
Else
Pic.Image = Image.FromFile("C:\Test.jpg")
End If

Convert a string to

For example i have a program with 10 buttons and when i press a random one its name is saved to a string and i want to add 1 to the button's name for example if i pressed button1 ill alter the string to say button2 but now i cant use that string because it cant convert string to system.windows.forms.buttons, i have already tried the Me.Controls bu it didnt work for me.
Example:
dim stringy as string
dim integr as integer
dim buton as button
sub procedureee
stringy = stringy.remove(0,6)
integr = val(stringy) + 1
stringy = "Button" & integr
button.backcolor = white
end sub
Button1_Click
stringy = button1
procedureee
/* EDIT */
Im sorry i think i didnt make my sefl clear, everything in this code works for me except "stringy = button1" it says that string cannot be converted to system.windows.forms.button but thats exactly what i want to do, i have a program with 100 buttons and when any button is pressed it sets the value of ("Dim local as button") the variable local= the button pressed, and it works so i take that button.name and remove 1 from it so i get the value of the button above(PS: i have the buttons on a grid and vertically its from 1 to 10 and if i remove 1 il get the name of the button abore ex: button1gA3 becomes Button1gA2) but when i try to do this local2 = stringy it gives me that message(string cannot be converted to system.windows.forms.button) anyone knows how to solve this?
Thanks.
Here you go ...
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.click
Dim FullButtonName As String = sender.text
Dim ButtonsNumber As String = FullButtonName.Replace("Button", "").Trim
Dim NewButtonNumber As Integer = CType(ButtonsNumber, Integer) + 1
sender.text = "Button " & NewButtonNumber.ToString
End Sub
You can use FindControl() to find a control by name. Note that it is not recursive, and so you'll need to call this method on the direct parent that contains your buttons.

How to change only one tooltip?

I am populating a FlowLayout with Pictureboxes. As I populate i give each of them a tooltip. I have a seperate function to change the pictures how can I change the tooltip as well?
dim laytt as tooltip = new tooltip
For i = 1 To count
Dim newPic As PictureBox = New PictureBox()
newPic.Image = p.Image
newPic.Size = p.Size
newPic.SizeMode = p.SizeMode
laytt.SetToolTip(newPic, ttstring)
AddHandler newPic.Click, AddressOf LayoutComponent_Clicked
sys.Add(a_component)
LayoutFlowLayout.Controls.Add(newPic)
Next
later i have a function to change the pics in it I want to be able to change the tool tip
Private Sub LayoutComponent_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = LayoutFlowLayout.Controls.IndexOf(sender)
If deleteModeOn Then
sys.components.RemoveAt(i)
LayoutFlowLayout.Controls.RemoveAt(i)
Exit Sub
End If
'get index in sys from layout?
If (sys.components.Item(i).GetType() = GetType(Transpositor)) Then
Form2.ShowDialog(Me)
sys.components.Item(i).divert = tempTranspositorDivert
'here I want to do something like this
laytt.RemoveAt(i) <--- THIS DOESN'T EXIST
End If
End Sub
TL;DR I want to remove/change only one tooltip text at a specific index
Since the sender parameter is the picture box control that was clicked, you can use that variable to specify which control you want to alter. For instance, this will remove the tool tip:
laytt.SetToolTip(sender, Nothing)
This will change it:
laytt.SetToolTip(sender, "new value")