application open on startup in background - vb.net

Apologies if this question has already been asked.
I currently have it in the startup folder, but I'm not sure what code to use to not open the parent form.
I want my vb.net application to open on startup but in the background so it doesn't annoy users when they log in.
How would i go about setting that up?

To prevent a form from showing itself (but still create itself) you can override the following code in the form.
Protected Overrides Sub SetVisibleCore(value As Boolean)
MyBase.SetVisibleCore(False)
End Sub
This will always hide your form. Obviously pass true to make it display on whatever criteria you'd like to use.
This approach doesn't require you to restructure your application to separate ui + logic.

Try to move all the form init code to a sub Main function in a new module a set that function as a start up function, after the init code add:
dim frm as new <your_form>
after when you want to display it only call frm.show or frm.showdialog

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.

Visual basic. Change default startup form in code, depending on value of variable

I have been working on a Visual Basic project in Visual Studio and have encountered a problem.
I understand that the Startup form property in the Application page of the Project Designer can be changed to a default form, however what I require is a way to do this through code in ApplicationEvents.vb depending on the value of a variable within application settings.
The goal is that if a user completes a form then a value is assigned to a variable, e.g. variable username = "xxx". If this value is true, then the default startup is a login form (as the user has already registered), and if it is false then the user is taken to a register form.
I appreciate that I could use another form to determine this, however this seems like I would be squandering the capabilities of ApplicationEvents and not using it correctly (I also want to avoid the inevitable flicker of a blank form as it decides).
I know that the default form is stored in Application.myapp, however with the final publication of the .exe this file will (presumably) not be exported with it, so I want to avoid writing directly to it. I have also read into the windowsformsapplicationbase.mainform property, however cannot figure out how to use it?
Here is a example piece of code from ApplicationEvents.vb to demonstrate my question.
If String.IsNullOrEmpty(My.Settings.username) Then
MsgBox("You have not registered")
'set register as default form
Else
MsgBox("You have registered")
'set login as default form
End If
Usually, if you need that much control over what happens at start-up, you just want to disable the a application framework. To do so, just un-check the Enable application framework check-box in the Application tab of the My Project settings designer window. Once you un-check that, you will be able to change the Startup object to Sub Main. Then you can add a new module with a Main method, like this:
Module Module1
Public Sub Main()
Application.EnableVisualStyles()
If String.IsNullOrEmpty(My.Settings.username) Then
Application.Run(New RegisterForm())
Else
Application.Run(New LoginForm())
End If
End Sub
End Module
Be aware, however--by disabling the application framework, you will loose the other automatic functionality that it provides, such as ApplicationEvents. If you want to use the application framework, you can accomplish the same thing by simply setting the MyApplication.MainForm property in the MyApplication.Startup event:
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
If String.IsNullOrEmpty(My.Settings.username) Then
Me.MainForm = New RegisterForm()
Else
Me.MainForm = New LoginForm()
End If
End Sub
End Class
Alternatively, you could always show the same form, but then have the form contain nothing but a single UserControl. Then you can simply switch which UserControl is displayed depending upon the settings. The user-controls would need to include all of the controls that would have otherwise been placed on the two different forms.

Active form in a Windows application?

I am developing a Windows Forms application.
I have four forms which is inherited from Baseform in another project.
In all four forms I am using a label to show some transaction count based on network status. I have implemented a thread which gets the active form of application and setting up the text. The code works fine if application screen is active. If I minimize and open any other application, I am getting an null error exception.
How do I get the active form of an application?
Private Sub StartThread()
pollThread =New Thread(AddressOf PollfileStatus)
pollThread.IsBackground =True
running =True
pollThread.Start()
End Sub
Private Sub PollfileStatus()
While (running)
Try
For Each e As Control In Me.ActiveForm.Controls
If (e.Name = "pbStatus") Then
e.Invoke(New SetTextCallback(AddressOf Settext),
New Object() {e, 10})
End If
Next
Catch ex As Exception
Throw New ApplicationException(ex.Message)
End Try
Thread.Sleep(6000)
End While
End Sub
Understandably Me.ActiveForm is empty, since minimizing your form makes it inactive. You have two options:
Test if Me.ActiveForm is empty, and if so, do not update the label. This will effectively 'pause' label updates, until the user restores the window again.
Create a property on your class that contains your thread, to pass a reference of the form you have to update. This way the label on the given form will update even if it is not the active form. This might be a better option, it will update even if the form is inactive in the background, for example.
You should look into the Application.OpenForms shared property. It contains a collection of all forms in your application.
EDIT: Since you're working with .net 1.1 and don't have access to Application.OpenForms, here are some suggestions:
Implement your own shared class (module) that contains an ArrayList of forms. You could then create a base class inheriting from Form, handle the Load event to add the current form to the list of forms and the Closed event to remove it. Once you have that class, have all your real forms derive from it. BTW, it's (almost) how it is done in .Net 2.0.
Turn the problem around and have the working thread raise events when the values change that you can handle in each form to update it.
An old question, but I'd thought I'd offer a suggestion as well: you could implement a Public Shared CurrentForm As Form that you set in the Form.Activated event of a base form class all your forms inherit from. From the thread you could then use CurrentForm to access the desired form.
This is very easy like this in vb.net
Dim MYFormName As String = Me.Name.ToString
You have many solutions for this situation:
first of all you should consider checking if the current form is null before proceeding in any action.
second: if that condition passes, a good approach to store the last active form's reference.
This might work but I cannot test it right now.
For Each frm As Form In Application.OpenForms
If frm.ContainsFocus Then
Return frm
End If
Next

VB.NET: How to close and re-open a dialog in this case?

I'm developing a WinForms app in VB.NET, that handles sets of style data, and when the user clicks on another set's label, it prompts through a dialog "You are leaving this style preset to edit another one. keep changes on this one? [Yes] [No]"
But, I'm facing the problem that, when the user clicks either option, and the dialog closes, everything has to be refreshed, and loading the form again seems a good option.
I've tried putting a public sub on a module, that does this:
Public Sub CloseOpenStyleDlg()
KeepOrDiscardPrompt.Close()
StylesDlg.Close()
StylesDlg.ShowDialog()
End Sub
But as soon as that sub is called from the prompt, it crashes the application. (doesn't show an error in debug, simply crashes) How should I, from a given dialog, close the dialog, it's parent, and re-open it's parent? (which triggers all the Dialog_Load() code of the parent)
Thanks in advance! :)
You need to instantiate the dialog again. If I take your code for example:
Public Sub CloseOpenStyleDlg()
KeepOrDiscardPrompt.Close()
StylesDlg.Close()
StylesDlg = new StylesDlg()
StylesDlg.ShowDialog()
End Sub
When a form is closed, all resources created within the object are closed and the form is disposed.
If you want to reuse the Window instance use StylesDialog.Hide() function instead.

Force multi-threaded VB.NET class to display results on a single form

I have a windows form application that uses a Shared class to house all of the common objects for the application. The settings class has a collection of objects that do things periodically, and then there's something of interest, they need to alert the main form and have it update.
I'm currently doing this through Events on the objects, and when each object is created, I add an EventHandler to maps the event back to the form. However, I'm running into some trouble that suggests that these requests aren't always ending up on the main copy of my form. For example, my form has a notification tray icon, but when the form captures and event and attempts to display a bubble, no bubble appears. However, if I modify that code to make the icon visible (though it already is), and then display the bubble, a second icon appears and displays the bubble properly.
Has anybody run into this before? Is there a way that I can force all of my events to be captured by the single instance of the form, or is there a completely different way to handle this? I can post code samples if necessary, but I'm thinking it's a common threading problem.
MORE INFORMATION: I'm currently using Me.InvokeRequired in the event handler on my form, and it always returns FALSE in this case. Also, the second tray icon created when I make it visible from this form doesn't have a context menu on it, whereas the "real" icon does - does that clue anybody in?
I'm going to pull my hair out! This can't be that hard!
SOLUTION: Thanks to nobugz for the clue, and it lead me to the code I'm now using (which works beautifully, though I can't help thinking there's a better way to do this). I added a private boolean variable to the form called "IsPrimary", and added the following code to the form constructor:
Public Sub New()
If My.Application.OpenForms(0).Equals(Me) Then
Me.IsFirstForm = True
End If
End Sub
Once this variable is set and the constructor finishes, it heads right to the event handler, and I deal with it this way (CAVEAT: Since the form I'm looking for is the primary form for the application, My.Application.OpenForms(0) gets what I need. If I was looking for the first instance of a non-startup form, I'd have to iterate through until I found it):
Public Sub EventHandler()
If Not IsFirstForm Then
Dim f As Form1 = My.Application.OpenForms(0)
f.EventHandler()
Me.Close()
ElseIf InvokeRequired Then
Me.Invoke(New HandlerDelegate(AddressOf EventHandler))
Else
' Do your event handling code '
End If
End Sub
First, it checks to see if it's running on the correct form - if it's not, then call the right form. Then it checks to see if the thread is correct, and calls the UI thread if it's not. Then it runs the event code. I don't like that it's potentially three calls, but I can't think of another way to do it. It seems to work well, though it's a little cumbersome. If anybody has a better way to do it, I'd love to hear it!
Again, thanks for all the help - this was going to drive me nuts!
I think it is a threading problem too. Are you using Control.Invoke() in your event handler? .NET usually catches violations when you debug the app but there are cases it can't. NotifyIcon is one of them, there is no window handle to check thread affinity.
Edit after OP changed question:
A classic VB.NET trap is to reference a Form instance by its type name. Like Form1.NotifyIcon1.Something. That doesn't work as expected when you use threading. It will create a new instance of the Form1 class, not use the existing instance. That instance isn't visible (Show() was never called) and is otherwise dead as a doornail since it is running on thread that doesn't pump a message loop. Seeing a second icon appear is a dead give-away. So is getting InvokeRequired = False when you know you are using it from a thread.
You must use a reference to the existing form instance. If that is hard to come by (you usually pass "Me" as an argument to the class constructor), you can use Application.OpenForms:
Dim main As Form1 = CType(Application.OpenForms(0), Form1)
if (main.InvokeRequired)
' etc...
Use Control.InvokeRequired to determine if you're on the proper thread, then use Control.Invoke if you're not.
You should look at the documentation for the Invoke method on the Form. It will allow you to make the code that updates the form run on the thread that owns the form, (which it must do, Windows forms are not thread safe).
Something like
Private Delegate Sub UpdateStatusDelegate(ByVal newStatus as String)
Public sub UpdateStatus(ByVal newStatus as String)
If Me.InvokeRequired Then
Dim d As New UpdateStatusDelegate(AddressOf UpdateStatus)
Me.Invoke(d,new Object() {newStatus})
Else
'Update the form status
End If
If you provide some sample code I would be happy to provide a more tailored example.
Edit after OP said they are using InvokeRequired.
Before calling InvokeRequired, check that the form handle has been created, there is a HandleCreated property I belive. InvokeRequired always returns false if the control doesn't currently have a handle, this would then mean the code is not thread safe even though you have done the right thing to make it so. Update your question when you find out. Some sample code would be helpful too.
in c# it looks like this:
private EventHandler StatusHandler = new EventHandler(eventHandlerCode)
void eventHandlerCode(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(StatusHandler, sender, e);
}
else
{
//do work
}
}