Is it possible to have one file that controls the look and feel of my custom title bar that I can access from any form? - vb.net

I have an application with 60+ forms and instead of having 60+ copies of the same code in each form, I would like to have one instance of the code that is executed as any form is loading.
The subs are standard:
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Protected Overrides Sub WndProc(ByRef m As Message)
I've made countless attempts using classes and modules, but am only able to get the forms that are hardcoded to work as desired.
I have triggered the OnPaint event residing in TitleBar (a non-form class), but I am unable to get TitleBar to apply any customizations to any form. Is there anyway I can circumvent the values in TitleBar's Me and MyBase, and assign them the calling form's values? Or can I get TitleBar to loan out its resources? Any help would be greatly appreciated. BTW, is there a word or phrase for what I'm trying to accomplish?
This has been a long round of trial and error.
Thank you

Create a form as normal and add that common code. In all your other forms, instead of inheriting the standard Form class, inherit the class you just created that contains the common code. That's how inheritance in OOP works.
If you're inheriting a form from a referenced library, you can select Inherited Form in the Add New Item dialogue but that doesn't work to inherit forms in the same project. At least, it doesn't work in an application project, although I haven't tested in a library project. The alternative is to add the form as normal, click the Show All Files button on the toolbar in the Solution Explorer, expand the node for your form, double-click the designer code file to open it and then manually edit the type that your form inherits from, e.g. change this:
Partial Class Form2
Inherits System.Windows.Forms.Form
to this:
Partial Class Form2
Inherits Form1

Related

Sharing Controls from multiple forms/classes

I'm currently stuck in how to share controls between two forms.
This is what i want to do:
- I have several forms that have their own function.
- Now i want to bring them all to gather on one mainform.
Normally i do this by creating the following code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim f As New Form2
GroupBox1.Controls.Add(f.Get_Groupbox_controls)
End Sub
End Class
This add all the control containing in Form2.Groupbox1 and adds it to Form1.Groupbox1.
Normally this works just fine for me.
Where "Get_Groupbox_controls" is a function that returns Form2.Groupbox1
Or if this doesn't works i display the entire form2 over form1 with a transparancykey. (this isn't a need solution, you have to do a lot of location calculating, but it works)
However now i got a form that has a lot going on. For the mainform i only want to display one Container(groupbox) but when the user clicks "More setting.." then it has to show the entire form with all the control on it.
The problem is when i use the container.controls.add() function i get an exception "Invoke or BeginInvoke cannot be called on a control until the window handle has been created "
The Error is logical because the entire form isn't made yet. But in order for the form to work properly it has to update the "hidden" UI controls (labels/buttons/etc)
So the main question is, how can i "port" a selection of controls from Form1 to form2 and keep all the handlers etc.. on Form1
If you have two forms where you want to place identical looking/functioning GroupBox sections, you may want to consider creating a custom user control that contains the shared functionality and can be placed in each form. User controls can even be added to a form through the form designer in Visual Studio.

VB.NET: How can you activate the childform when only the control inside is clicked?

*edit: OK, so this is my real problem, below scenario happens only when the form is MDIChild.. thanks for anyone that could provide me with the code
I have a form with labels, panels, buttons etc. Where I'm having problem is, while form2 is my active window/form and I clicked on a control inside form1, the form1 does not activate itself. What I would like to happen is for form1 to activate even when it's not the form I clicked, only the control inside it (any control)..
I'm thinking that if I clicked a control on the form, there's an event fired on the form. If I could only know of that certain event, that would help - maybe (coz I could just add Me.activate on that event if it exists). I've tried searching for series of events when a control (ex. label) is clicked but to no avail. I hope that someone could help me with this one.
Thanks in advance.
*edit
i will just try to make my question more understandable..
How can I activate the form when only the control is clicked (say, label or textbox)? My forms does not activate or focused when I click inside it except the form itself..
I can do this on one control..
Private Sub Label1_Click - Handles Label1.Click
Me.Activate()
End Sub
But what if I have 20 controls (labels, buttons, textbox, combobox, etc)? See? =)
EDIT: this answer does not apply to MDI applications.
I think what you really want to know is which one of your forms is currently the foreground window (if any). The first thing you need to understand is that a form instance lives inside a window, but the window's behavior is controlled somewhere higher up. Similar to how a form instance is identified by a variable pointing to the instance, a window can be identified by what's known as a window handle.
Knowing this, the proper way to find out whether a form is the "active" form is to:
find out the window handles of the windows containing our instances of Form1 and Form2
find out the window handle of the foreground window (which can be any window)
compare the value found in step 2 to all of the values found in step 1
Perhaps you'd then like to fire an event if the foreground window changes, but I'll leave the actual implementation up to you. There are probably several ways to perform step 1 and 2, but I can't give any solutions off the top of my head. Hopefuly I've put you back on the right track.
EDIT
Alternatively, you can use the form's Containsfocus property. If its value is True, you can safely assume that your form is the foreground window. I didn't find out about this property until after I wrote my own implementation, which I'll show you anyway:
One module containing only a windows API call
Friend Module NativeMethods
Friend Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
End Module
Calling this method will return the window handle of the foreground window (if any).
One module containing the extension method for the Form class
Imports System.Runtime.CompilerServices
Public Module FormExtensions
<Extension>
Public Function IsForeground(f As Form) As Boolean
Return (f.Handle = NativeMethods.GetForegroundWindow)
End Function
End Module
Calling this method returns whether the specified form f has the same window handle as the foreground window.
Usage example
You could use a Timer that periodically checks whether a form is the foreground window.
Public Class Form1
Private WithEvents timer As New Timer With {.Enabled = True}
Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
If Me.IsForeground() Then
Console.WriteLine("this instance of Form1 is the foreground window")
End If
End Sub
End Class
Like I said before, you can use Me.ContainsFocus instead of my extension method and it will work just fine.
In non-MDI forms, the form is automatically activated when you click any control inside it.

Non visual inheritance of WinForms with vb.net

I have two Forms, which have similar Functionality (i.e. an amount of similar Controls) but complete different Layout. So the normal inheritedForm (which VS2010 provides) wont work here.
I have tried following:
Public Class BaseForm
Inherits System.Windows.Forms.Form
Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
And in Form1.Designer.vb:
Partial Class Form1
Inherits BaseForm
...
<all Designer generated Code>
...
' Friend WithEvents Button1 As System.Windows.Forms.Button <- remove this Line
End Class
If i compile/execute this, the test form works as expected; But now I am unable to design the form any longer.
If I switch to Design-Mode, it says:
The variable 'Button1' is either undeclared or was never assigned.
So it looks like, the Designer only tries to guess how the Form looks like by inspecting the top-most Class, without compiling the full inheritance-Tree...
Does anyone know a workaround for this?
Thx,
Daniel
I have a feeling you have some existing controls on the base form which you want to bring to the inherited form and then add more functionality of your own. Remember one thing, inheritance in VB.NET is by default set to shadowing, so you can redefine code in your inherited page for the same button to do a different function.
However, I think you forgot the Overridable keyword when declaring the form elements in the parent form. This will help the compiler know that the form can be overridden and allow you to define custom functionality.
Remember, all controls and pages in VB.NET are classes and whatever we learned during OOP classes in college apply here.

How can I hide the inherited menu strip?

I've got a form that needs to inherit a base form. The problem is that the base form has a menu strip. The form I'm working on should not have any kind of menu on it.
How can I hide the inherited menu strip? Alternatively, how can I exchange the inherited menu for something I construct myself in the new form?
In the base form class, extract the menu strip creation code from InitializeComponent() into a virtual method, and then override that method in the new form. The drawback of this is you lose visual designer support for the menu in the base form.
I found another way, which I actually ended up using.
If you don't need to show the menu again in that particular form, you can initialize all the components as usual. Then simply make a public method like HideMainMenu() in your base class, that you call after all Init's are run, which only does this:
Public Sub HideMainMenu()
Me.Menu = Nothing
End Sub
You could probably show the menu again by doing
Public Sub ShowMainMenu()
Me.Menu = Me.myPrivateMainMenu
End Sub
But I haven't tried that...

Creating a Partial Class for a Form

I would like to create a partial class for my form. I have a lot of events, and it gets messy, so I would like to break up sections into their own files.
The problem: When I create a partial class of my form, say:
Partial Public Class Form1
End Class
Visual Studio decides I need another form for this partial class.
Questions:
1. How do I create a partial class for a form?
2. If I cant do that, how can I break up all the events in my form into different files?
Yeah, it does. As soon as you drop a control on this phantom form, you'll get the design-time code (InitializeComponent) generated into that source code file. This is compatibility behavior for .NET 1.x, it didn't support the Partial keyword. Which will break the build, there are now two of them. It is somewhat avoidable with careful clicking, but you know it's going to happen sooner or later.
Other things go wrong too btw, the designer can no longer track an event handler when you move it from one file to another. And will readily let you add another, a much trickier source of bugs.
This just doesn't work very well, abandon hope of relying on it to solve your problem.
The generic diagnostic is that a convoluted user interface begets convoluted code. But that ship has sailed, no doubt. A more structural solution is pursuing the MVC model, separating the data from the view of the data. You'll still have a lot of event handlers but they won't do anything more than calling a method of a class that does the real work. Whose source code can of course live in another source code file. The typical hangup is that Windows Forms has no support whatsoever built in for this, you have to craft it by hand. Nothing similar to the MVVM model in WPF.
Something that can work well is isolating control + code into a separate UserControl. You have to do so carefully though, you don't want to have to add a bunch of properties and events that expose internal controls.
Sometimes I create partial classes for better readibility, especially when I have very large classes.
But when I click on the partial class, then the VS IDE will open the form editor showing me an empty form. If I do not care, than I could damage the main form (it seems to be a bug of VS 2008/2010)
A possibility could be using DesignerCategoryAttribute Class
Mark the partial class with the attribute "code".
<System.ComponentModel.DesignerCategory("code")>
Partial Class Form1
End Class
In this way when you click on the file, you open the class in the code editor.
Of course this will apply to all files, also to the main form file.
If you want to edit again your form in the form editor, you have to quote the attribute:
'<System.ComponentModel.DesignerCategory("code")>
Some more details here.
While it does not answer the original question, I found using regions made my code a little more manageable/readable.
#Region "RegionA"
#End Region
I orginally called this method a "hack", thus the comment below.
Not sure what you mean be "Visual Studio decides you need another form", however, are you sure the new Form1 partial class is declared in the corresponding original namespace?
All partial classes for a given .NET type must be declared in the same namespace of course (whatever files they're stored on).
I appreciate the answers given by Hans and I'm not disputing these at all. It is curious though that in Visual Studio 2010, when you create a form called say Main you get a Main.designer.vb which is a partial class. It says 'Partial Class Main' at the top. This class doesn't open a form when clicked. It also includes reference to Event Handlers. So I was wondering how do they get around this? Is there a way to create one of these 'special' partial classes that work as we would expect.
I noticed that when I created a Form Partial class, that the icon went from a class icon to a form icon. The icon associated with the Main.designer.vb file looks like a class icon with a arrow.
what worked for me (VS 2010) was naming Form1 class, already saved in Form1.vb with its own designer (Form1.Designer.vb) as:
Public Class Main 'saved in Form1.vb
VS updated the name in the designer as:
Partial Class Main 'saved in Form1.Designer.vb
then I created another "partial class" with the same name:
Partial Class Main 'saved in Main.vb
Whether I am editing Form1.vb or Main.vb VS shows me on the top navigation pan all the routines, functions, subs, even background workers and timers. For event handlers, to avoid the loophole mentioned earlier (you click on a control in the layout designer and a brand new event handler will be created in the original Form1.vb) I go:
Partial Public Class Main 'in Form1.vb file
Private Sub SomeControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SomeControl.Click
Call SomeControlClick(sender, e)
End Sub
End Class
Partial Public Class Main 'then in Main.vb file
Private Sub SomeControlClick(ByVal sender As Object, ByVal e As System.EventArgs)
'blah blah
End Sub
End Class