Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds

Reply
 
Thread Tools
  #1  
Old 12-11-2009, 06:28 AM
Johnny Boy Johnny Boy is offline
Serf
 
Join Date: Jul 2009
Location: Pennsylvania
Thanks: 0
Thanked 3 Times in 3 Posts
Default Help in VB.NET 2008

I'm pretty new to programming, and i need help getting a button object to work while inside of a different public sub (Painteventargs)

The button is supposed to open a openfiledialog in which the user can select and image which is then opened on the form. However when i try to write code for the openfiledialog to appear when the button = true, nothing happens.

Here is the code I have:

Public Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

Dim button2 As Boolean = False

If button2 = True Then
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
End If
End If

' Create image.
Dim Newimage As Image = Image.FromFile(user)

' Create Point for upper-left corner of image.
Dim ulCorner As New Point(100, 100)

' Draw image to screen.
e.Graphics.DrawImage(Newimage, ulCorner)

End Sub

-----Edit

If i change the top of the code to:

Dim button2 as boolean = true

if button2 = true then
'code
end if

the openfiledialog works and lets me open a picture which the appears on the form like i want....however it just asks me to open another file after the first one opens...its stuck in a loop, so when i change the dim statement to false, it should just do the openfile dialog once i hit the button right? It doesn't and i am not sure why.

Last edited by Johnny Boy; 12-11-2009 at 06:59 AM.
Reply With Quote
  #2  
Old 12-11-2009, 08:51 AM
Axiom Axiom is offline
Duke
 
Join Date: May 2008
Thanks: 21
Thanked 53 Times in 42 Posts
Default Re: Help in VB.NET 2008

What's the code for calling this function? Is it in a loop?

also - use code tags [code] it makes it easier to read...
Reply With Quote
  #3  
Old 12-11-2009, 09:04 AM
Johnny Boy Johnny Boy is offline
Serf
 
Join Date: Jul 2009
Location: Pennsylvania
Thanks: 0
Thanked 3 Times in 3 Posts
Default Re: Help in VB.NET 2008

Code:
Imports System.IO
Imports System.Windows.Forms.MouseEventArgs
Imports System.Windows.Forms.Form
Imports System.Windows.Forms.PaintEventArgs

Public Class Edtor
    Private gfx As Graphics
    Public user As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Visible = True
        Me.Visible = False
    End Sub

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

    End Sub

    Private Sub Edtor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        gfx.Dispose()
    End Sub

    Private Sub Edtor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        gfx = Me.CreateGraphics()

    End Sub

    Public Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

        If (Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            user = Me.SaveFileDialog1.FileName

        End If

    End Sub

    Public Sub Edtor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove


        If RadioButton1.Checked = True Then

            If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
            Dim rect As Rectangle

            With rect
                .X = e.X - 1
                .Y = e.Y - 1
                .Height = 2
                .Width = .Height / .Height

                gfx.DrawRectangle(System.Drawing.Pens.Red, rect)

            End With
        End If
        If RadioButton2.Checked = True Then

            If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
            Dim rect As Rectangle
            With rect
                .X = e.X - 1
                .Y = e.Y - 1
                .Height = 2
                .Width = .Height / .Height


                gfx.DrawRectangle(System.Drawing.Pens.Blue, rect)

            End With
        End If
        If RadioButton3.Checked = True Then
            If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
            Dim rect As Rectangle

            With rect
                .X = e.X - 1
                .Y = e.Y - 1
                .Height = 2
                .Width = .Height / .Height

                gfx.DrawRectangle(System.Drawing.Pens.Black, rect)

            End With
        End If

    End Sub

    Private Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Call Button2_Click(sender, e)

        ' Create image.
        Dim Newimage As Image = Image.FromFile(user)

        ' Create Point for upper-left corner of image.
        Dim ulCorner As New Point(100, 100)

        ' Draw image to screen.
        e.Graphics.DrawImage(Newimage, ulCorner)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonpic.Click

        If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            user = Form1.openDiag.FileName
        End If


    End Sub
End Class
That is the entire code if that helps you out any, the form is called from a button on a different form called Form1...one this form gets called...nothing is supposed to happen UNTIL i push button2, and then the openfile dialog appears and i choose a file to load...but it does it automatically, and in a loop even though i never specified it in an actual loop.

Thanks for the reply!
Reply With Quote
  #4  
Old 12-11-2009, 09:10 AM
Fatman Fatman is offline
Member
 
Join Date: Nov 2009
Thanks: 0
Thanked 9 Times in 7 Posts
Default Re: Help in VB.NET 2008

Quote:
Originally Posted by Johnny Boy View Post
Dim button2 As Boolean = False

If button2 = True Then
...
Button2 will never be true in this case. You declare the variable and make it false as the first statement in the method.

Try making it a global variable, or handling the button click event and putting your code there
Reply With Quote
  #5  
Old 12-11-2009, 09:11 AM
Johnny Boy Johnny Boy is offline
Serf
 
Join Date: Jul 2009
Location: Pennsylvania
Thanks: 0
Thanked 3 Times in 3 Posts
Default Re: Help in VB.NET 2008

if i dont make it false
button2 is never assiged a value (True or false)

So i get an error saying that 'user' has no filepath because i was never able to choose a file since the openfiledialog doesnt open.

Last edited by Johnny Boy; 12-11-2009 at 09:17 AM.
Reply With Quote
  #6  
Old 12-11-2009, 09:18 AM
Fatman Fatman is offline
Member
 
Join Date: Nov 2009
Thanks: 0
Thanked 9 Times in 7 Posts
Default Re: Help in VB.NET 2008

Make it global, see what happens.
Reply With Quote
  #7  
Old 12-11-2009, 09:25 AM
Johnny Boy Johnny Boy is offline
Serf
 
Join Date: Jul 2009
Location: Pennsylvania
Thanks: 0
Thanked 3 Times in 3 Posts
Default Re: Help in VB.NET 2008

When you say global, do you mean like

dim button2 as global?

Because im not sure how that syntax works, it gives me an error.
I'm very new to programming.

Thanks for your help though!
Reply With Quote
  #8  
Old 12-11-2009, 09:48 AM
Fatman Fatman is offline
Member
 
Join Date: Nov 2009
Thanks: 0
Thanked 9 Times in 7 Posts
Default Re: Help in VB.NET 2008

Global means you can access if from all methods. It is a class wide variable. Declare it just below 'Public class whatever'.
Reply With Quote
  #9  
Old 12-11-2009, 10:08 AM
Johnny Boy Johnny Boy is offline
Serf
 
Join Date: Jul 2009
Location: Pennsylvania
Thanks: 0
Thanked 3 Times in 3 Posts
Default Re: Help in VB.NET 2008

Problem solved thanks guys! Heres the code incase your interesed

Code:
Imports System.IO
Imports System.Windows.Forms.MouseEventArgs
Imports System.Windows.Forms.Form
Imports System.Windows.Forms.PaintEventArgs

Public Class Edtor
    Private gfx As Graphics
    Public user As String
    Private pathset As Boolean
    Dim button2 As Global.System.Boolean




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Visible = True
        Me.Visible = False
    End Sub

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

    End Sub

    Private Sub Edtor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        gfx.Dispose()
        
    End Sub

    Private Sub Edtor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        gfx = Me.CreateGraphics()

    End Sub

    Public Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

        If (Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            user = Me.SaveFileDialog1.FileName

        End If

    End Sub

    Public Sub Edtor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove


        If RadioButton1.Checked = True Then

            If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
            Dim rect As Rectangle

            With rect
                .X = e.X - 1
                .Y = e.Y - 1
                .Height = 2
                .Width = .Height / .Height

                gfx.DrawRectangle(System.Drawing.Pens.Red, rect)

            End With
        End If
        If RadioButton2.Checked = True Then

            If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
            Dim rect As Rectangle
            With rect
                .X = e.X - 1
                .Y = e.Y - 1
                .Height = 2
                .Width = .Height / .Height


                gfx.DrawRectangle(System.Drawing.Pens.Blue, rect)

            End With
        End If
        If RadioButton3.Checked = True Then
            If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
            Dim rect As Rectangle

            With rect
                .X = e.X - 1
                .Y = e.Y - 1
                .Height = 2
                .Width = .Height / .Height

                gfx.DrawRectangle(System.Drawing.Pens.Black, rect)

            End With
        End If

    End Sub

    Private Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint



        If pathset = True Then

            If button2 = True Then
                If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                    user = Form1.openDiag.FileName
                End If
            End If

            ' Create image.
            Dim Newimage As Image = Image.FromFile(user)

            ' Create Point for upper-left corner of image.
            Dim ulCorner As New Point(100, 100)

            ' Draw image to screen.
            e.Graphics.DrawImage(Newimage, ulCorner)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonpic.Click

        If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then

            user = Form1.openDiag.FileName

            pathset = True

        End If
    End Sub
End Class
Reply With Quote
Reply

Bookmarks

Tags
2008, vbnet

Thread Tools

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The X Files: I Want to Believe [2008] Dfg TV and Movies 3 09-16-2009 07:17 PM
I'm so 2008 reject Generally Speaking 2 06-29-2009 11:59 AM
Q-Tip - The Renaissance (2008) Young Meth Music! 2 05-12-2009 12:37 AM
Bonnaroo 2008 duuude Music! 0 03-14-2009 06:43 PM
i cant believe 2008 is over H a r o l d Bat Country 10 01-27-2009 10:59 AM


All times are GMT. The time now is 08:24 AM.


Hot Topics
On IRC
Users: 4
Messages/minute: 0
Topic: "http://www.zoklet.net/..."
Users: 20
Messages/minute: 0
Topic: "ask ibm why atlantis is real"
Users: 9
Messages/minute: 0
Topic: "So wie ich die sache sehe ist die intelligenz bereits ausgerot..."
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.