How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object?
How do I make a button spawn an object? How do I make a button spawn an object?
How do I make a button spawn an object?
Go Back  Xtreme Visual Basic Talk > > > How do I make a button spawn an object?


Reply
 
Thread Tools Display Modes
  #1  
Old 12-20-2009, 06:33 PM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default How do I make a button spawn an object?


I have a button named cmd_Grunt and a picture box named pb1_Grunt. I want to click the button and have a picture box created.

"Public Class Form1
Dim grunt = (50)

Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
cmdGrunt_Click(CreateObject('pb1Grunt', grunt))
End Sub
End Class"

This is the code I have so far. I get an error message, "Expression expected." It wants the expression between "CreateObject" and "('pb1Grunt', grunt))".

ie cmdGrunt_Click(CreateObject HERE ('pb1Grunt', grunt))

What do I have to do?
Reply With Quote
  #2  
Old 12-20-2009, 07:28 PM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Single quotes mark off comments of code. It could be giving you that error because, to the compiler, it doesn't see anything after the ':
cmdGrunt_Click(CreateObject(

If you are using VB.NET, create a new control by examining the code in the designer and copying the applicable code to your button click procedure
It should look something like:
Dim PB As PictureBox
PB = New PictureBox
set properties - don't forget to make visible.
Me.Controls.Add(PB)
And this is all you should need... no calls to a Click event.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 12-21-2009, 10:19 AM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

Thanks.

Public Class Form1
Dim PB As PictureBox = New PictureBox

Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
cmdGrunt.Controls.Add(pb1Grunt)
End Sub

End Class

This is my code now, but it just gets rid of the picture box thats on the form.
Reply With Quote
  #4  
Old 12-21-2009, 10:33 AM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

What is pb1Grunt?
The new picturebox you created is in PB, so you should add PB to the form.
You should also look at what I posted earlier:
Quote:
set properties - don't forget to make visible.
Me.Controls.Add(PB)
You also shouldn't add controls to the form that are already on the form (such as
pb1Grunt if you have such a control on your form).
Also, it's not a good idea to New PictureBox outside of your subroutines... plus you'll only be able to add one picturebox.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 12-21-2009, 11:41 AM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

Thanks.

Public Class Form1
Dim PB As PictureBox = New PictureBox

Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
cmdGrunt.Controls.Add(pb1Grunt)
End Sub

End Class

This is my code now, but it just gets rid of the picture box thats on the form.
Reply With Quote
  #6  
Old 12-21-2009, 11:48 AM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

Ignore above, I accidentally double posted. How would I know if it works or not?
Reply With Quote
  #7  
Old 12-21-2009, 12:53 PM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

You run it and you see if a control appeared at the bounds that you specified. You did specify left, top, width, and height, right?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #8  
Old 12-22-2009, 08:20 AM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

Public Class Form1
Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
Dim PB As PictureBox
PB = New PictureBox
Me.Controls.Add(PB)
PB.Left = 500
PB.Top = 300
PB.Width = 250
PB.Height = 250
End Sub
End Class

I click the button, but nothing happens.
Reply With Quote
  #9  
Old 12-22-2009, 09:44 AM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Read more carefully:
set properties - don't forget to make visible.
You make it visible by setting the Visible property to True.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #10  
Old 12-22-2009, 05:00 PM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

Public Class Form1
Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
Dim PB As PictureBox
PB = New PictureBox
Me.Controls.Add(PB)
PB.Left = 500
PB.Top = 300
PB.Width = 250
PB.Height = 250
End Sub
End Class

PB is the name of my picture box.

I have the visible property, for both the button (cmdGrunt) and the picture box (PB), set to True.

I am trying to make an RTS. That picture box will eventually become a unit with stats.

That's my code. In case you haven't noticed, I'm fairly new to VB, ie a month and a bit. I used QBasic for four months beforehand as well. However, I am highly motivated to learn. I am not just copying and pasting, I am learning what it is you're telling me to do.

Thanks again for your help.
Reply With Quote
  #11  
Old 12-22-2009, 05:10 PM
jojoofu jojoofu is offline
Newcomer
 
Join Date: Dec 2009
Posts: 11
Cool

WOAH !!! Not trying to be rude but let me stop you there. If you want to make an RTS using picturebox to draw images is not the way to go. For one RTS can have hundreds of units active at once this would require you to have hundreds of picture boxes floating around. Which would cause mass problems when it comes to FPS(frames per second) and visibility.
You should use the graphics class to draw images. Here is some sample pseudo code.

Code:
'create graphics object
        Dim G As Graphics
'create bitmap , size = 500,500
        Dim Bmp As New Bitmap(500, 500)
'set the graphics object to draw on the new bitmap you just created
        G = Graphics.FromImage(Bmp)
'draw an unscaled image (scaling is very process intensive and slows down 
'your application speed
            G.DrawImageUnscaled(Bmp, 0, 0)
'dispose of graphics object

Me.BackGroundImage = Bmp
G.Dispose
The graphics class can make transparent colors and do so much more for you too. There are better ways to use the graphics class too , but this is just to get you started.
Reply With Quote
  #12  
Old 12-22-2009, 05:34 PM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Quote:
I have the visible property, for both the button (cmdGrunt) and the picture box (PB), set to True.
No, you don't. I don't see anywhere in that subroutine that you posted where you set that picturebox's .Visible property to True.
Quote:
PB is the name of my picture box.
... PB is the name of the picturebox variable that you are using to set properties for the picturebox that you just created. If you have another picturebox on your form that is also PB, then you are just confusing yourself in thinking that you are controlling something else when you are not.
Quote:
PB = New PictureBox
When you set PB equal to a new picturebox, think of it as getting a shipment of DVDs in the mail - let's say you just brought a bunch of Action Movies. It comes in a box. You know there are DVDs there, but you can't see them.
So, you need to put them somewhere on your DVD rack.
Let's say:
Code:
PB.Left = 500
PB.Top = 300
PB.Width = 250
PB.Height = 250
You clear out that area in your DVD shelf... but the DVDs are still in the box laying outside.
Code:
Me.Controls.Add(PB)
Now, you've picked up the box of DVDs and put them on the shelf... but they are still in the box. You still can't see the DVDs, because they are in the box. You want to make your DVDs visible, so, you need to
PB.Visible = True
set the visible property to True.
Now, you've got more DVDs on your shelf i.e. you've got another control on your form.
These are the things you need to do everytime you want to dynamically create a control:
Position, Visible, then add to the form.

Saying that you've got PB as a control on your form is like looking at the box of DVDs on the ground which says "Action Movies" and saying "Oh... I already have Action Movies in my DVD rack."
It doesn't matter. A: That's a different control than you're thinking of (just like those are different action movies you are thinking of). B: What you do to PB doesn't affect what you did with the control on your form (i.e. putting the box of action movies on your form doesn't cause the other Action Movies on your DVD case to disappear)
.
It's better to just name the picturebox something else, so that you aren't getting it mixed up with another one. i.e. you wouldn't have two boxes of Action Movies both labeled "Action Movies", looking identical, but with different movies in it, right?
Quote:
In case you haven't noticed, I'm fairly new to VB, ie a month and a bit.
Also, if you are new to VB, why are you trying to make a RTS? Shouldn't you be making a test project or a simpler one so that you can learn VB first? Trying to make programs that are over your head doesn't seem very good for long-term programming.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #13  
Old 12-22-2009, 07:26 PM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

How can I make a new picture box with a picture inside of it?

Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
Dim PB As PictureBox
PB = New PictureBox
PB.Visible = True
Me.Controls.Add(PB)
PB.Left = 500
PB.Top = 300
PB.Width = 250
PB.Height = 250
End Sub

The problem is, when i click the button cmdGrunt nothing shows up.

And as I like to say, 'Don't learn to walk before you run. Learn to fly before you walk!'

Thanks for the help. I really appreciate it.
Reply With Quote
  #14  
Old 12-22-2009, 08:40 PM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

To create a bitmap at runtime:
Bmp = New Bitmap(filename)

Regardless of where the picture comes from, you assign pictures to your picturebox by setting the Picturebox's .Image property
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #15  
Old 12-23-2009, 10:10 AM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

What would the bitmap be for? When I specify the file name, do I put in the location of the file (ie C:\Pictures\etc)? How do I use the .Image property?

Thanks.
Reply With Quote
  #16  
Old 12-23-2009, 10:28 AM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

A Bitmap is a type of image.
When you create a bitmap from a filename, you can use a hardcoded filename:
Bmp = New Bitmap("C:\Users\Iceplug\Pictures\Snow.jpg")
You can use the result of an openfiledialog
Bmp = New Bitmap(OFD.Filename)
You can use a relative path if you have bitmap files in the folder with the application by using Application.StartupPath
Bmp = New Bitmap(Application.StartupPath & "\crystal.bmp")

In all of these cases, the value between the parentheses is a string that has a complete filename and filepath in it.
The resulting bitmap can be assigned to a bitmap object
(Dim Bmp As Bitmap)
as it is done in the above examples,
and you can then assign that bitmap object to an image property of one of your forms.
PicBox.Image = Bmp
Now, your picture box will display the image that is contained in the bitmap object.
Or you could assign the new bitmap directly to the .Image property of the PicBox:
PicBox.Image = New Bitmap(Filename)

__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #17  
Old 12-23-2009, 05:08 PM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

Private Sub cmdGrunt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrunt.Click
Dim PB As PictureBox
PB = New PictureBox
PB.Visible = True
Dim bmp As New Bitmap
bmp = New Bitmap("C:\Users\Owner\PicturesGrunt-Paint")
PB.Image = bmp
Me.Controls.Add(PB)
PB.Image = bmp
PB.Left = 500
PB.Top = 300
PB.Width = 250
PB.Height = 250
End Sub

The bmp in the line 'Dim bmp As New Bitmap' is giving me an error. The error is 'Error 1 Overload resolution failed because no accessible 'New' accepts this number of arguments. C:\Users\Owner\Documents\Visual Studio 2008\Projects\Summative.vb 18 13 Summative'
Reply With Quote
  #18  
Old 12-23-2009, 05:53 PM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Dim bmp As Bitmap
You should not make it a habit of Dimming New things... you want to:
declare:
Dim bmp As Bitmap
and then instantiate:
bmp = New Bitmap(filename)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #19  
Old 12-24-2009, 04:59 PM
Epidilius Epidilius is offline
Newcomer
 
Join Date: Dec 2009
Posts: 22
Default

I have it almost working! I thank both of you for your advice and help, and I have one more (hopefully) question.

Private Sub cmdWizard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWizard.Click
Dim G As Graphics
Dim BMP As Bitmap
BMP = New Bitmap("C:\Users\Owner\Pictures\Grunt-Paint.jpg")
G = Graphics.FromImage(BMP)
G.DrawImageUnscaled(BMP, 0, 0)
Me.BackgroundImage = BMP
G.Dispose()
End Sub

The problem: it covers my entire form with the picture. Not as one giant stretch, but hundreds of little tiny ones. Is there a fix?
Reply With Quote
  #20  
Old 12-24-2009, 07:13 PM
Iceplug's Avatar
IceplugHow do I make a button spawn an object? Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Do you want the picture to be scaled over the entirety of your form? If so, then Unscaled won't scale it.
You should use the overload of DrawImage that deals with one rectangle.
Bmp, New Rectangle(L, T, W, H)
http://www.iceplug.us/cngdi.htm
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object? How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object? How do I make a button spawn an object?
How do I make a button spawn an object?
How do I make a button spawn an object?
 
How do I make a button spawn an object?
How do I make a button spawn an object?
 
-->