Display a files icon in a PictureBox - vb.net

I know there is a lot of these noob questions already.
I wrote a small code for display the Icon from a file in a PictureBox and its not working :|
Button6.BackgroundImage = Bitmap.FromHicon(
New Icon(OpenFileDialog1.FileName, New Size(48, 48)).Handle
)
The error says:
Argument 'picture' must be a picture that can be used as a Icon
If anyone know whats the problem, please write it down.

This error means that the file you are trying to open is not an icon. An icon file has extension .ico. Also you need to open the OpenFileDialog1 before geting the selected file name:
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Button1.BackgroundImage = Bitmap.FromHicon(New Icon(OpenFileDialog1.FileName, New Size(48, 48)).Handle)
End If

Related

AppBarButton.Icon doesn't change on runtime

I have a problem updating AppBarButton Icon on runtime depending on some conditions.
<AppBarButton x:Name="WeekButton" Click="OnClick" Label="SomeText"
</AppBarButton>
I'm trying to update Icon property in some code behind with this code
WeekButton.Icon = new FontIcon() { Glyph = Runtime_Value_Here};
But nothing happens. The button doesn't change. But in any random time when the code works, It MAY change the button. I always see, that the Icon is new in code, but not on screen. None of the UpdateLayout helps.
Would appreciate any help.
Thanks
UPDATE:
It seems to not working with FontIcon, as with BitmapIcon changing everything works fine.
Force InvalidateArrange and it works
WeekButton.Icon = new FontIcon() { Glyph = "\uE29B", FontFamily = new FontFamily("Segoe UI Symbol")};
WeekButton.InvalidateArrange();
I believed you already found the solution with bitmap icon.
But some people who reached this page and want to know the solution like me,
Here is using bitmap icon and changing app-bar button icon dynamically.
BitmapIcon _bitmapIcon = new BitmapIcon();
_bitmapIcon.UriSource = new Uri("ms-appx:///Assets/yourBitmapIcon.png");
WeekButton.Icon = _bitmapIcon;
Try using the IconUri in order to give the value.
WeekButton.IconUri = new Uri("/Images/pause.png", UriKind.Relative);
Reference: Disable/Enable applicationbar Button in runtime with event textchanged (Windows Phone)

Image array help in Silverlight

I'm Back! And having more Silverlight issues (yay!)
I am trying to create an image array in Silverlight but the images are not appearing on the page. Here is my code:
Public imgImages(50) As Image
Public Sub Create_Image_Array()
Dim I As Integer
For I = 0 To 50
imgImages(I) = New Image
imgImages(I).SetValue(Canvas.LeftProperty, System.Convert.ToDouble(0))
imgImages(I).SetValue(Canvas.TopProperty, System.Convert.ToDouble(0))
imgImages(I).Name = "imgImages" & I
imgImages(I).Width = System.Convert.ToDouble(18)
imgImages(I).Height = System.Convert.ToDouble(18)
imgImages(I).Source = New BitmapImage(New Uri("/Resources/yellow2.png", UriKind.Relative))
imgImages(I).Visibility = Windows.Visibility.Visible
AddHandler imgImages(I).MouseLeftButtonUp, AddressOf ImageClickEventProc
Next I
End Sub
Public Sub Draw_Images()
For I = 1 To secObject.intNumberOfImages
imgImages(I).SetValue(Canvas.LeftProperty, System.Convert.ToDouble(secObject.Images(I).intPosX))
imgImages(I).SetValue(Canvas.TopProperty, System.Convert.ToDouble(secObject.Images(I).intPosY))
imgImages(I).Visibility = Windows.Visibility.Visible
Next I
End Sub
The image array is created when the page is navigated to and then the page requests location information from a server and once it has that information it sets the X and Y coordinates of the images. All that part works fine - that was apparently the easy part - All the coordinate information is received and stored in secObject, the data is there. The URI for the resource of the image is there and it is valid, I tested it with another image control on the page.
The problem is that the little images are not displaying. I have tried numerous ways of getting them to display. I have found code on Google that does almost the exact same thing that I am trying to do and it is written in a similar way just for non-arrayed images.
I also tried another suggestion, to use TranslateTransform to set the positions of the images. This did nothing.
Dim tt As New TranslateTransform
tt.X = secObject.Images(I).intPosX
tt.Y = secObject.Images(I).intPosY
imgImages(I).RenderTransform = tt
I also removed the background image on the screen thinking that maybe the images were rendering below the background, and that is not the case.
Am I missing something? I admit to being a Silverlight n00b...
Thanks
-RW
OK I finally figured it out... I needed to create add the controls to the canvas:
LayoutRoot.Children.Add(imgImages(I))

VB.NET Form Icon Error

Everytime I set the form icon, I get the following error :
Value of type 'System.Drawing.Icon' cannot be converted to 'String'
I have tried both external icon file and resources method, but none worked.
Dim X as new System.Drawing.Icon("icon.ico")
Me.Icon = X
and
Me.Icon = My.Resources.Icon
Same thing if I create a new project.
Any help is appreciated, and thanks in advance!
If you are developing a WinForms application, your first attempt should work:
Me.Icon = New System.Drawing.Icon("path/to/.ico/file")
If you are writing your UI with WPF, then the .Icon property expects an ImageSource object:
Dim iconUri As New Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute)
Me.Icon = BitmapFrame.Create(iconUri)
The second example is directly taken from the MSDN.

Associated icons blank in OpenFileDialog

I'm deploying an application built with VS 2010 Express edition via ClickOnce.
I have associated some file extensions using the options provided in the Publish section of the project's properties.
When I install the application, the associated file extension icons appear normally in Explorer but, when the user tries to open a file in my application, the associated icons appear blank.
The relative code section is the following:
Dim dialog As New OpenFileDialog
With dialog
.AddExtension = True
.CheckFileExists = True
.CheckPathExists = True
.DefaultExt = "pss"
.FileName = String.Empty
.Filter = "Data files|*.pss|All files (*.*)|*.*"
.FilterIndex = 0
.InitialDirectory = MySettings.SavePath
.Multiselect = False
.ValidateNames = True
If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
OpenFile(.FileName)
End If
End With
I was always under the impression that OpenFileDialog is a wrapper for Explorer, but for some reason the icons don't appear correct.
Does anyone have any idea how to correct the problem?
Edit: The behaviour is only relevant to Windows 7. Windows XP behaves normally.
I believe you need to set the default view to thumbnail, so that the graphics appear for the pss files. You can check this by manually changing the view to thumbnail while running your application.
If changing the view to thumbnail shows the graphics then check the link below to achieve this:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/571b39c2-0edf-4159-ac8d-1c059f69a978
I forgot to change the included associated icons to:
Build action: Content
Copy to Output Directory: Always
I found this thanks to the manual updates, during which I was getting the error message "Object Reference not set to an instance of an object" during the updateCompleted event.

Modify HTML in a Internet Explorer window using external.menuArguments

I've got a VB.NET class that is invoked with a context menu extension in Internet Explorer.
The code has access to the object model of the page, and reading data is not a problem. This is the code of a test function...it changes the status bar text (OK), prints the page HTML (OK), changes the HTML by adding a text and prints again the page HTML (OK, in the second pop-up my added text is in the HTML)
But the Internet Explorer window doesn't show it. Where am I doing wrong?
Public Sub CallingTest(ByRef Source As Object)
Dim D As mshtml.HTMLDocument = Source.document
Source.status = "Working..."
Dim H As String = D.documentElement.innerHTML()
MsgBox(H)
D.documentElement.insertAdjacentText("beforeEnd", "ThisIsATest")
H = D.documentElement.outerHTML()
MsgBox(H)
Source.status = ""
End Sub
The function is called like this from JavaScript:
<script>
var EB = new ActiveXObject("MyObject.MyClass");
EB.CallingTest(external.menuArguments);
</script>
To the best of my understanding, in order to use insertAdjacentText or any of the other editing methods, the document object should be in the design mode.
In design mode you can edit the document freely, and so can the user.
Check this site for more details
I do not think that Alex is right, something else is the matter.
When I tried to do something like that, insertBefore would not work for me, but appendChild worked just fine, so adding an element is possible.
I worked in Javascript, but I don't expect that makes a difference.