Adding and Removing Images

A

Andy Chan

First of all, thanks to Crirus and Fergus for helping me with a related
posted a few days ago.

Ok, I can add images to my form (for whatever reason I cannot added more
than 1 image within the same procedure; rather I had to call the same
"addimage" procedure over and over again to get it working).

For anyone that's curious the code I used is the following:
----------------------------------------------------------------------------
-----------------
Sub dealcards(ByVal value As String, ByVal locX As Integer, ByVal locY As
Integer)

Dim myCard As New PictureBox()

Me.Controls.Add(myCard)

With myCard

myCard = New System.Windows.Forms.PictureBox()

.Visible = True

.Name = CStr(intCardsDealt)

intCardsDealt += 1

.Size = New System.Drawing.Size(70, 90)

.TabStop = False

.Location = New System.Drawing.Point(locX, locY)

.Image = Image.FromFile("c:\blackjack\" & value & ".jpg")

.BringToFront()

.Show()

End With

End Sub

----------------------------------------------------------------------------
----------------------------------------------------------------------

And if you're wondering, yes, I'm creating a blackjack game. (I'm trying to
improve my memory so that I can count cards. If anyone's interested in the
finished product, would be more than happy to share....so long as you don't
expect to win every time!!!)


Anyways, I've tried to surf around for my next problem: how to remove those
images I've added dynamically.

I tried:
Me.Controls.RemoveAt(mintCount) where mintCount is an integer count of the
control's index value.

I've even named the new picture box controls with the cstr(mintCount) value.

Thanks!
 
H

Herfried K. Wagner [MVP]

* "Andy Chan said:
For anyone that's curious the code I used is the following:
----------------------------------------------------------------------------
-----------------
Sub dealcards(ByVal value As String, ByVal locX As Integer, ByVal locY As
Integer)

Dim myCard As New PictureBox()

Me.Controls.Add(myCard)

Why do you add the control _before_ setting its properties?
With myCard

myCard = New System.Windows.Forms.PictureBox()

The line above doesn't make sense because you never add this control.
You can remove it.
.Visible = True

.Name = CStr(intCardsDealt)

intCardsDealt += 1

.Size = New System.Drawing.Size(70, 90)

.TabStop = False

.Location = New System.Drawing.Point(locX, locY)

.Image = Image.FromFile("c:\blackjack\" & value & ".jpg")

.BringToFront()

.Show()

End With

End Sub [...]
Anyways, I've tried to surf around for my next problem: how to remove those
images I've added dynamically.

I tried:
Me.Controls.RemoveAt(mintCount) where mintCount is an integer count of the
control's index value.

I've even named the new picture box controls with the cstr(mintCount) value.

You can add a 'Hashtable' to your form (on top of the form 'Private m_ht
As New Hashtable()') and add all the controls to this table. As key you
can use the name of the control, the value is the control itself.

Later you can easily get a reference to the control with a specific name
by querying it from the Hashtable. Then you can pass this reference to
the 'Me.Controls.Remove' method. After removing the control from the
'Controls' collection, you can remove it from the 'Hashtable'.
 
A

Andy Chan

Oh yes,

Obviously, I'll have many pictureboxes on my form after a while. What I'm
trying to do is iterate through the form.controls collection.

If I use form.remove(picturebox), how would I know which picturebox was
being removed? I've looked at MSDN without luck.
 
A

Andy Chan

OK. Some success.

Couldn't figure out how to remove the image from the form, but I added the
cards to a panel, and removed it from the panel. Tada!!!

I'd still rather just add and remove the cards directly onto the form, so
would still appreciate some guidance. Thanks!
 
C

Crirus

Andy

Consider this:


Public Class Form1
Inherits System.Windows.Forms.Form

Private myPath As String
Private MyCardLocation As Point = New Point(10, 10)
'....some code here

Public Sub SetCard(path as string) 'Handles a Button Click if you want
myPath=path
Form1.Refresh
end sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim myCard As Image = Image.FromFile(myPath)
e.Graphics.DrawImage(myCard, New Rectangle(MyCardLocation.X,
MyCardLocation.Y, myCard.Width, myCard.Height))

End Sub
 
A

Andy Chan

Hi Crirus,

Thanks for the alternative. If I were to use the form1_paint method, how
would I delete the image later?

I'm playing with the picture box in a panel control. It's very easy to use;
I'm leaning towards just using it in the meantime and go one to solve other
issues I have....
 
C

Crirus

It is deleted by itself

The code I posted should work as it is....

Every time a paint is called the form is completly erased and redrawed..so
you dont have to worry about any further deletion, you only have to deal
with what should be draw

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Andy Chan said:
Hi Crirus,

Thanks for the alternative. If I were to use the form1_paint method, how
would I delete the image later?

I'm playing with the picture box in a panel control. It's very easy to use;
I'm leaning towards just using it in the meantime and go one to solve other
issues I have....



Crirus said:
Andy

Consider this:


Public Class Form1
Inherits System.Windows.Forms.Form

Private myPath As String
Private MyCardLocation As Point = New Point(10, 10)
'....some code here

Public Sub SetCard(path as string) 'Handles a Button Click if you want
myPath=path
Form1.Refresh
end sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim myCard As Image = Image.FromFile(myPath)
e.Graphics.DrawImage(myCard, New Rectangle(MyCardLocation.X,
MyCardLocation.Y, myCard.Width, myCard.Height))

End Sub



--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Andy Chan said:
First of all, thanks to Crirus and Fergus for helping me with a related
posted a few days ago.

Ok, I can add images to my form (for whatever reason I cannot added more
than 1 image within the same procedure; rather I had to call the same
"addimage" procedure over and over again to get it working).

For anyone that's curious the code I used is the following:

--------------------------------------------------------------------------
--
-----------------
Sub dealcards(ByVal value As String, ByVal locX As Integer, ByVal locY As
Integer)

Dim myCard As New PictureBox()

Me.Controls.Add(myCard)

With myCard

myCard = New System.Windows.Forms.PictureBox()

.Visible = True

.Name = CStr(intCardsDealt)

intCardsDealt += 1

.Size = New System.Drawing.Size(70, 90)

.TabStop = False

.Location = New System.Drawing.Point(locX, locY)

.Image = Image.FromFile("c:\blackjack\" & value & ".jpg")

.BringToFront()

.Show()

End With

End Sub

-------------------------------------------------------------------------- trying
to
improve my memory so that I can count cards. If anyone's interested in the
finished product, would be more than happy to share....so long as you don't
expect to win every time!!!)


Anyways, I've tried to surf around for my next problem: how to remove those
images I've added dynamically.

I tried:
Me.Controls.RemoveAt(mintCount) where mintCount is an integer count of the
control's index value.

I've even named the new picture box controls with the cstr(mintCount) value.

Thanks!
 
A

Andy Chan

Wow. Thanks! I'll try it now!


Crirus said:
It is deleted by itself

The code I posted should work as it is....

Every time a paint is called the form is completly erased and redrawed..so
you dont have to worry about any further deletion, you only have to deal
with what should be draw

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Andy Chan said:
Hi Crirus,

Thanks for the alternative. If I were to use the form1_paint method, how
would I delete the image later?

I'm playing with the picture box in a panel control. It's very easy to use;
I'm leaning towards just using it in the meantime and go one to solve other
issues I have....



Crirus said:
Andy

Consider this:


Public Class Form1
Inherits System.Windows.Forms.Form

Private myPath As String
Private MyCardLocation As Point = New Point(10, 10)
'....some code here

Public Sub SetCard(path as string) 'Handles a Button Click if you want
myPath=path
Form1.Refresh
end sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim myCard As Image = Image.FromFile(myPath)
e.Graphics.DrawImage(myCard, New Rectangle(MyCardLocation.X,
MyCardLocation.Y, myCard.Width, myCard.Height))

End Sub



--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

First of all, thanks to Crirus and Fergus for helping me with a related
posted a few days ago.

Ok, I can add images to my form (for whatever reason I cannot added more
than 1 image within the same procedure; rather I had to call the same
"addimage" procedure over and over again to get it working).

For anyone that's curious the code I used is the following:

-------------------------------------------------------------------------- locY
As
Integer)

Dim myCard As New PictureBox()

Me.Controls.Add(myCard)

With myCard

myCard = New System.Windows.Forms.PictureBox()

.Visible = True

.Name = CStr(intCardsDealt)

intCardsDealt += 1

.Size = New System.Drawing.Size(70, 90)

.TabStop = False

.Location = New System.Drawing.Point(locX, locY)

.Image = Image.FromFile("c:\blackjack\" & value & ".jpg")

.BringToFront()

.Show()

End With

End Sub

--------------------------------------------------------------------------
--
----------------------------------------------------------------------

And if you're wondering, yes, I'm creating a blackjack game. (I'm trying
to
improve my memory so that I can count cards. If anyone's interested
in
the
finished product, would be more than happy to share....so long as you
don't
expect to win every time!!!)


Anyways, I've tried to surf around for my next problem: how to remove
those
images I've added dynamically.

I tried:
Me.Controls.RemoveAt(mintCount) where mintCount is an integer count
of
the
control's index value.

I've even named the new picture box controls with the cstr(mintCount)
value.

Thanks!
 
C

Crirus

Just make a new test project with a form, no controls and add my code in
it... you may need some changes but you should handle it
 
H

Herfried K. Wagner [MVP]

* "Andy Chan said:
Obviously, I'll have many pictureboxes on my form after a while. What I'm
trying to do is iterate through the form.controls collection.

If I use form.remove(picturebox), how would I know which picturebox was
being removed? I've looked at MSDN without luck.

You can give every picturebox a name by setting its 'Name' property.
Then you can add them to a hashtable:

\\\
Private m_ht As New Hashtable()
..
..
..

' Add a control (use name as key).
m_ht.Add(DynamicPictureBox.Name, DynamicPictureBox)
..
..
..

' Get a control.
Dim p As PictureBox = DirectCast(m_ht.Item("PictureBox1"), PictureBox)

..
..
..

' Remove a control.
m_ht.Remove("PictureBox1")
///
 
F

Fergus Cooney

Hi Andy,

Rather than talking about PictureBoxes and the like, do you think
you could describe what you are doing in terms of how the User is
going to play the game?

Ie. How dealing works and where the cards go on the screen,
how you deal extra cards, etc.

Then we may be able to advise from the big picture rather than
helping you implement what may be an awkward method.

Regards,
Fergus
 
A

Andy Chan

Hi Fergus,

First of all, thanks for taking the time to read my posts. You and Crirus
have been extremely helpful. This is my first VB .net project; it's made me
realize how far I've got to go.

So, this is the "big picture", per say:

UI:

1. I've designed a simple blackjack table, with Panels representing each
player position.
2. Created 52 jpg's for the cards, 1 jpg for "face down".
3. I've grouped together a bunch of face down cards to represent the cards
in the "shoe". As cards are dealt, I'll be hiding these cards to show that
there are fewer cards left in the shoe.

Objects:

1. A deck class will contain a collection of cards
2. a card class will contain the suit, card value.
3. The dealer class will contain the deck collection. I'm creating methods
such as deal, and shuffle.
4. The player class will contain a similiar deck collection, representing
the hand of cards the player has. I'm creating methods such as draw, stand,
split, etc.


Other:
1. I want to add a feature called "Monte Carlo" (ie. Monte Carlo Simulation)
which will draw 1000 times the next card, based on the cards in the deck
collection. I'll then show the probabilities of the value of the hand after
the next card is drawn.
2. I'll show the card count, which represent the number of face cards less
the number of cards 2 to 7, still left in deck.
3. There are some other card counting strategies I'll be showing, such as
"Omega II".


database tier:
1. I plan to add records to a jet database so I can track how I do,
depending on different strategies. I'll build some Crystal Reports to query
the database.

I'm also diagramming with UML in Visio. I've got a background in VB, using
OOM. I'm really more interested in seeing how the new object capabilities
work in .NET as opposed to the graphics manipulation.

So, I've got my work to do. Again, really appreciate you guys helping me
with the initial hurdles. By the way, I got interested in programming this
for 2 reasons:
1. get through the Vb.Net learning curve, and
2. I read this book called "Bringing down the house". Very light read. It's
about a bunch of MIT students who won money playing blackjack. I certainly
don't think I'll be rich following their example, but I decided it'd make
the perfect project for me to learn the UI, objects, Crystal Reports, ADO of
the new VB.

Phew. That's it.


Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top