Form Inheritance in Visual Studios designer implementations - vb.net

I'm in the process of moving a project from Visual Studio 2003 to 2005 and have just seen the
The event Click is read-only and cannot be changed
when using inherited forms regardless of the Modifier on the Base Forms Controls will make all the Controls from the Base Readonly in the designer (Though in 2003 it didn't work this way).
I found this post mentioning that this functionality has been temporarily" disabled
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/c25cec28-67a5-4e30-bb2d-9f8dbd41eb3a
Can anyone confirm whether this feature is used anymore? Or how to program around it to be able to use the Base Control Events and still have a designer?
This is one way I've found but quite painful when it used to do the plumbing for you. Even just hiding one of the controls you have manually do now.
Public Class BFormChild
Friend Overrides Sub cmdApply_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.cmdApply_Click(sender, e)
End Sub
Friend Overrides Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.cmdCancel_Click(sender, e)
End Sub
Friend Overrides Sub cmdOk_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.cmdOk_Click(sender, e)
End Sub
End Class

Base classes that generate events require the standard event generation pattern. That must be done in code, the designer cannot auto-generate it. It never will.
Public Class BFormBase
Public Event ApplyClicked As EventHandler
Protected Overridable Sub OnApplyClicked(ByVal e As EventArgs)
'--- Possible default implementation here
'...
RaiseEvent ApplyClicked(Me, e)
End Sub
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
OnApplyClicked(e)
End Sub

Related

Call function outside user control

I need help to call a function in a usercontrol that is shown in a panel within the form, so far this is i tried, but no luck, i can't still get the text inputted on the textbox
Public Class Form1
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Dim ustudent As New StudentAdd
ustudent.Dock = DockStyle.Fill
SplitContainer1.Panel2.Controls.Add(ustudent)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
ustudent.SaveData()
End If
End Sub
End Class
in user control have some textbox
textbox1 and textbox2
Public Class StudentAdd
Public Sub SaveData() As Boolean
'just testing whether it could work well
' getting textbox value
MsgBox(TextBox1.Text)
End Sub
End Class
But ustudent is a local var, try to declare it outside link_clicked event. Do you want to create multiple user controls in the win or just one?
For one you could add it at design time by dragging from the your project components panel
For more, you should implement some logic to identify the selected component and make it available for save data. If you want to save all just enumerate components in Panel2 (of type StudentAdd) and call the method

using GoogleEarth plugin from VB.NET through FC.GEPluginCtrls

I'm really looking for a simple way to build VB.NET apps that use the GEplugin. So I have found this project that seems to do the dirty job I need: http://code.google.com/p/winforms-geplugin-control-library/
Well, all the code posted there around works on C#, but I need to have it on VB.NET. So I have tried this:
created a new 32-bit solution from VB.NET 2010 Express (I simply added
<PlatformTarget>x86</PlatformTarget >
inside the .vbproj file)
added a reference to FC.GEPluginCtrls.dll
inserted a GeWebBrowser control on the form
at the top of the code, added
Imports FC.GEPluginCtrls
then, in the form Load event, put this code:
InitializeComponent()
GeWebBrowser1.LoadEmbeddedPlugin()
Do
Loop Until GeWebBrowser1.PluginIsReady = False
GeWebBrowser1.CreateInstance(ImageryBase.Earth)
that, I think, would be equivalent to
http://code.google.com/p/winforms-geplugin-control-library/wiki/CreateInstance
So, the project compiles and doesn't get errors, but the GeWebBrowser control remains empty.
I actually wrote the library you are using. You are not listening for the PluginReady event. http://code.google.com/p/winforms-geplugin-control-library/wiki/PluginReady
To use it with VB simply convert the basic examples to VB -
http://code.google.com/p/winforms-geplugin-control-library/wiki/ExampleForm
Also, a loop polling PluginIsReady is totally unnecessary as the PluginReady event is asynchronous.
To show the earth all you would need is the following.
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin()
End Sub
To use the plugin when it has initialzesd use the PluginReady event. Something like.
Option Strict Off
Public Class Form1
Private Dim _ge as Object = Nothing
Private Sub GeWebBrowser1_PluginReady( ByVal sender As System.Object, ByVal e As FC.GEPluginCtrls.GEEventArgs) Handles GeWebBrowser1.PluginReady
_ge = e.ApiObject ' reference to the Google Earth Plugin object
MessageBox.Show(_ge.getApiVersion()) ' _ge is the plugin -use it just as in the javascript api...
End Sub
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin() ' load the plugin
End Sub
End Class

Multi parent form

I've a vb .net winform that is show by others forms. I've frmA.vb, frmB.vb, frmC.vb and frmD.vb.
This all forms can call frmItem.vb.
frmItem.vb allows the user to select an item from a Database, this item is sent by calling a Set Property on the parent.
i.e.
I open frmA, click on button (something like:)
fi = new frmItem(frmA) 'frmItem has 4 New() methods, frmA.. b... c and d
'i need to pass the correct parent.
fi.showModal()
So, when i add an item, it calls
fA.addItem(item_id)
It works OK, my doubt is about optimization, because i've duplicated frmItem; one copy managed frmA and frmB, and the other one, frmC and frmD.
i.e.
in frmItem1 when i've to sent the item, i use:
private fB as frmB
private fA as frmA
if parentFrmA is nothing then
'Is frmB
fB.addItem(item_id)
else
'Is frmA
fA.addItem(item_id)
end if
And, on frmItem2:
private fC as frmC
private fD as frmD
if parentFrmC is nothing then
'Is frmD
fD.addItem(item_id)
else
'Is frmC
fC.addItem(item_id)
end if
If i modify frmItem1, i've to modify frmItem2 and viceversa, because they should look and act like one.
All four forms, have the same Set Property, but like they're differents forms, i can't use a unique Form class in frmItem.
Is the posibility that one form, can manage multi parents in an easy way??
If you need more info, let me know. Thanks
I can't completely follow your example since, well, I think it's just hard to follow.
But in general, it sounds like these child forms should just be raising an event that the parent form is listening for. That way, you can separate your concerns a bit and not hardcode these dependencies.
You can try making your own EventArgs class to follow best practices:
Public Class ChildFormEventArgs
Inherits EventArgs
Private _ItemID As Integer
Public Sub New(ByVal itemID As Integer)
_ItemID = itemID
End Sub
ReadOnly Property ItemID() As Integer
Get
Return _ItemID
End Get
End Property
End Class
Your child forms would have a public event and you would raise it when ever this "added" thing happens:
Public Class Form2
Public Event ItemAdded(ByVal sender As Object, ByVal e As ChildFormEventArgs)
Private _ItemID as Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RaiseEvent ItemAdded(Me, New ChildFormEventArgs(_ItemID))
End Sub
End Sub
And then your parent form is the one listening and can act accordingly:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Using testForm As New Form2()
AddHandler testForm.ItemAdded, AddressOf ChildForm_ItemAdded
testForm.ShowDialog(Me)
RemoveHandler testForm.ItemAdded, AddressOf ChildForm_ItemAdded
End Using
End Sub
Private Sub ChildForm_ItemAdded(ByVal sender As Object, ByVal e As ChildFormEventArgs)
'// do something here.
'// sender is the child form that called it
'// e is the event arguments that contains the ItemID value
End Sub

VB.net Webclient.DownloadFileAsync Not Downloading

I am trying to make a download manager for my program. But when I run this code it gives me the message box say "Download Started" but that is it. I do not get any file downloaded or progress bar change? Does anyone know why?
Public Class frmDownloader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebClient1.DownloadFileAsync(New Uri("https://s3.amazonaws.com/MinecraftDownload/launcher/Minecraft_Server.exe"), "C:\hi.exe")
MsgBox("download started")
End Sub
Private Sub WebClient1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
MsgBox("Download Progress Changed")
End Sub
End Class
The DownloadProgressChanged method is invoked on a different thread than the one that started the download. Inside this callback you seem to be manipulating some GUI element: ProgressBar1.Value. You should never manipulate GUI elements on different threads than the one on which they were created or you might get an exception. Depending on the type of application you are working on there are different ways to marshal calls on the GUI thread. For example in WinForms you should use the Control.BeginInvoke method. In WPF and Silverlight the equivalent is the Dispatcher.BeginInvoke.

How to get event name in vb.net?

Here there are two handlers in a particular procedure then how to get which event handler has performed.
for example
Private Sub TextBox1_Events(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.GotFocus
End Sub
how to get which event has occured.
It is possible using the StackTrace (could be a better way I'm not sure...). Try the following code.
Private Sub TextBox1_Events(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.GotFocus
Dim s As New StackTrace(True)
For Each f As StackFrame In s.GetFrames
Debug.WriteLine(f.GetMethod.Name)
Next
End Sub
When the text box gets focus the following is written:
TextBox1_Events
OnGotFocus
OnGotFocus
WmSetFocus
Ect…….
Where as when it’s a text changed event
TextBox1_Events
OnTextChanged
OnTextChanged
Ect….
I’m sure you could write something using this to do what you need. But i fully agree with the other guys separate handlers is better.
In this case, you cannot.
If the events were bound to two separate controls, you could check the sender property for the type
If the e argument for the event had some type other than EventArgs (some events use a different arguments type), or the control passed some type derived from EventArgs, then you might be able to check properties on that variable
There aren't any other tricks you could use because events don't provide any sort of data to the handler specifying which event occurred.
With these two events, they're both going to be sent from the same text box, so the first option is out. Also, with both events, they send just an instance of the EventArgs class (not a derived class), so that option is out.
Ultimately, you're going to have to have multiple event handlers to solve this specific problem.
It's not possible. If you're in a situation where you need to know which event occurred, you will always be better off using two separate handlers.
Since you are dealing with 2 events (similar in signature) emitted by the same control the easiest/cleanest way of solving this would be 2 separate event handlers (as suggested by Merlyn Morgan-Graham):
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'the TextChanged specific code would go here
HandletTextBox1EventInternal(sender, e)
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
'the GotFocus specific code would go here
HandletTextBox1EventInternal(sender, e)
End Sub
Private Sub HandleTextBox1EventInternal(ByVal sender As System.Object, ByVal e As System.EventArgs)
'code common to GotFocus and TextChanged handlers
End sub