Different VB.Net problems

I.e. show- and print.

Page 1 of 1

6 Replies - 1332 Views - Last Post: 30 April 2010 - 07:17 AM Rate Topic: -----

#1 amsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 10

Different VB.Net problems

Posted 29 April 2010 - 06:05 AM

Hey

Im totally new to programming, therefor sites like this helps alot (thanks for that :smile2:.)
Before I gets to the problems, I will just quickly tell a little about the program. It´s created in VB.Net and is meant to be a "Software-collector," meaning that things like web browser, sending mails and so on, is collected in one piece of software with my design. The functions is created using the tools VB.Net include.It is very simple buid with a start page linking to all the abbilities.
Now, the problems:
1. When accessing a function from the start page (button click,) I want the start page to close, therefore I added a
Me.close
in the buttons Sub. Instead of closing "Start," it closes the whole program.
2. I also have a picture viewer containing a "PictureBox" linked to a "OpenFileDialog." When viewing a picture, keeps the picture its original size which often is bigger than the "PictureBox" resolution. I have searched the internet, but the codes I found is hard to understand - I´m not asking you to write a whole new code, maybe if their exist a nice and easy guide :smile2:.
3. I also want a show- and print function in my web browser+my text editor (contains a "RichtTextBox.") I succed in printing from Web browser by adding
WebBrowser1.ShowPrintDialog()
to the "MenuItem" refering to it, but not from Text editor - even though I use same kind of code (of curse changed to
Texteditor.ShowPrintDialog()
.) I have added a Printdialog to both forms.
4. My last problem, which propaply is the biggest, occurs when trying to resize the program when running. I can make it "grow" but not make it "smaller." I think I have tried every possible "Anchor" setting, without luck. This problem goes for every form.

My code for:
Start page form:
Public Class Start


    Private Property ScaleHeight As Integer
    Private Property ScaleWidth As Integer
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ButWeb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButWeb.Click
        Dim Form As New WebBrowser
        Form.Show()

    End Sub

    Private Sub ButText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButText.Click
        Dim Form As New Text_organizer_form
        Form.Show()

    End Sub

    Private Sub ButPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButPicture.Click
        Dim Form As New PictureViewer
        Form.Show()

    End Sub

    Private Sub ButMedia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButMedia.Click
        Dim Form As New MediaPlayer
        Form.Show()

    End Sub

    Private Sub ButAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAbout.Click
        Dim Form As New About
        Form.Show()
    End Sub

    Private Sub ButMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButMail.Click
        Dim Form As New Email
        Form.Show()

    End Sub

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

    End Sub

End Class



Web browser form:
Public Class WebBrowser

    Private Property LinkLabel As WebBrowser

    Private Property vbMinimized As FormWindowState
    Private Property vbKeyReturn As Object
    Private Property KeyAscii As Object

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

    End Sub

    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs)

    End Sub

    Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    End Sub
 
    Private Sub WebBrowser1_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
        ProgressBar1.Maximum = e.MaximumProgress
        ProgressBar1.Value = e.CurrentProgress
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ButGå_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButGo.Click
        WebBrowser1.Navigate(AdresseBox.Text)
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ButSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButSearch.Click
        WebBrowser1.Navigate("http://www.google.dk/search?hl=da&q=" & AdresseBox2.Text)
    End Sub

    Private Sub ButForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButForward.Click
        On Error GoTo fejl
        WebBrowser1.GoForward()
fejl:
    End Sub

    Private Sub ButRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButRefresh.Click
        WebBrowser1.Refresh()
    End Sub

    Private Sub ButBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButBack.Click
        On Error GoTo Fejl
        WebBrowser1.GoBack()
Fejl:
    End Sub

    Private Sub ButStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButStop.Click
        WebBrowser1.Stop()
    End Sub

    Private Sub ButHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButHome.Click
        WebBrowser1.GoHome()
    End Sub

    Private Sub PrintToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem1.Click
        WebBrowser1.ShowPrintDialog()
    End Sub

    Private Sub NewPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewPageToolStripMenuItem.Click

    End Sub

    Private Sub CloseWebBrowserToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseWebBrowserToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ShowPrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowPrintToolStripMenuItem.Click
        On Error GoTo fejl
        Dim Form As New PrintPreviewDialog
        Form.Show()

fejl:   MsgBox("Nothing to print")
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click

    End Sub

    Private Sub AdresseBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles AdresseBox.KeyDown
        If e.KeyCode = Keys.Enter Then
            WebBrowser1.Navigate(AdresseBox.Text)

        End If
    End Sub

    Private Sub AdresseBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdresseBox.SelectedIndexChanged

    End Sub

    Private Sub AdresseBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles AdresseBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
            WebBrowser1.Navigate("http://www.google.dk/search?hl=da&q=" & AdresseBox2.Text)
        End If
    End Sub

    Private Sub AdresseBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdresseBox2.SelectedIndexChanged

    End Sub

    Private Sub PrintPreviewDialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewDialog1.Load

    End Sub

    Private Sub BackToStartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackToStartToolStripMenuItem.Click
        Dim Form As New Start
        Form.Show()
        Me.Close()
    End Sub

    Private Sub VisitAWebPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VisitAWebPageToolStripMenuItem.Click
        Dim Form As New Web_browser_Help___Visit_a_web_page
        Form.Show()
    End Sub

    Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click
        Dim Form As New Web_Browser_Help___Search
        Form.Show()
    End Sub
End Class


Text Editor:
Public Class TextEditor

    Private Property wrd As Object
    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        wrd = GetObject(, "Word.Application")
        wrd.Visible = True
        wrd.Documents.Open("C:\My Documents\Temp.doc")
        wrd = Nothing
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        Dim Form As New TextEditor
        Form.Show()
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Dim Form As New SaveFileDialog
        Form.ShowDialog()
    End Sub

    Private Sub BackToStartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackToStartToolStripMenuItem.Click
        Dim Form As New Start
        Form.Show()
        Me.Close()
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    End Sub

    Private Sub PrintPreviewControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    End Sub

    Private Sub PrintToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem1.Click


    End Sub
  
End Class


PictureViewer code form:
Public Class PictureViewer

    Private path As Bitmap
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub ButLoadImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButLoadImage.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        path = New Bitmap(OpenFileDialog1.FileName)
        PictureBox1.Image = path
    End Sub

    Private Sub ClosePictureViewerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClosePictureViewerToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click

    End Sub

    Private Sub BackToStartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackToStartToolStripMenuItem.Click
        Dim Form As New Start
        Form.Show()
        Me.Close()
    End Sub
End Class


Thanks for your help, really appreciated.

Amsel

This post has been edited by amsel: 29 April 2010 - 06:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Different VB.Net problems

#2 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Different VB.Net problems

Posted 29 April 2010 - 06:18 AM

Hi,

For your first question, you can hide the startform like this:

Me.Hide()


For your second form you can scale the image like this:

Dim PicBoxHeight As Integer
Dim PicBoxWidth As Integer
Dim ImageHeight As Integer
Dim ImageWidth As Integer
Dim TempImage As <strong class="highlight">Image</strong>
Dim scale_factor As Single

PictureBox1.SizeMode = PictureBoxSizeMode.Normal
' Get control <strong class="highlight">size</strong>
PicBoxHeight = PictureBox1.Height
PicBoxWidth = PictureBox1.Width
 ' Load the <strong class="highlight">image</strong>. Change <strong class="highlight">Image</strong>.FromFile() to match your code
TempImage = <strong class="highlight">Image</strong>.FromFile("D:\Download\Image00b.jpg")
' Get <strong class="highlight">image</strong> <strong class="highlight">size</strong>
ImageHeight = TempImage.Height
ImageWidth = TempImage.Width
' Calculate scale_factor
scale_factor = 1.0 ' No scaling by default i.e. <strong class="highlight">image</strong> fits <strong class="highlight">in</strong> the Box
' 1) Height
If ImageHeight > PicBoxHeight Then
  ' Reduce height first
  scale_factor = CSng(PicBoxHeight / ImageHeight)
End If
' 2) Width. Notice, we do know now how much we have to scale down the height
' and the scale_factor affects width too
If (ImageWidth * scale_factor) > PicBoxWidth Then
  ' Scaled width exceeds Box's width, recalculate scale_factor
  scale_factor = CSng(PicBoxWidth / ImageWidth)
End If

' Move <strong class="highlight">image</strong> to control for resizing
PictureBox1.Image = TempImage

' Get the source bitmap.
Dim bm_source As New Bitmap(PictureBox1.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
    CInt(bm_source.Width * scale_factor), _
    CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source <strong class="highlight">image</strong> into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
    bm_dest.Width + 1, _
    bm_dest.Height + 1)
' Display the result.
PictureBox1.Image = bm_dest

Was This Post Helpful? 0
  • +
  • -

#3 amsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 10

Re: Different VB.Net problems

Posted 29 April 2010 - 06:52 AM

View PostLuc001, on 29 April 2010 - 05:18 AM, said:

Hi,

For your first question, you can hide the startform like this:

Me.Hide()


For your second form you can scale the image like this:

Dim PicBoxHeight As Integer
Dim PicBoxWidth As Integer
Dim ImageHeight As Integer
Dim ImageWidth As Integer
Dim TempImage As <strong class="highlight">Image</strong>
Dim scale_factor As Single

PictureBox1.SizeMode = PictureBoxSizeMode.Normal
' Get control <strong class="highlight">size</strong>
PicBoxHeight = PictureBox1.Height
PicBoxWidth = PictureBox1.Width
 ' Load the <strong class="highlight">image</strong>. Change <strong class="highlight">Image</strong>.FromFile() to match your code
TempImage = <strong class="highlight">Image</strong>.FromFile("D:\Download\Image00b.jpg")
' Get <strong class="highlight">image</strong> <strong class="highlight">size</strong>
ImageHeight = TempImage.Height
ImageWidth = TempImage.Width
' Calculate scale_factor
scale_factor = 1.0 ' No scaling by default i.e. <strong class="highlight">image</strong> fits <strong class="highlight">in</strong> the Box
' 1) Height
If ImageHeight > PicBoxHeight Then
  ' Reduce height first
  scale_factor = CSng(PicBoxHeight / ImageHeight)
End If
' 2) Width. Notice, we do know now how much we have to scale down the height
' and the scale_factor affects width too
If (ImageWidth * scale_factor) > PicBoxWidth Then
  ' Scaled width exceeds Box's width, recalculate scale_factor
  scale_factor = CSng(PicBoxWidth / ImageWidth)
End If

' Move <strong class="highlight">image</strong> to control for resizing
PictureBox1.Image = TempImage

' Get the source bitmap.
Dim bm_source As New Bitmap(PictureBox1.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
    CInt(bm_source.Width * scale_factor), _
    CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source <strong class="highlight">image</strong> into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
    bm_dest.Width + 1, _
    bm_dest.Height + 1)
' Display the result.
PictureBox1.Image = bm_dest


I just noticed I forgot to put my PictureViewer code in my first post (sorry for that), I will correct it just now.
Anyway back to reply, thanks for your codes :smile2:. The first solution works completely, simple as it maybe seems :smile2:.
Number two I have a little problems with:
When adding the code to the "PictureBox1_Click" sub (isn´t it correct placed:smile2:), it reports the following:
1. Name `TempImage´ is yet not declared.
2. Type expected - points at the little sign before "Strong":
Dim TempImage As <strong class="highlight">Image</strong>
.
3. `FromFile´ is not a member of `System.Xml.Linq.XElemt´. Points at text after "=" until "()":
TempImage = <strong class="highlight">Image</strong>.FromFile("D:\Download\Image00b.jpg")


Amsel

This post has been edited by amsel: 29 April 2010 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Different VB.Net problems

Posted 29 April 2010 - 02:39 PM

I would like to add one comment as to why the application is closing when you used me.close

In the project properties is a setting called "Shutdown Mode" You can set this to
"When Startup Form Closes" which means, no matter what other forms are open in the project, the project will close if the form that appears on startup is closed
and
"When last form closes" Which means as long as you have a form open in the project, it will not automatically shut down.

You would choose the second of the two and that would let you close the main form as long as you previously opened another form in the application.

Right now, if you close the last form and forget to close the "Hidden" form, your application is still running unless you End it.
Was This Post Helpful? 1
  • +
  • -

#5 amsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 10

Re: Different VB.Net problems

Posted 30 April 2010 - 12:58 AM

View PostCharlieMay, on 29 April 2010 - 01:39 PM, said:

I would like to add one comment as to why the application is closing when you used me.close

In the project properties is a setting called "Shutdown Mode" You can set this to
"When Startup Form Closes" which means, no matter what other forms are open in the project, the project will close if the form that appears on startup is closed
and
"When last form closes" Which means as long as you have a form open in the project, it will not automatically shut down.

You would choose the second of the two and that would let you close the main form as long as you previously opened another form in the application.

Right now, if you close the last form and forget to close the "Hidden" form, your application is still running unless you End it.


Thank you for your comment :smile2:.
I see what you mean, but I canīt find the Shutdown Mode function under Properties :/.
Was This Post Helpful? 0
  • +
  • -

#6 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Different VB.Net problems

Posted 30 April 2010 - 02:12 AM

Hi,

You can find the startupmode like this:

Menubar -> Project -> properties (the last one) -> Application and scrolldown until you'll find shutdown mode -> select when lastform closes. Done.
Was This Post Helpful? 0
  • +
  • -

#7 amsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 10

Re: Different VB.Net problems

Posted 30 April 2010 - 07:17 AM

View PostLuc001, on 30 April 2010 - 01:12 AM, said:

Hi,

You can find the startupmode like this:

Menubar -> Project -> properties (the last one) -> Application and scrolldown until you'll find shutdown mode -> select when lastform closes. Done.


Great, there it was, thanks :smile2:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1