Script Errors In VB - vb.net

I am trying to stop those annoying Runtime Errors in VB. I am using a tabcontrol with a webbrowser item. I am trying to supress the errors. It does not return any errors but at runtime, it doesn't work. This is my code
CType(TabControl.SelectedTab.Controls.Item(0), WebBrowser).ScriptErrorsSuppressed = True
What am I doing wrong?

Your browser control may not be Item(0) on the page (it could be in a panel etc), so as an alternative (and safer method) you can use the Controls.Find method to search the selected tab and all child controls in order to find the Browser on that tab.
Something like this:
'assumes you have a control named WebBrowser1 on the first tab page etc
Dim browserControlName As String = String.Format("WebBrowser{0}", TabControl1.SelectedIndex + 1)
Dim browser As WebBrowser = CType(TabControl1.SelectedTab.Controls.Find(browserControlName, True).First, WebBrowser)
browser.ScriptErrorsSuppressed = True

Related

WebBrowser control Anchor tag target = _Blank opening outside WebBrowser control

I want to navigate to new tab when click on anchor. My code :
Hide Copy Code
Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each pElem As HtmlElement In elements
If (pElem.GetAttribute("HREF") <> "" AndAlso pElem.GetAttribute("HREF") <> "#" Then
WebBrowser1.AllowNavigation = True
pElem.SetAttribute("target", "_Blank")
pElem.InvokeMember("click")
End If
Next
When i Perform Click it will open in IE Browser instead of Web-Browser Control. Please brief me what I am doing wrong?
FYI, Instead of Navigate using URL i want to Navigate Using click and its compulsory.
Thanks in advance.

How to press Upload button through VBA

I was searching a lot on the web and found your website very helpfull - that is why I have not posted anything yet, just reading.
However, I need your advice in the following step of my project. The porject is to automate the creation/placing of ad for selling goods.
The website is http://olx.bg/adding/
What I am failing to do through VBA is to press the blue square called "ДОБАВИ СНИМКА" which stands for "ADD PHOTO" in the form.
Once pressed a default BROWSE window opens to select the file.
Thanks in advance!
Add two references to your VBA project:
Microsoft Internet Controls
Microsoft HTML Object Library
The HTML behind the button has an id = add-files which makes it pretty easy to locate using getElementById
Here is a working example:
Sub PushAddPhotoButton()
Dim IEexp As InternetExplorer
Set IEexp = New InternetExplorer
IEexp.Visible = True
IEexp.navigate "http://olx.bg/adding/"
Do While IEexp.readyState <> 4: DoEvents: Loop
Dim photoButton As HTMLInputElement
Set photoButton = IEexp.Document.getElementById("add-files")
If (Not photoButton Is Nothing) Then
photoButton.Click
Else
MsgBox "Button not found on web page."
End If
IEexp.Quit
Set IEexp = Nothing
End Sub
If your IE security settings are on 'High' for Internet then you won't get a dialog or an error. The IE window will simply flash and then be gone.
Change to:

VB.Net WebBrowser Navigate Only Working Once

Hoping someone can help me with this. I have two separate but related Forms, one of which contains a WebBrowser control. The user fills out some information on Form 1 and clicks a button with the following code:
If Form2Shown = False Then
Dim memoscreen As New Form2
Form2Ref = memoscreen
memoscreen.show()
Form2Shown = True
memoscreen.TopMost = OptionOnTop
Else
Dim memoscreen As Form2
memoscreen = Form2Ref
memoscreen.TopMost = OptionOnTop
memoscreen.QuickRefresh()
End If
The QuickRefresh sub in Form2 is the method that navigates. It is called both when the form is loaded as well as manually in the code above:
Public Sub QuickRefresh()
Dim HM As Form1
HM = Form1Ref
Me.Text = "retrieving information..."
Me.AxWebBrowser1.Navigate("SomeValidURL")
HM.Focus()
HM.SetHugoFocus()
End Sub
The problem I'm having is that the first time QuickRefresh is called (i.e. when Form2 is loaded) the navigation is successful and the page displays fine. If I then click the button on Form1 again, the page does not change. The Text attribute and window focus does change however, so I know the method is firing.
Some things I've tried/checked:
AllowNavigation is set to True on the WebBrowser control
Have tried looping while the browser is busy while calling Application.DoEvents()
Any suggestions would be appreciated. Thanks.
From your "Internet Options dialog > General Tab > Settings Button > Check for newer version of stored page" change that option to *Every Time I visit the webpage". That setting impacts how the webbrowser control deals with the refreshing.
Use the method refresh.
browser.Navigate("http://www.google.com") : browser.Refresh()

Invalid Conversion error

I recently upgraded a VB 6 project to .net. I'm having a problem with this block of code:
Dim CtrlName As System.Windows.Forms.MenuItem
For Each CtrlName In Form1.Controls
'Some code here
Next CtrlName
Now this code compiles but throws the following runtime error:
Unable to cast object of type 'System.Windows.Forms.Panel' to type 'System.Windows.Forms.MenuItem.
I have a panel control on the subject form. How do I resolve this?
Thanks.
You are iterating over all controls that are directly inside the form, not just the MenuItems. However, your variable is of type MenuItem. This is causing the problem.
For normal controls (e.g. Buttons), you’d want to use the following, easy fix; test inside the loop whether the control type is correct:
For Each control As Control In Form1.Controls
Dim btt As Button = TryCast(control, Button)
If btt IsNot Nothing Then
' Perform action
End If
Next
However, this does not work for MenuItems since these aren’t controls at all in WinForms, and they aren’t stored in the form’s Controls collection.
You need to iterate over the form’s Menu.MenuItems property instead.
The items in the Controls property of a form, which may or may not be MenuItem. Assuming that you just want to iterate over MenuItem objects you can change your code to:
For Each menuControl As MenuItem In Me.Controls.OfType(Of MenuItem)
' Some code
Next
Note that the menuControl variable is declared in the For so is only accessible within the block and is disposed automatically.
for each ctrl as control in me.controls
if typeof ctrl is menuitem then
' do stuff here
end if
next
typeof keyword allows you to test the type of control being examined in the control collection.
Found the answer after a bit of research, you need to search for the menu strip first and then loop through the items collection.
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is MenuStrip Then
Dim mnu As MenuStrip = DirectCast(ctrl, MenuStrip)
For Each x As ToolStripMenuItem In mnu.Items
Debug.Print(x.Name)
Next
End If
Next

VB.NET Programmatically add code between tags

I have a custom control that does some work for me on async postbacks. Normally, I'd call the control directly from the presentation side using the following code on the ASPX page:
<mytag:CustomControl runat="server">
html (or other text) goes here
</mytag:CustomControl>
However, in my current application, I need to dymanically create the control from the codebehind, using code similar to the following:
Dim myControl As myClass.CustomControl = New myClass.CustomControl
myControl.ID = "someID"
myControl.?????? = "html (or other text) goes here"
Me.Controls.Add(myControl)
When adding the control to the page dynamically, how do I add info that would normally be between the start and end tags if the control were added the normal, non-dynamic way?
Thanks
Here's the actual control:
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim scriptmanagerPage As ScriptManager = ScriptManager.GetCurrent(Page)
If scriptmanagerPage Is Nothing Then
'Do nothing
Else
'See if we are in a postback
If scriptmanagerPage.IsInAsyncPostBack Then
'We are in a postback; register the script
Dim stringbuilderWorking As New StringBuilder()
MyBase.Render(New HtmlTextWriter(New StringWriter(stringbuilderWorking)))
Dim stringScript As String = stringbuilderWorking.ToString()
ScriptManager.RegisterStartupScript(Me, GetType(ScanWorkXAJAX), UniqueID, stringScript, False)
Else
'Not in a postback
MyBase.Render(writer)
End If 'In an async postback
End If 'Scriptmanager present
End Sub
What do you mean by data? More controls?
You can use
myControl.Controls.Add(childControlHere);
EDIT After question was clarified:
Add a literal control. i.e.
myControl.Controls.Add(new LiteralControl("<b>hello world</b><script type='text/javascript'>alert('hi');</script>"));
Do you mean FIND and append data to controls inside your dynamic control?
You can use the WebControl.FindControl method to find your control embedded in your custom control and then you can add data via its properties.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.findcontrol.aspx
That depends on the properties of the custom control you are using. You may have to bind to a template to display so that events will be fired, it really just depends on your specific control.
Was able to accomplish this using a literal control.
Code above plus:
Dim myLiteral As LiteralControl = New LiteralControl
myLiteral.ID = "myLiteral"
myLiteral.Text = "html (or some other text) goes here"
myControl.Controls.Add(myLiteral)