Page 1 of 2

VB Questions

Posted: Wed Jun 27, 2007 8:36 pm
by Patrickh
I'm new to making applications, and I need to know how to (in VB) make it so when you click your button, it opens a new form that you can add things to. Help is always appreciated, thanks.

Posted: Thu Jun 28, 2007 4:30 am
by stephen10101
In VB 6 you go to project and then add form, then you put a command button and the code for it is (if you named the new form form2, if not replace form2 with what you named it.)

Code: Select all

form2.visible = true
In VB.net you go to project and then add windows form and put a command button and the code is

Code: Select all

YourForm'sNameHere.show()
I hope that was helpful.

Posted: Thu Jun 28, 2007 7:15 am
by Altimit01
In another basic dialect, you can also add windows in code if you want to have multiple instances of a form. (Which might be useful for what you need to do.) It should work in VB.

First make and design a new form in your IDE like stephen10101 said. Then when you want to add that window during runtime do something like this:

Code: Select all

dim w as new custom_form
w.show

Posted: Thu Jun 28, 2007 7:36 am
by Dagger13
for vb 2005 press this button

Image

Posted: Thu Jun 28, 2007 8:23 am
by LuxuriousMeat
Dagger13 wrote:for vb 2005 press this button

Image
He wants to open a form he already made, not add a new form to his project.

Posted: Thu Jun 28, 2007 11:01 am
by Dagger13
o ok yeah then all u do is type

Code: Select all

form2(or what ever its name is).show()

Posted: Thu Jun 28, 2007 12:04 pm
by Patrickh
Wow! Thank you guys so much. I can do all sorts of things I was just really tired of jamming them all into one form. Now the possibilities are endless!
Thanks again.

Posted: Thu Jun 28, 2007 4:17 pm
by PlasmaGhost
You should give C# a chance :wink:

Posted: Thu Jun 28, 2007 6:23 pm
by Patrickh
yeah, the reason I'm doing VB is because I started off using Locke's tutorials. :)
I have another problem, I'm not sure why this doesn't work.
My main form is called frmExplorer2. Then I have another form (form4) that contains a textbox (TextBox1) and a button (cmdname)
I tried to make it so that if you type some thing in the box, then hit the button, it sets the title of the main form to whatever's in the box. It worked when I put the box and button in the main form, but not when they are in the new window (it just does nothing). Anyway, here's what I wrote:

Code: Select all

Public Class Form4
    Private Sub cmdname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmExplorer2.Text = "" & TextBox1.Text
    End Sub

End Class
no build errors, but it doesn't work...

Posted: Thu Jun 28, 2007 6:44 pm
by Dagger13
because you wrote frmexplorer2 and not form2 or in your case form4

Code: Select all

Public Class Form4
    Private Sub cmdname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmExplorer2.Text = "" & TextBox1.Text
    End Sub

End Class
it should be like this

Code: Select all

Public Class Form4
    Private Sub cmdname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       form4.Text = "" & TextBox1.Text
    End Sub

End Class
and the reason u have a exploer is beacause you picked explorer instead of windows form

Posted: Thu Jun 28, 2007 7:02 pm
by Altimit01
Patrickh wrote:My main form is called frmExplorer2. Then I have another form (form4) that contains a textbox (TextBox1) and a button (cmdname)
I tried to make it so that if you type some thing in the box (in form4), then hit the button (also in form4), it sets the title of the main form (frmExplorer2) to whatever's in the box...
You misunderstand what the problem is. And I can't figure out why that wouldn't work. You might need to refresh the form to make the change visible.

Code: Select all

Public Class Form4 
    Private Sub cmdname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
        frmExplorer2.Text = "" & TextBox1.Text 
		frmExplorer2.Refresh
    End Sub 

End Class

Posted: Thu Jun 28, 2007 7:04 pm
by Patrickh
Okay, I erased it all and typed it out again and it worked. Dagger, yours works for changing the name of form4, except you have to change form4 in the third line to 'Me'.
I was trying to change the name of a different form via this form. Here's what it looks like:

Code: Select all

Public Class Form4
    Private Sub cmdname_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdname.Click
        frmExplorer2.Text = "" & TextBox1.Text

    End Sub
End Class

Posted: Thu Jun 28, 2007 7:10 pm
by Dagger13

Code: Select all

Public Class Form4 
    Private Sub cmdname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
        frmExplorer2.Text = "" & TextBox1.Text 
		frmExplorer2.Refresh
    End Sub 

End Class
no u dont have to refresh it and yes i forgot u do have to put "me.show"
and make sure you have the form that u want to change the name is open

Posted: Thu Jun 28, 2007 7:21 pm
by Patrickh
Would anybody mind if I just renamed this topic to 'VB questions' and just posted any problems/questions here rather than flooding the section with new topics?

Posted: Thu Jun 28, 2007 7:30 pm
by Dagger13
i wouldnt mind and i would be happy to help

Posted: Sat Jun 30, 2007 10:20 pm
by Patrickh
Ok, new problem. I made a program that opens .txts, edits them and then can save them somewhere. So I decided to make another one but for images. I got it so that when you hit File>Open it opens a Open Folder Dialogue, you browse for an image, hit open, and the image appears in a picbox, with the location in a textbox. no issues so far. However, I can't get it to save :? . I've tried several methods with no luck. I need the code so that when you hit a button, lets say 'cmdSave', a Save File Dialog opens, and you can save your image (as a JPEG, I can figure out the rest). Call the Box whatever, I can always edit it, but I need a working code to study for future use. Thanks a lot to whoever helps me.
EDIT: Actually if you can only save as bmp that's fine too. And keep in mind I started programming like 2 or 3 days ago.

Posted: Sun Jul 01, 2007 7:39 am
by OwnZ joO
Dagger13 wrote:

Code: Select all

Public Class Form4 
    Private Sub cmdname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
        [b]frmExplorer2.Text = "" & TextBox1.Text [/b]
		frmExplorer2.Refresh
    End Sub 

End Class
no u dont have to refresh it and yes i forgot u do have to put "me.show"
and make sure you have the form that u want to change the name is open
Jw why you concant empty string and TextBox1.Text, you can just put TextBox1.Text, and if you're trying to add what's in TextBox1 to the end of frmExplorer2.Text you should put &= i think(I forgot most of what I learned in VB.net). That's where I started out too, but I like C# better.

Posted: Sun Jul 01, 2007 9:02 am
by Prey
Heres the C# for saving the image of a picturebox, should only be marginally different in VB

Code: Select all

//let p be the picturebox
p.Image.Save("filename", System.Drawing.Imaging.ImageFormat.Bmp);

Posted: Sun Jul 01, 2007 5:27 pm
by Patrickh
I think it's much different in VB, I'll tinker with that though.

Posted: Sun Jul 01, 2007 6:34 pm
by LuxuriousMeat
Patrickh wrote:I think it's much different in VB, I'll tinker with that though.
Nope its the exact same way...

Code: Select all

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image.Save("filename", System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

End Class