how to re-display form2 in hide mode from Form3? - vb.net

form2.Hide() [ currently at Form 3 ]
form1.Close()
(different) form1.Close()
is there anyway, by any chance i can bring up the Form2 that is in Hide mode? from From 3? I have been looking around.
i had try to do Form2.Show(). However, the system inform me " Reference to a non-shared member requires an object reference ".
This project i am doing, I use pass information from 1 form to another.
From Form1 - 3 passing info the information have no prob. The problem now is that from Form3 i would like to re-display the Form2 (which is in Hide mode). but it do not allow me to do so. so far i see is that i can only type the code Form2.ActiveForm.Show(). am sorry for my long message. did really hope for help cause i can't find solution on the net. could be i fail to ask to make a hit on google

the answer so far can be found on this link given. please read through the commend for your details.
what is the different in form2.Close() and Form3.ActiveForm.Close() in VB

Not sure if its the best solution but if I understand you right.
If you were to have a "Globals" Module and add form2 as a variable in there:
Public Form2 As New Form2
You would then be able to control it from other forms without it becoming invalid e.g.
Global.Form2.Hide
Global.Form2.Show
And even modify the controls and values inside it from other forms:
Global.Form2.ThisLabel.Text = "TEXT"
This means form3 can have full control over your "form2" even while its hidden.

Related

What Is the Correct Way to Close and Reopen a Form Declared as Public?

My problem pertains to a COM add-in for Microsoft Excel. The use case is as follows: 1. User clicks the add-in's button on the ribbon. 2. A form window appears. 3. User interacts with the form window and clicks an OK button. 4. Various reports are generated, while a progress bar on the form window shows progress. 5. At the end of the process, the form window closes.
The process works as designed on the first run, but after the form window has been closed there is no way to start a new "session." From the user's perspective the add-in button becomes non-responsive. When run in debug mode from Visual Studio, clicking the add-in button a second time generates an error message: "Cannot access a disposed object."
Clearly something is wrong with the way I have hooked everything up, but I haven't been able to find a simple description of how to do it correctly. Here is what I have:
In a public class a number of public (or "global" variables) are declared; the form is also declared and instantiated here:
Public Class GlobalVariables
Public Shared FormInstance As New MyFormDesign
End Class
The reason for declaring the form as a public object is to be able to be able to send progress values from various different subs and functions. The GlobalVariables class is imported by all modules that require it.
Behind the ribbon button is a single line of code:
FormInstance.Show()
Clicking the button instantiates and shows the form as intended. To keep things simple we can ignore the bulk of the code; simply clicking the "Cancel" button will trigger the problem. The code behind the "Cancel" button is straightforward:
Me.Close()
GC.Collect()
After closing the form it is no longer possible to create a new instance, per the error message cited above.
I don't really understand what is going on here, but it looks to me like the GlobalVariables class, once created, persists until the end of the Excel session. If that is correct the problem could presumably be cured by instantiating the form in a standard module. Instead of attempting to revive a form that has been disposed, the add-in would just create a new instance each time the user clicks the button. But if I go that route I can't figure out how to send progress values from other subs back to the form. It seems like a Catch-22.
There has got to be a way to both (a) create the form as a public object, and (b) create and destroy a new instance each time the add-in is run. Right? What am I doing wrong?
It has been a long journey, but I finally found out how to build the functionality described in my question. I will post the answer here, as it may help others in the future.
The challenge was to declare a form as Public in order to make it accessible throughout the project, so that subs and functions can send progress updates back to the form.
The problem was that the form, when declared and instantiated as described in my question, can only be created once per Excel session.
The solution is to declare the form as Public without instantiating it, then access it via a Public ReadOnly Property.
First, declare FormInstance as a public variable without instantiating it:
Public FormInstance As MyFormDesign
Second, define a Public ReadOnly Property. This establishes a way to call the form:
Public ReadOnly Property CallMyForm() As MyFormDesign
Get
Return FormInstance
End Get
End Property
Third, the ribbon button's Click event instantiates and shows the form:
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
FormInstance = New MyFormDesign
FormInstance.Show()
End Sub
Now a new form instance will be created each time the ribbon button is clicked, but the form can still be accessed via the CallMyForm property.
Instead of ...
FormInstance.BackgroundWorker1.ReportProgress(i)
... we will use:
CallMyForm.BackgroundWorker1.ReportProgress(i)
This solves the conundrum laid out in the question.

Best practice for using main form controls

So lets say I have a form1 that opens on startup. Form1 contains a textbox named textbox1 and a button named button1. When I click button1 this is code that is called:
Class form1
Sub CapLetters () 'buttons click event
Dim myObj as new MyObject
MyObj.CapAllLetters
Textbox1.text = MyObj.GetThoseCapLetters
End sub
End class
Now, I know this is kinda dumb, but my main question is, since im creating a new object, the textbox1 will not be available in my object unless I specifically call it like:
Form1.textbox1.text
Is this good practice or is there a better way? Now in my program I have about 10 textbox and comboboxs I need my object to use. I know i could do something like this:
MyObj.CapAllLetters (textbox1.text)
But that doesn't seem like a good idea to pass that many values in a method.
I think I need a way for my object to gather all the info upon initating it?
Thoughts?
You basically seem to be asking whether it's OK to use the default instance of a form. The answer is yes. In recent versions of VB, each form that has a parameterless constructor also has a default instance, which is a single instance that can be accessed anywhere and at any time via the class name. This feature exists because it makes it easier for beginners to access forms from different places in a project without having to worry about passing references to forms around that project.
In the case of the startup form in a project, it is always the default instance of its type, assuming that you haven't disabled the application framework. That means that you can always access your startup form anywhere in your project using the default instance of its type.
Now, you'll find that experienced developers rarely use default instances. That's because they are never required and rarely add value if you know what you're doing. That means that you can always access your application's startup form is reasonable locations throughout your project without using the default instance.

(VB.NET) Why doesn't my login system work?

OK. I have a code, and it should work, it DOES close the login form, but doesn't open the menu form.
If username.Text = "lolman8776" Then
If password.Text = "#########PASSWORD HIDDEN FROM THE INTERNET#########" Then
Form2.Show()
Me.Close()
End If
End If
What I don't understand, is that this code has no syntax errors and SHOULD WORK!
It shows form2 and THEN it closes itself, but Form2 never shows up.
I'm running VB.Net 2013 Community, as it was a free download. (I also registered it so it wasn't trial).
I have tried removing Me.Close() from Form1 and putting a line of code into Form2 that would close Form1:
Form1.Close()
But, still nothing. I do not know why ANY METHOD won't work. I've searched high and low for an awnser, but to no success. Does anyone have a solution?
According to your comment: "I tried not even hiding/closing form1, but just showing Form2 will cause the app to break."
I believe your issue lies around not instansiating your Form. The Show() method is not static and cannot be called directly on Form2. It must be called on an instance of your Form. For example:
Dim myForm As New Form2()
myForm.Show()
Please refer to this for a little more information.

vb.net Prevent Form 1 from activating when form 2 is clicked

im having a similar problem like the solution here Prevent main form from appearing when showing another form . but some of the suggestions were to minimize the main app so it doesnt show, which i cant do because my main app is supposed to be a desktop to be underneath all other apps to replace the windows desktop. And the second forms are supposed to be sticky notes. so i cant minimize the main window cause it has the user background and other controls. i tried making the parent of the notes a Nothing pointer, a pointer to the desktop, creating the form through a dll but i had no success.
My main problem is that when i click a note (form2) form1 comes up, even with form1 having the WS_EX_NOACTIVATE in the createparams. form1 does the form2.show() but they shouldn't be attached.
Another reason im having trouble with the solutions preseted in that post is that they are for delphi and im doing it in vb.net.
All i need is being able to click on the controls and write in the note without bringing the main form behind the note. either making them independent, or making the note not focusing the first form or being able to operate the note without it activating. i dont know. my last resource is to attach my main form to the desktop but i've heard is the worst thing you can do because it can cause problems hanging the system.
If you want both forms to co-exist but don't each to interfere with the other: In this case, then you might want to have a third Form that calls both Form1 and Form2 to be opened, and let me suggest and MDI Form with Form1 and Form2 as children forms of the MDI form
'============== my previous post ========================
You can force the user to first dismiss Form2 then allow him to go back to form1 by showing Form2 as Modal form. Here is how to display Form2 as modal
Dim f2 as New Form2
f2.ShowModal()
If that does not work, try this
Dim f2 as New Form2
f2.Show(True)

MessageBox persistence in VB.net

I'm using MessageBox to give some information to the user, but when such a box pops up, I want it to block access to the main window. So, until the user has clicked "OK", they should not be able to click (or even focus on) the window that's below it.
Does anybody know how to do this? I've noticed that MessageBox has very few functions, so maybe I'll even have to use a different object for this.
A quick and dirty solution could be a dialog form (Project->Add Windows Form->Dialog). This gives you visual extensibility of your MessageBox.
You can call your MessageBox's ShowDialog method and it will of course block access to the parent window.
There are of course other methods.
Example:
' This is your Dialog you created by going Project->Add Windows Form->Dialog
Public Class MessageBoxDialog
Public Overloads Sub ShowDialog(ByVal message As String)
Me.txtMessage.Text = message
ShowDialog()
End Sub
End Class
' This is the form you want to call the message box on
Private Sub btnShowMsgBox_Click(sender As System.Object, e As System.EventArgs) _
Handles btnShowMsgBox.Click
Dim messageBox As New MessageBoxDialog
messageBox.ShowDialog("This is another way to show a MsgBox but allows greater extensibility.")
End Sub
Just after I posted here, my friend came up with the answer. By calling MessageBox.Show(mf, "text") where mf is the main form, mf will be disabled as long as the OK button has not been clicked. I suppose this question was a bit silly to post after all, but I hope it might help others if they're stuck with the same problem.