Inform programmer with a message when about to use a Usercontrol - vb.net

First of all I am working with Visual Studio 2017. I have create a UserControl which requires a borderless form. Is there any way to popup a message if programmer try to place this control into a non-borderless form? Something like the message box which appears in design view when we give a wrong value at Properties Window of any control.
EDIT
I noticed that if I add a simple MsgBox into my UserControl's Public Sub New and then add this UserControl to a Form, message box appears.
Public Sub New()
InitializeComponent()
MsgBox("Test Message")
End Sub
But, how can I check if parent form is borderless or not? Something like this example below, which of course doesn't work inside Public Sub New because there is no parent yet!!!
Public Sub New()
InitializeComponent()
if Not MyBase.ParentForm.FormBorderStyle = FormBorderStyle.None Then
MsgBox("Test Message")
End If
End Sub

Well after some tests I managed to solved it...
Into UserControl's Load event (where ParentForm is acceptable), I check first if UserControl is in DesignMode (else user will gets messages everytime application starts) and then, if ParentForm is not FormBorderStyle.None, user informed by a MessageBox.
Private Sub UserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not Site Is Nothing AndAlso Site.DesignMode Then
If Not ParentForm.FormBorderStyle = FormBorderStyle.None Then
MessageBox.Show("This control works better with a borderless form!!!", "Control Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
End Sub

Related

Cannot view Designer

Please help! I can not view the Control Parameter Adjustment Form in my VB project, other three forms are normal.
How can I fix this problem?
Here is the Designer.vb code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Control_Parameter_Adjustment
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.Label31 = New System.Windows.Forms.Label()
......
End Class
At first, I apologize for my bad english.
This worked for me: If you add an empty form to your project, you can double clik on it, and you can see the following:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
You can't see that class definition in the other form, probably because this class have dissapeared (I can't explain you why).
Also and probably, you can find the form_Load event in the form code.
Simply rewrite this class, changing the Form Name in class definition. Then, you can see the designer. The icon also change.

Closing Window's from upon switching to another or vice versa

I wrote a Window Application which has two different parent-forms (form1, and form2). Each form has several child forms. After I login it opens form1. Now I have a button (called switch to form2) on form1, which switch to form2. Now I need to close form1 after opening form2. I need to same thing from form2 to form1.
What would be the best way to handle this.
I tried something like below by adding this code to close form under form load of each forms, but I am getting the following exception.
A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional information: Collection was modified; enumeration operation may not execute.
Can you please suggest me to handle this problem?
Form 1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each frm As Form In Application.OpenForms
If frm.Name.ToLower = "form2" Then
frm.Close()
End If
Next
End Sub
Form 2
Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each frm As Form In Application.OpenForms
If frm.Name.ToLower = "form1" Then
frm.Close()
End If
Next
End Sub
I suppose you are responsible for creating your forms.
If so, I would overload the constructor of each of the forms to take a parameter of the type of the other form and close it there.
For your Form1 it would be :
Public Sub New(form2 As Form2)
InitializeComponent()
'and the rest of your initialization code
If form2 IsNot Nothing Then
form2.Close()
End If
End Sub
Vice versa for the constructor of Form2.
It doesn't even have to be so specialized like above. You could always generalize it to take an object of the type Form as a parameter.
why not use Form1.Hide()?
that way you can still access the other form while it is not visible.
I hope this helps.

Create And Implement a Custom Form in VB.NET

I am trying to create a customized form class (CustomBorderlessForm) in VB.NET.
My Progress So Far
I created a new Class and named it CustomBorderlessForm.vb
I then proceeded to write the following code:
CustomBorderlessForm.vb
Public Class CustomBorderlessForm
Inherits Form
Dim _form As Form = Nothing
Public Sub New(form As Form)
_form = form
MsgBox("Testing: New()")
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
MsgBox("Testing OnMouseMove()")
End Sub
End Class
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
End Sub
End Class
Results of progress
A message box displays "Testing: New()" on load
Nothing shows on mouse move
As you can see, my problem lies with the events
Questions
Is it possible to create a form object and use that instead of the pre-populating form?
If so, can I give this form custom properties, such as, a border and some boolean values (shadow...etc), just like any other custom object/class?
What am I doing wrong in my current approach?
Why isn't the OnMouseMove being overridden?
Am I initialising the class wrong?
Can it even be done this way?
After creating a form you also need to show it. Change your logic to:
Dim form As New CustomBorderlessForm(Me)
form.Show()
Before you do that, I'd recommend changing from MsgBox to Console.WriteLine(), otherwise you can run into a fun/frustrating little cat and mouse game.
EDIT
Based on the comments, if, from VS you did a "Add New, Windows Form" you can just right-click the project, select property and on the Application tab change the Startup object to your new form. VS only allows you to do this with forms it creates for you (by default, more on this later).
If you wrote that file by hand (which is absolutely fine) you can perform the Show() like I did above and call Me.Hide() to hide the "parent" form. Unfortunately the Load event is fired before the Show event so if you place this in Form1_Load() it won't work. Instead you can use the Shown event like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
form.Show()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Hide()
End Sub
Another option has to do with "Application framework". You can read about it here however it basically handles application events that other languages have to manually implement. If you go into your project properties you can uncheck the "Enable application framework" checkbox. This will give you more option in the "Startup object" dropdown. If you add the following code to your project one of the items in the Startup object dropdown menu should now be "Loader"
Public Module Loader
<STAThread()>
Public Sub Main()
Dim form As New CustomBorderlessForm(Nothing)
form.ShowDialog()
End Sub
End Module
You'll notice that the above bypasses Form1 completely. Also, instead of Show() I'm using ShowDialog() because otherwise the form shows and then the program ends.

Vb.Net - Accessing text in controls on another form

I am fairly new to vb.net and would like to be able to access the value (such as .text on a textbox) from another an open form. In my application I open a form from my main form, and when I try to access the text in the controls on the main form I am unable to see the .text value on the control.
I can loop through all controls on the main form just fine, but when I want to see the actual values, all controls are empty. My controls such as text boxes and combo boxes are inside of a tab control and group boxes.
Is there a way to make all .text or values on the open form available from the other open form?
Here is how I am looping through the controls on the main form.
Try
For Each Tp As TabPage In UserData.UserTabControl.TabPages
'Name of Tabcontrol is UserTabcontrol
For Each gbx As GroupBox In Tp.Controls
For Each ctrl As Control In gbx.Controls
If ctrl.Name = "UserName" Then
MsgBox(UserData.UserName.Text) 'Messagebox here is empty
End If
Next ctrl
Next gbx
Next Tp
Me.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Thanks in advance.
Chris
From the example you've given you already have access to a reference to your Control. Instead of going back to the Form and trying to access that control as a property of the Form you could just cast your reference and call its Text property directly.
If ctrl.Name = "UserName" Then
MsgBox(DirectCast(ctrl, TextBox).Text) 'Assuming your UserName control is a TextBox
End If
If you would like to reference controls on an open form, call it Form1:
First add a Form1 property or variable to the calling form:
Public Class Form2
Public Property f1 As Form1
...
Private Sub DoSomething()
MsgBox("Here's some text from Form1: " & f1.Textbox1.Text)
End Sub
End Class
In the callee form, set the Form2 property to the form object:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.f1 = Me
Form2.ShowDialog() ' or Form2.Show()
End Sub
End Class
You can then reference all Form1 objects from Form2 using the f1 property.
By this command, I could change the amount of control in another form.
My.Forms.myForm.labelControl.Text = "bela"
Please try this :
MsgBox(My.Forms.UserData.UserName.Text)

VB.net - Derived class from MenuStrip

What I am trying to do is make a custom MenuStrip control with several items (Main Menu, Log Out, Exit, etc...) already attached. There would be methods to handle items being clicked. I think this would save me a some redundant code in the long run and I might learn a little something too.
The end product would basically be a custom MenuStrip control that I can throw on my forms and already have the functionality for the items within it.
So my question is, can this be done? I am a novice but if it can be done and is actually a good idea, then I want give it a shot.
Errors abound but this is what I was thinking...
Public Class MenuStripCustom
Inherits MenuStrip
Add MenuItem(MainMenuToolStripMenuItem)
MainMenuToolStripMenuItem.Text = Main Menu
Protected Sub MainMenuNav(e As System.EventArgs) _
Handles MyBase.MainMenuToolStripMenuItem.Click
MainMenu.Visible = True
Me.Close()
End Sub
End Class
Thanks!
Yes, can be done no problems. Just create a new user control, and make it inherit from MenuStrip. Then put in code similar to the below for a user control called "UserControl1".
Public Class UserControl1
Inherits MenuStrip
Private WithEvents NavToolStrip As New ToolStripMenuItem("Nav")
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim tsi As New ToolStripMenuItem
Me.Items.Add(NavToolStrip)
End Sub
Private Sub NavToolStrip_Click(sender As Object, e As EventArgs) Handles NavToolStrip.Click
MsgBox("Nav clicked")
End Sub
End Class
Compile the code then you will be able to drag "UserControl1" from your toolbox onto your form.