6 Replies - 983 Views - Last Post: 01 March 2009 - 11:20 AM Rate Topic: ***** 1 Votes

#1 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Why is this code not working?

Post icon  Posted 28 February 2009 - 10:44 AM

The following code gets the icon of the application (location in textbox1) then saves it to another location (textbox2)
Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			Me.Icon = (System.Drawing.Icon.ExtractAssociatedIcon(TextBox1.Text))
			MsgBox("success")
			Me.Icon.ToBitmap()
			PictureBox1.Image = Me.Icon.ToBitmap

			Try
				PictureBox1.Image.Save(TextBox2.Text)
			Catch
				Dim save As New SaveFileDialog
				save.ShowDialog()
				save.Title = "save icon"
				save.Filter = ".bmp"
				save.InitialDirectory = FileIO.SpecialDirectories.MyPictures
				save.FileName = "icon " + TextBox1.Text
				save.CheckPathExists = True
			End Try

		Catch
			MsgBox("fail")
		End Try

	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		TextBox2.Text = FileIO.SpecialDirectories.MyPictures
	End Sub
End Class



But I get my own FAIL mesage every time and I cannot work out why.

Is This A Good Question/Topic? 0
  • +

Replies To: Why is this code not working?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Why is this code not working?

Posted 28 February 2009 - 10:56 AM

Well I tested this and it worked fine for me. For textbox1 I typed in the path "C:\Program Files\Mozilla Firefox\firefox.exe" and in the second textbox I typed "C:\firefoxicon.bmp"

This was to copy over the firefox icon into a bmp file on the C:\ root folder.

So to figure out what you are doing wrong, instead of doing a simple catch, change your catch (where you write fail) to something like....

catch ex as Exception
     MessageBox.Show(ex.ToString())
end try



Now try it again and this time instead of just printing out "fail" you will get a more detailed error message of what is going on. You are probably not typing in a valid or recognizable path to an exe file.

So give it a try and then if you can't figure it out still, show us the error message you are getting from your try catch.

:)
Was This Post Helpful? 0
  • +
  • -

#3 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Why is this code not working?

Posted 28 February 2009 - 11:03 AM

Thanks i did try that i did this
dim ex as exceptio
msgbox "Fail" + Ex

but it didnt work :) thanks Thats a gd bit of code and maybe its just me lol what a funny thought :blink: :P
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Why is this code not working?

Posted 28 February 2009 - 11:13 AM

What Martyr2 was saying is to change your Catch block, take a look at the modifications I made to your code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Try
		Me.Icon = (System.Drawing.Icon.ExtractAssociatedIcon(TextBox1.Text))
		MsgBox("success")
		Me.Icon.ToBitmap()
		PictureBox1.Image = Me.Icon.ToBitmap

		Try
			PictureBox1.Image.Save(TextBox2.Text)
		Catch
			Dim save As New SaveFileDialog
			save.ShowDialog()
			save.Title = "save icon"
			save.Filter = ".bmp"
			save.InitialDirectory = FileIO.SpecialDirectories.MyPictures
			save.FileName = "icon " + TextBox1.Text
			save.CheckPathExists = True
		End Try

	Catch ex As Exception
		MessageBox.Show("Error: " & ex.Message().ToString())
	End Try
End Sub



The change I made was in the Catch part of the Try...Catch. This will now show you what the error is that is preventing this from working for you.

Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#5 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Why is this code not working?

Posted 28 February 2009 - 11:21 AM

Purely out of intrest how would I save as an icon because at the mo it wont let me
when i do theis code and Im aware of the checkbox thing it wont let me
				If CheckBox.Checked Then
					Me.Icon.Save(TextBox2.Text)
				Else
					PictureBox1.Image.Save(TextBox2.Text)
				End If


it says "value type of string cannot be converted to system.io.stream" :(
Was This Post Helpful? 0
  • +
  • -

#6 CodingIlliterate  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 32
  • Joined: 28-September 08

Re: Why is this code not working?

Posted 28 February 2009 - 02:26 PM

View PostAsscotte, on 28 Feb, 2009 - 10:21 AM, said:

Purely out of intrest how would I save as an icon because at the mo it wont let me
when i do theis code and Im aware of the checkbox thing it wont let me
				If CheckBox.Checked Then
					Me.Icon.Save(TextBox2.Text)
				Else
					PictureBox1.Image.Save(TextBox2.Text)
				End If


it says "value type of string cannot be converted to system.io.stream" :(


You want to do this,

If CheckBox.Checked = true Then
					Me.Icon.ToBitmap.Save(TextBox2.Text, System.Drawing.Imaging.ImageFormat.Icon)
				Else
					PictureBox1.Image.Save(TextBox2.Text, System.Drawing.Imaging.ImageFormat.Jpeg)
				End If


I hope this solves your problem :D

This post has been edited by CodingIlliterate: 28 February 2009 - 02:28 PM

Was This Post Helpful? 1
  • +
  • -

#7 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Why is this code not working?

Posted 01 March 2009 - 11:20 AM

How would I override the function so that i could set the icon rather than get it because at the moment it just comes up with the error read only so it cant be set...

what the code would be like is somehting like this:
System.Drawing.Icon.ExtractAssociatedIcon(TextBox1.Text) = (me.icon)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1