Imports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private mrButton As Button
Public Sub MyButtonClass()
mrButton = New Button()
mrButton.Text = "Click me "
mrButton.Click += New System.EventHandler(MyButtonClickEventHandler)
Me.Controls.Add(mrButton)
End Sub
Shared Sub Main()
Application.Run(New MyButtonClass())
End Sub
Private Sub MyButtonClickEventHandler(ByVal sender As Object, ByVal e As EventArgs)
mrButton.Text = "You clicked me!"
End Sub
End Class
You're mixing C# and VB.Net code.
mrButton.Click += New system.EventHandler(MyButtonClickEventHandler)
Is C# syntax.
The button handler should either be declared as:
Private Sub MyButtonClickEventHandler(ByVal sender As Object, ByVal e As EventArgs) Handles mrButton.Click
Or you use the AddHandler as:
AddHandler mrButton.Click, AddressOf MyButtonClickEventHandler
Related
My program was opening a different form to what I wanted it to. The answers solved it.
Basically I wanted to stop a form opening when the program started, but when it opened manually (on a button press), it updated the data. The second part of the problem has not been solved, but the first part has been.
You can try something like this:
Public Class HomeForm
Private WithEvents m_DataChangeForm As DataChangeForm
Private Sub HomeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_DataChangeForm = New DataChangeForm()
m_DataChangeForm.Show()
End Sub
Private Sub OnDataSourceChanged(sender As Object, args As EventArgs) Handles m_DataChangeForm.OnDataSourceChanged
MessageBox.Show("Data source changed!")
End Sub
End Class
Public Class DataChangeForm
Inherits Form
Public Event OnDataSourceChanged(sender As Object, args As EventArgs)
Private WithEvents m_Button As Button
Public Sub New()
m_Button = New Button()
m_Button.Text = "Change"
m_Button.Parent = Me
End Sub
Public Sub buttonClick(sender As Object, args As EventArgs) Handles m_Button.Click
RaiseEvent OnDataSourceChanged(sender, args)
Me.Close()
End Sub
End Class
Reason your form is displayed before HomeForm is becaouse you call ShowDialog, it blocks until DataChangeForm is closed.
You should move your code from "Load" to "Shown" Event.
Private Sub Homefrm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Using fp = New dataChangefrm(m_database)
If fp.ShowDialog() = DialogResult.OK Then
uwgHome.DataSource = Nothing
loadData()
End If
End Using
Me.Location = New Point(0, 0)
loadData()
End Sub
Please have a look on the Handle in the first line. It depends on your Project.
I'm taking some random print through the html and web browser control in vb.net winforms
Here is my code
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myWebBrowser As New WebBrowser
AddHandler myWebBrowser.DocumentCompleted, AddressOf DocumentCompleted
myWebBrowser.Navigate("http://www.bing.com")
End Sub
Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
With DirectCast(sender, WebBrowser)
If .ReadyState = WebBrowserReadyState.Complete Then
.Print()
End If
End With
End Sub
End Class
I want the form to be closed after the print. Now if I write Me.Close() after .Print() nothing is being printed. what should I do to achieve this?
Any help is appreciated.
::Update::
after #Noseratio's suggestion I tried to handle the event onafterprint in my html and tried to invoke Me.Close() using ObjectFoprScripting set to my form. But that is firing the close method without any print.
Here is my code
script tag in my html page
<script>
function window.onafterprint() {
window.external.Test('called from script code');
}
</script>
VB.net Code of my form
Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WebBrowser1.AllowWebBrowserDrop = False
WebBrowser1.IsWebBrowserContextMenuEnabled = False
WebBrowser1.WebBrowserShortcutsEnabled = False
webBrowser1.ObjectForScripting = Me
WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
End Sub
Public Sub Test(ByVal message As String)
MessageBox.Show(message, "client code")
Me.BeginInvoke(DirectCast(Sub() Me.Close(), MethodInvoker))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Print()
End Sub
End Class
Found my solution
No need to handle onafterprint in javascript.
Here is what I did,
Step 1
Added a reference to SHDocVw.dll in my project. This can be found in c:\windows\system32 folder.
Step2
My new updated code
Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WebBrowser1.AllowWebBrowserDrop = False
WebBrowser1.IsWebBrowserContextMenuEnabled = False
WebBrowser1.WebBrowserShortcutsEnabled = False
WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
End Sub
Private Sub WebBrowser1_DocumentCompleted_1(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim wb As WebBrowser = TryCast(sender, WebBrowser)
Dim ie As SHDocVw.InternetExplorer = DirectCast(wb.ActiveXInstance, SHDocVw.InternetExplorer)
AddHandler ie.PrintTemplateInstantiation, AddressOf IE_OnPrintTemplateInstantiation
AddHandler ie.PrintTemplateTeardown, AddressOf IE_OnPrintTemplateTeardown
'Just to get reference of the webBrowser1 control in ie events, uncomment the below line
'ie.PutProperty("WebBrowserControl", DirectCast(wb, Object))
wb.Print()
End Sub
Private Sub IE_OnPrintTemplateInstantiation(pDisp As Object)
' The PrintTemplateInstantiation event is fired when the print job is starting.
End Sub
Private Sub IE_OnPrintTemplateTeardown(pDisp As Object)
' The PrintTemplateTeardown event is fired when the print job is done.
'Just to get reference of the webBrowser1 control, uncomment the below line
'Dim iwb2 As SHDocVw.IWebBrowser2 = TryCast(pDisp, SHDocVw.IWebBrowser2)
'Dim wb As WebBrowser = DirectCast(iwb2.GetProperty("WebBrowserControl"), WebBrowser)
Me.Close()
End Sub
End Class
I have a TextBox1 in Form1. I need to pass it to another class (in class libraries/another project). So, that instance of class can modify what inside TextBox1 within the entire class (not just a scope). For my problem i need to pass it to an event handler.
Public Class TheClass
WithEvents Timer1 As New Timer
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
End Sub
End Class
I can think of passing by reference. But, I can't find a way to pass TextBox1 to that event handler.
What should I do to make the Timer1 have access to modify TextBox1 in Form1?
If TheClass is created after Form1, you can use constructor injection in order to pass a reference of Form1 to TheClass.
Public Class TheClass
Private WithEvents Timer1 As New Timer
Private m_form1 As Form1
Public Sub New (ByVal form1 as Form1)
m_form1 = form1
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
m_form1.TextBox1.Text = "tick"
End Sub
End Class
Note that TextBox1 must be Public or Friend.
Another way is to add a public event to TheClass.
Public Class TheClass
Public Event Tick()
Private WithEvents Timer1 As New Timer
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
RaiseEvent Tick()
End Sub
End Class
Now the form can handle this event.
' In Form1
Private WithEvents theObj As New TheClass
Private Sub theObj_Tick() _
Handles theObj.Tick
Me.Textbox1.Text = "tick"
End Sub
Now you can keep Textbox1 private and TheClass does not need to know anything about a textbox.
The event handler can also have parameters
' In TheClass
Public Event Tick(ByVal counter As Integer)
...
Counter += 1
RaiseEvent Tick(Counter)
and
' In Form1
Private Sub theObj_Tick(ByVal counter As Integer) _
Handles theObj.Tick
Me.Textbox1.Text = "counter = " & counter
End Sub
I am just starting out with Visual basic .Net.
I can't seem to figure what's the scope of button properties like button.text. Can they be used outside the button_click event sub? And if so, how?
How can I modify button properties from a module in real time when a certain condition is met?
I'd surely appreciate some guidance and an example, if possible. Thanks.
Just as quick sample, I don't suggest doing something like this
I have 2 forms open, Form2 and Form3. Each form has a button on it.
I also have a Module, called MyModule
Public Class Form2
Public Sub ChangeButtonText(ByVal s As String)
Button1.Text = s
End Sub
End Class
.
Public Module MyModule
Sub ChangeForm2Btn()
Form2.ChangeButtonText("LOL")
End Sub
End Module
From my Form3 I click the button and call the module function to change Form2 button's text
Public Class Form3
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyModule.ChangeForm2Btn()
End Sub
End Class
You could pass a reference to the button to a sub in the module, and then call that sub from the form.
i.e.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeButtonText(Me.Button1, "Changed")
End Sub
End Class
Module modButton
Public Sub ChangeButtonText(ByRef Button As Button, ByVal Text As String)
Button.Text = Text
End Sub
End Module
I have the following code in a form called Fetch.vb:
Imports System.ComponentModel ' might not be needed?
Imports System.Threading
Public Class Fetch
Public Sub New()
InitializeComponent()
backgroundWorker1.WorkerReportsProgress = True
backgroundWorker1.WorkerSupportsCancellation = True
End Sub
Private Sub btnFetch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFetch.Click
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Text.Length = (Not 0) Then
tbList.Add(ctrl.Text)
MsgBox(tbList.Item(0).ToString)
Exit For
End If
End If
Next
' ProcessLinks()
btnFetch.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Public Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
AddHandler BackgroundWorker1.DoWork, AddressOf backgroundWorker1_DoWork
ProcessLinks()
End Sub
End Class
Now process links is a module with a public sub in with the code I am trying to run, I don't need any arguments passed to it, and it doesn't do anything that (I think) could affect this, I think I'm just doing the threading code wrong. I have backgroundworker1 in my fetch.vb form, and when I click btn Fetch, the program does nothing.
Any help and guidance or reading material would be greatly appreciated.
EDIT Here is my LinkProcess module.
Public Module LinkProcess
Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Fetch.BackgroundWorker1.DoWork
ProcessLinks()
End Sub
Public Sub ProcessLinks()
Dim tbContent As String
For Each tbContent In Fetch.tbList
Process.Start(tbContent)
Next
End Sub
End Module
What does your ProcessLinks() method do? Is it accessing any UI elements - like setting some text in label, or adding text a TextBox, etc.? If that is the case, then that won't work. You should not access the UI elements from inside your BackgroundWorker DoWork.
Here is a small post I wrote about how to use the BackgroundWorker correctly. This might help you.
http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker