Project level conception of modal and nonmodal forms - vb.net

I have project which consist of several forms and want to open it in certain modality rules which I can't achieve.
First, here is main form "Form1", then "Form11" and "Form111", "Form12" and "form121"
From main form "Form1" I can start only forms "Form11" and "Form12" like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form11.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form12.Show()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
End Sub
End Class
In this situation, when "Form11" and "Form12" are showed I can easily exit application by pressing Button4 on "Form1" what will close all forms.
Now, here is another form, "Form111" which I open modally by clicking a button on "Form11"...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form111
f.ShowDialog(Me)
f = Nothing
End Sub
And here I have some misunderstanding or misconception of my project.
When "Form111" is opened I like it to block "Form11" but not "Form1" where I would like to (say) open "Form2" or exit application where modal form "Form111" on nonmodal form "Form11" is opened.
Is it possible to achieve such functionality with described project configuration and how?

First, the code for the button clicks in the first block may not be right. If the forms are named Form11 and Form12 that is their class name. They should be instanced as you do with Form111.
The reason the application closes is because that form (Me) is set as the startup form. If/when that closes, the app ends. You can change the app to exit when the last form closes in project properties.
As for your question, to have a dialog "block" "Form11" but not "Form1", the answer is no. Forms are either Modal (what you are calling "blocking") or Modeless. You could tell Form111 to stay on top, but it would not be "blocking" any other form.
What you are trying to do suggests that the operations on these forms may not be as well organized or planned as they need to be.

Related

How do I stop a button from triggering when the enter key is pressed? (focused)

(I would say hello here, but Stackoverflow always removes it for some reason)
My problem is that on my Visual Basic form the button that I use to close the form can be triggered by the enter key. This is a problem because my form opens a webpage that frequently uses forms, meaning that the enter key on a form will close the window!
Therefore, I need to find a way of stopping the focus on this button. It is a custom-ish button and is its own class but inherits from a normal button. Here's some diagrams:
Stage 1: Before the user focuses on the WebKitBrowser
Stage 2: After the user focuses on the WebKitBrowser
As you can see, the button is "focused" with a white border, similar to a normal Windows button where the border is instead blue. If I was to press enter at this stage, the form would close.
In conclusion, or TL;DR, how do I stop my close button from being focused?
Your button by default is included in the tab order which results in the focus effect.
Try removing the button from the tab order. You can do by setting the TabStop of your button to False when your form loads. I tried the example below and it worked for me.
Before
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
After
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.TabStop = False
End Sub
End Class

Vb.NET Windows Forms Focus Event

I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class

VB.NET End application in Form2

Here's my scenario:
I have two forms named Form1 and Form2.
Form1 has a button.
When the button is pressed, Form1 will become hidden and Form2 will be shown.
If I close Form2 by pressing [x] button on the top right of the form, the application is still running.
According what I get in my research, it seems I have to work with FormClosingEventArgs.
Anyone have any idea?
Find Shutdown Mode in your application properties. There you will see two options.
1. When start up form closes.
2. When last form closes.
If you choose no.1 then until you close your start up form your application will not close but you can apply force close. eg.
Private Sub Form2Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
End
End Sub
And if no.2 then your application will close automatically when last active form closes.
Hope this is ok
Seems you are not closing your first form. You are just hiding the first form instead of closing it. You should also close the first form. But, if you are creating an object of Form2 in Form1 then you can't close the first form. if you close it then the Form2 will not be displayed because the object of Form2 will be disposed initially. So, you should handle the FormClosed event of Form2 in Form1 when the Form2 is being displayed.
'Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Dim form As New Form2
AddHandler form.FormClosed, AddressOf Me.form2_FormClosed
form.Show()
End Sub
Private Sub form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
Me.Close()
End Sub
Problem solved.
Private Sub Form2Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub
Please give more suggestion to me if there is another nice way :)

form starting but not displaying vb.net

So I was coding a program that opens 20 web browsers on one page, for a youtube or view bot any URL really, but when I launched the program It displayed the text box I put in to see if it was actually starting and then nothing else
So my question is, Whats wrong with my code?
startup form (where you can launch the main code with button 1)
Public Class Startup
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Settings.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
views.Show()
End Sub
End Class
main form
Public Class views
Private Sub views_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show("Form Initalised")
WebBrowser1.Navigate(My.Settings.url)
WebBrowser2.Navigate(My.Settings.url)
WebBrowser3.Navigate(My.Settings.url)
WebBrowser4.Navigate(My.Settings.url)
WebBrowser5.Navigate(My.Settings.url)
WebBrowser6.Navigate(My.Settings.url)
WebBrowser7.Navigate(My.Settings.url)
WebBrowser8.Navigate(My.Settings.url)
WebBrowser9.Navigate(My.Settings.url)
WebBrowser10.Navigate(My.Settings.url)
WebBrowser11.Navigate(My.Settings.url)
WebBrowser12.Navigate(My.Settings.url)
WebBrowser13.Navigate(My.Settings.url)
WebBrowser14.Navigate(My.Settings.url)
WebBrowser15.Navigate(My.Settings.url)
WebBrowser16.Navigate(My.Settings.url)
WebBrowser17.Navigate(My.Settings.url)
WebBrowser18.Navigate(My.Settings.url)
WebBrowser19.Navigate(My.Settings.url)
WebBrowser20.Navigate(My.Settings.url)
Threading.Thread.Sleep(My.Settings.Time)
reload.Show()
Me.Close()
End Sub
End Class
and form reload mentioned in the main form.
Public Class reload
Private Sub reload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Close()
views.Show()
End Sub
End Class
Try to use the Form.Shown event instead of the Form.Load event. In Form.Load the controls are often not fully initialized and if something goes wrong there the initialization of the whole form sometimes goes awry. It's better to use the Form.Shown for such lengthy procedures you have there.

VB.NET - Handle many textboxes with less code

I got many textboxes (about 10) in a form. I want the text in the textbox to be higlighted whenever it gets foucs. The code for that looks like:
Private Sub txtBillNo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBillNo.GotFocus
HoverText(txtBillNo)
End Sub
Private Sub HoverText(ByRef ctrl as TextBox)
ctrl.SelectAll()
End Sub
It works perfectly, but I though I could do some code optimization here. Since, I have about 10 textboxes (and many other forms containing several textboxes), I have to HoverText(TextBox) in every Private Sub.. Handles TextBox.GotFocus for every textbox in each form.
I look for any form event (or any other way) that is trigerred when focus is given to another control (textbox) within the form, either by MouseClick or TAB so that HoverText(TextBox) is needed to be written only once for a form.
You can list all your textboxes in the Handles clause:
Private Sub atextbox_GotFocus(ByVal sender As System.Object, ByVal e As _
System.EventArgs) _
Handles txt1.GotFocus, _
txt2.GotFocus, _
txt3.GotFocus, _
(...remaining text boxes..)
txt9.GotFocus, _
txt10.GotFocus
Or you can add handlers to all textboxes when loading the form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each t In Me.Controls.OfType(Of TextBox)()
AddHandler t.GotFocus, AddressOf HoverText
Next
End Sub
Private Sub HoverText(ByVal sender As System.Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).SelectAll()
End Sub
The .NET way to alter the behavior of a class is to derive a new one. That's well supported by VB.NET and Winforms as well. For any class derived from Control, you can intercept an event by overriding the protected OnXxx event where Xxx is the event name. You should be using the Enter event btw. Use Project + Add Class and make the code look like this:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
Me.Select(0, Me.Text.Length)
MyBase.OnEnter(e)
End Sub
End Class
Compile. Go back to your form and note that you now have a new control on the top of the toolbox. Drag it on the form and note how it now behaves the way you want it, without writing any code or event handler in your form at all.