Optical zoom in webbrowser control - vb.net

I successfully use webbrowser control from VB.NET to show and print documents but I can't get zoom functionality which I would like to apply.
For example you can create a new project, add a form with "Webbrowser1" control and two buttons "btn_Plus" and "btn_Minus".
Don't forget to add COM reference to "Microsoft Internet Controls".
Option Strict Off 'because of late binding MyWeb.ExecWB
Imports SHDocVw
Public Class Form1
Dim zoomvalue As Integer = 50
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strHtml As String = "<HTML><h1>Doc...<h1></br><h4>My html here</br>acts like web page.<h4></HTML>"
Me.WebBrowser1.Navigate("about:" + strHtml)
End Sub
Private Sub zoom()
Try
Dim MyWeb As Object = Me.WebBrowser1.ActiveXInstance
MyWeb = Me.WebBrowser1.ActiveXInstance
MyWeb.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, zoomvalue, IntPtr.Zero)
MyWeb = Nothing
Catch ex As Exception
'MessageBox.Show("Error:" & ex.Message)
End Try
End Sub
Private Sub btn_Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Plus.Click
zoomvalue += 5
zoom()
End Sub
Private Sub btn_Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Minus.Click
zoomvalue -= 5
zoom()
End Sub
End Class
1) Why this won't work and I think it should work?
2) How to get this to work?
3) How to get current zoom value from actual webbrowser?

Try MyWeb.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, CObj(zoomvalue), CObj(IntPtr.Zero))

Related

Create list box in runtime and change its color via menu in runtime

I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...

It maybe inaccessible due to its protection level

I was trying to code a download manager but get this error:
'save' is not declared. It maybe inaccessible due to its protection level
Here is my code:
Imports System.Net
Public Class Form1
Private WithEvents httpclient As webClient
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Clear()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.ShowDialog()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
httpclient = New WebClient
Dim total As Integer = ListBox1.Items.Count
Dim current As Integer = -1
While current < total
ListBox1.SelectedIndex = current + 1
Dim download As String = ListBox1.SelectedItem
httpclient.DownloadFileAsync(New Uri(download), save)
Label3.Text = "Current Status: Downloading:..."
Do While httpclient.IsBusy
Application.DoEvents()
Loop
current = current + 1
End While
Catch ex As Exception
ListBox1.Items.Clear()
End Try
End Sub
End Class
DownloadFileAsync takes 2 parameters. The first is the URL of the file to download and the second is the local location to save the file. The tutorial you were following likely set the variable Save somewhere in the code.
To get this to work, you can replace it with something like "C:\Test.txt" to download it to that location or define the variable Save and set it's value to the location you want to save the file.
With your existing code, to allow a user to select the save location, you need to make the following changes. Declare the Save variable private to the form, under the Public Class Form1 line:
Private Save as string
Then in your Button1_Click event, it should look more like this:
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Save = SaveFileDialog1.FileName
End If
Dim download As String = ListBox1.SelectedItem
httpclient.DownloadFileAsync(New Uri(download), "save")

Reacting to third-party application form events using vb.net

I would like to know how I should code my VB.net application to react to the form load event in a third-party application (also written in VB.net)
To test, I have created two basic programs, one with two forms (program A) and one (program B) that attempts to the listen to program A's appropriate form load event. I have tried using WithEvents but it does not get fired when Program's A second form loads.
Here is the code for Program A:
Public Class StartPage
Public WithEvents loadtimer As New System.Windows.Forms.Timer
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadtimer.Interval = 1000
loadtimer.Enabled = True
loadtimer.Start()
End Sub
Private Sub loadtimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles loadtimer.Tick
loadtimer.Stop()
SystemStatus.Show()
End Sub
End Class
Public Class SystemStatus
Inherits StartPage
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "This is the form that I want to listen for the load event"
Me.loadtimer.Enabled = False
End Sub
End Class
Here is the code for Program B:
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New Program_A.SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
End Sub
End Class
I am quite new when it comes to programming so maybe this is not even possible or I just haven't been understanding what I've been reading. In any case please enlighten me as to what my next course of action should be.
Thanks very much in advance,
theoleric
This will do what your asking.
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
' now this event will fire
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
' the load event will not fire until you call this
testlisten.Show()
End Sub
End Class

Resizing userControls in newly initiated TabPage

When a new TabPage is added in my TabControls, I would like it to autosize to Form1's current height/width. I can do this on Form Load. However, when I add a new tab then resize the window, it does nothing. Thanks for any helpful input.
Imports System.IO
Public Class Form1
'The Global Variables
Dim theControls As New theControls
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'For Auto Sizing theControls to Form1
TabPage1.Controls.Add(theControls)
theControls.theBrowser.Navigate("http://google.com")
End Sub
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
'For Auto Sizing theControls to Form1
Me.TabControl1.Width = Me.Width - 20
Me.TabControl1.Height = Me.Height
theControls.Width = Me.TabControl1.Width - 20
theControls.Height = Me.TabControl1.Height - 20
End Sub
Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
'Add new tab with the same controls.
Dim theNewTab As New TabPage
Dim theOtherControls As New theControls
Dim theTabCounter As Integer = TabControl1.TabPages.Count
Dim theSelectedTab As String = TabControl1.SelectedTab.Text
If theSelectedTab = "+" Then
TabControl1.TabPages.Insert(theTabCounter - 1, theNewTab)
theNewTab.Controls.Add(theOtherControls)
theControls.theBrowser.Navigate("http://google.com")
theOtherControls.theBrowser.Navigate("http://google.com")
TabControl1.SelectTab(theTabCounter - 1)
End If
End Sub
End Class
The tabPageCounter variable can be ignore. I started to think a For Each loop would be needed but I think there's an easier way. From what I have found on the web, I have gotten to this Dock function but I guess I'm not exactly clear on how to use it...
Do it in Form_sizechanged event ..
Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
Me.TabControl1.Width = Me.Width - 20
myUserControl.Width = Me.TabControl1.Width -20
End Sub

VB.NET PictureBox scroll through images in a folder

I have a pictureBox on my form along with two buttons (Back and Forward) but I can not find a viable method of doing what I wish to do: Scrolling through images in a folder like the default Windows Picture Viewer does with Arrow Keys.
Is there an efficient way to do this?
I'm using Visual Basic .NET with Visual Studio 2010, if that matters.
You'll need to load the pictures using DirectoryInfo, then browse through them with an index. Here is an example:
Public Class Form1
Private files As List(Of FileInfo)
Private currentFileIndex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RefreshFolder("c:\path\to\your\pictures")
End Sub
Private Sub backButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backButton.Click
Advance(-1)
ShowCurrentFile()
End Sub
Private Sub forwardButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles forwardButton.Click
Advance(1)
ShowCurrentFile()
End Sub
Private Sub Advance(ByVal delta As Integer)
currentFileIndex = ((currentFileIndex + files.Count) + delta) Mod files.Count
End Sub
Private Sub RefreshFolder(ByRef path As String)
Dim di As DirectoryInfo = New DirectoryInfo(path)
files = (From c In di.GetFiles()
Where IsFileSupported(c)
Select c).ToList()
If files.Count > 0 Then
currentFileIndex = 0
End If
ShowCurrentFile()
End Sub
Private Sub ShowCurrentFile()
If currentFileIndex <> -1 Then
Try
PictureBox1.Image = Image.FromFile(files(currentFileIndex).FullName)
Catch ex As Exception
' TODO: handle exceptions gracefully
Debug.WriteLine(ex.ToString)
End Try
End If
End Sub
Private Function IsFileSupported(ByRef file As FileInfo) As Boolean
Return file.Extension = ".jpg" Or file.Extension = ".png" ' etc
End Function
End Class
you should be more specific.
if it will help you you have to create two subrotines that assign the next and pervious image to the picture box and triggier these subrotines on the key down events and the bottons clicks.