Use a NotifyIcon between two forms in C# - c#

I plan to use this method to display a notify icon so it can be accessed between the main form and child forms (found from another SO post)
public partial class MainForm : Form {
public MainForm() {
InitializeComponent();
notifier = this.notifyIcon1;
this.FormClosed += delegate { notifier = null; };
}
public static NotifyIcon Notifier { get { return notifier; } }
private static NotifyIcon notifier;
}
Is it necessary to add the FormClosed delegate? I would think that when the form everything else gets destroyed and freed up?
Edit: For clarification - I can confirm that using the code without the delegate above, when i close the form the system tray icon does disappear, also I did use the VS UI to drag/drop a NotifyIcon from toolbox so designer is handling initialization for me and I am using the constructor like above to access it.

If the notify icon is logically tied to this instance of the form then you shouldn't have it be a static field. Marking it as static is done to specifically say that it's not tied to one instance, but rather is shared between all instances.
I would think that when the form everything else gets destroyed and freed up?
If it were instance data it would be, but because it's static, and therefore not tied to the instance, it will not be cleaned up when the form goes away.
Of course, if this is the main form (and not a misnomer) then the entire application will end when the form closes, which will clean up everything regardless, static or not.

You need to set
nutifier.Visible = false;
notifier = null;
on closing. If not, the icon stays after the application was closed

Related

Calling a public method from another class doesn't respond

I'm making a game library with C#.
I have a main Form which has a FlowLayoutPanel, which hosts the game library. There is an "add game" method in main Form which adds an item to FlowLayoutPanel, but this method is being called from a second form. But when I'm calling this method from this second form, nothing happens at all, but it works, if called from the main form.
Here's the code:
Here's the add game method in mainForm:
public void addIso()
{
PictureBox gameBox = new PictureBox();
gameBox.ImageLocation = "link here";
gameBox.Height = 200;
gameBox.Width = 150;
gameBox.SizeMode = PictureBoxSizeMode.StretchImage;
isoPanel.Controls.Add(gameBox);
}
This method adds a placeholder game to FlowLayoutPanel called isoPanel. Works when called from the same form.
And here's how the method is being called from second form:
private void addGameButton_Click(object sender, EventArgs e)
{
MainWindow mainForm = new MainWindow();
mainForm.addIso();
}
When I tried to add simple message box in the method, message box did show, but game wasn't added to FlowLayoutPanel.
Any tips or solutions?
I apologize for my poor english and messy programming terms, I started learning C# a little while ago.
You can achieve this by using delegate and Delegate.
What you are doing wrong here:
From addGameButton click event you are creating a new instance of the mainForm. Which is not the actual main form that you are currently seeing, its a different instance of the mainForm. your code adds the control to the layout just confirm this by calling mainForm.Show() this will opens a new form with the control.
What you can do:
In such cases, you want to modify the parent control, calling methods in parent class, you need to use delegates by the following way:
Changes needs to apply in the Parent class ie., MainWindow.
Define a delegate and an event of that delegate type.
public delegate void UpdateUiDelegate();
public event UpdateUiDelegate UpdateUiEvent;
Assign the required method to the Event(better use this in constructor):
public MainWindow ()
{
UpdateUiEvent+= new UpdateUiDelegate(addIso);
}
Next work is in the child form ie., Form2 (use a better name let it be ImageChildControl). Create a Delegate there:
public Delegate ControlCreator;
Come back to MainWindow, Locate the place where you are calling the Second form, after creating instance of the child form assign the created event to that Delegate of the instance, for that Use the following codes there:
ImageChildControl ImageChildControlInstance = new ImageChildControl();
ImageChildControlInstance.ControlCreator = UpdateUiEvent;
One more work to do in child form that is; Calling the delegate from the Button click event. ie.,
private void addGameButton_Click(object sender, EventArgs e)
{
ControlCreator.DynamicInvoke();
}
Now you can see your code works as you expected.
You seem to be recreating the form when you load the second form.
as this does lack a little context.
I suggest you have a main tread which will run the two forms
create a game manager class Ex: GameManager.cs
In the class create a variable for both forms and set them as protected.
in each form send a reference of the gamemanager to it.
which will allow each window to communicate with each other through the manager by provide secure inter communication.
Let me know if you would like some source with this.

Splash screen does not return focus to main form

I everyone. I currently have a problem with my focus when using a splash screen. I am using VS2008, with .NET framework 2.0. Also, I have linked my project with the VisualBasic.dll since I use the ApplicationServices to manage my single instance app and splash screen.
Here is a code snippet simplified of what I tried debugging.
namespace MyProject
{
public class Bootstrap
{
/// <summary>
/// Main entry point of the application. It creates a default
/// Configuration bean and then creates and show the MDI
/// Container.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Creates a new App that manages the Single Instance background work
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
App myApp = new App();
myApp.Run(args);
}
}
public class App : WindowsFormsApplicationBase
{
public App()
: base()
{
// Make this a single-instance application
this.IsSingleInstance = true;
this.EnableVisualStyles = true;
// There are some other things available in the VB application model, for
// instance the shutdown style:
this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses;
// Add StartupNextInstance handler
this.StartupNextInstance += new StartupNextInstanceEventHandler(this.SIApp_StartupNextInstance);
}
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new MyMainForm();
this.SplashScreen.StartPosition = FormStartPosition.CenterScreen;
}
protected override void OnCreateMainForm()
{
// Do your initialization here
//...
System.Threading.Thread.Sleep(5000); // Test
// Then create the main form, the splash screen will automatically close
this.MainForm = new Form1();
}
/// <summary>
/// This is called for additional instances. The application model will call this
/// function, and terminate the additional instance when this returns.
/// </summary>
/// <param name="eventArgs"></param>
protected void SIApp_StartupNextInstance(object sender,
StartupNextInstanceEventArgs eventArgs)
{
// Copy the arguments to a string array
string[] args = new string[eventArgs.CommandLine.Count];
eventArgs.CommandLine.CopyTo(args, 0);
// Create an argument array for the Invoke method
object[] parameters = new object[2];
parameters[0] = this.MainForm;
parameters[1] = args;
// Need to use invoke to b/c this is being called from another thread.
this.MainForm.Invoke(new MyMainForm.ProcessParametersDelegate(
((MyMainForm)this.MainForm).ProcessParameters),
parameters);
}
}
}
Now, what happens is that, when I start the application, the Splash Screen shows as expected, but when it is destroyed, it does not return the focus to the main form (Form1 in the test). The MainForm simply flashes orange in the taskbar. If I launch the application from the IDE (VS2008), focus works just fine. I am using XP Pro. Also, the main form is not on top of every other windows. If I comment out the OnCreateSplashScreen() method, the application gets focus normally.
To test normal execution, I am using the VS Command Prompt to launch my application. I am using the Release version of my project.
Any ideas?
Edit:
I also handle the StartUpNextInstance event to send my command-line arguments to my main form. For test purposes, it was removed here.
Edit: Added a bit more code. Now you have the full extent of my bootstrap.
The question is not in detail.
1) What the is the relationship between the SplashScreen and the main form of the application?
2) How does SplashScreen automatically close?
I'm sure the problem here is that the main form had already started loading up in the background while SplashScreen is yet to close. Due to bad timing, the main form loads up in the background and the SplashScreen unloads... hence the flash in the taskbar.
Make them appear in serial controlled manner. There are many ways. I cannot suggest exactly how since hardly any detail has been provided. Like what is VB doing in the project, how many threads are working, what are the custom forms used here.. etc.
EDIT:
Algorithm to implement Splash screen (IMHO) :)
1) Create a custom form - splash screen
2) Run it on a separate thread. Implement it's behaviour as you like.
3) In your splash screen form, write a handler to capture a custom unload event handler which closes the splash screen form.
4) Now, back in the main thread, create you main app form. Set its Visible property to false.
5) Write even handler of the main form's Load event. In this handler, fire an event to splash screen to unload. Then, make the main form visible.
You need to call the Activate() method on your main form in order to display it properly.
In some of my implementations of splash forms on a different thread, I've seen this situation as well.
usually if I call this.Activate(); as my first line of code in the form load it tends to work as expected.
putting this after hiding the splash usually doesnt work correctly.
other methods that can help.
this.BringToFront();
this.TopMost = true; //setting this to true and back to false sometimes can help
What if you set the focus explicitly in your Form1 Load event?
For some obscure reason, focus seems to be working correctly. I am going to try and incorporate more of my normal execution of my main program into the MyMainForm and try to find what really causes the focus change to fail. I will keep you all posted.
try calling form1.show() after this.MainForm = new Form1(); It worked for me.
I had the same issue and wanted easy solution for this.
I tried above but no luck, but ideas from above useful information.
I tried my trick and it works for me.
This is my Programs.cs code which works fine:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var mainWindow = new frmMain();
splashScreen.DisplaySplashScreen(mainWindow, 10000);
**mainWindow.TopMost = true;**
Application.Run(mainWindow);
}
}
Notice the mainWindow.TopMost = true;
Ok that was working enough but I did not want the mainWindow to stay on top.
So in Shown event i added this.TopMost = false;
private void frmMain_Shown(object sender, EventArgs e)
{
this.TopMost = false;
}
Now my application is working as I wanted it to work.
Note: Just posted to help any other user who face the same issue what I faced.
Happy Coding :)
in you main form's load event add
this.Activate();

Get Data from Windows Form into C# program

I want my C# program to collect data. Then, when the OK button is clicked, I want this data to be loaded into my program -- such as into variables in Main(), or into data members of a class I have constructed, and I want the form to then go away -- not be hidden, but actually closed. Alas, read as I might in the book I have, the data collected by the form stays in the form, execution proceeds within the form, and the same form is used to display the result of the program's computations. Sheesh. All I want is to capture the form's information, close the form and proceed with my program. I would appreciate some tips on geting data from a form into Main() or into a class's data members.
Thanks,
Lucky
What you want to do is perfectly acceptable, it just isn't typical.
When you use Visual Studio to generate a WinForms project, it creates one form for you and
generates a call to Application.Run(new Form1()). For this version of the Run() method, your application will exit when the "main form" (the one passed to Run(), in this case Form1) closes.
There are three overloads (versions) of Application.Run(). For your purposes, you need to use a different overload:
Application.Run(ApplicationContext)
When you use this overload of Run(), you get to control when the application exits. In a nutshell, here's one way you could do it:
Create a class which inherits
ApplicationContext.
In its constructor:
Create your form.
Subscribe to its Closing and Closed events.
Show your form.
In your FormClosing event handler,
get the data from the form.
In your FormClosed event handler, do
whatever you want to do with the
data, and then exit the thread (or do something else).
Here's a crude example, but I will leave out the code for the form itself. Assume the form simply has one TextBox which has its Modifiers property set to Public. (This is NOT an elegant way to get data from a form, but that part is up to you).
namespace Me.MyDemo
{
static class Program
{
[STAThread]
static void Main()
{
MyApplicationContext ac = new MyApplicationContext();
Application.Run(ac);
}
class MyApplicationContext : ApplicationContext
{
string _text = "";
public MyApplicationContext()
{
Form1 f1 = new Form1();
f1.FormClosing += new FormClosingEventHandler(f1_FormClosing);
f1.FormClosed += new FormClosedEventHandler(f1_FormClosed);
Console.WriteLine("I am here. Showing form in 1 second...");
Thread.Sleep(1000);
f1.Show();
}
void f1_FormClosing(object sender, FormClosingEventArgs e)
{
_text = (sender as Form1).textBox1.Text;
}
void f1_FormClosed(object sender, FormClosedEventArgs e)
{
Console.WriteLine("You wrote: " + _text);
Console.WriteLine("I will go away in 2 seconds...");
Thread.Sleep(2000);
ExitThread();
}
}
}
}
Of course, you don't have to exit the thread. You can leave it running if there are other things for your program to do. It will just run as a windowless process. Just remember that you're responsible for eventually ending it.
For more help, look at the documentation for the System.Windows.Forms.Application class, and the ApplicationContext class.
For getting the data from your form, there are many ways to approach this. The simple way is to just give your form some public properties. A more sophisticated way would be to create a data class and use data-bound controls on your form.
You're writing in WinForms? As far as I know a Windows application has to have a window, even if it's one pixel by one pixel.
Have you seen any other Windows applications that work the way that you want yours to work? Opens a window, the window closes, but the program keeps on running? This is generally considered undesired behavior, similar to viruses and trojans.
You can create a console application or a Windows service with no GUI, of course.
What is the application doing behind the scenes after the data is entered? If it's just doing some calculations and saving to disk, uploading, or printing, leave the window open for that and then exit when it's done. Possibly include a progress bar.

FileSystemWatcher.SynchronizingObject without a form

I have an windows form application written in C# in which I use a FileSystemWatcher to monitor a folder for new files and then perform some processing on them. My application is designed to run in the system tray and therefore does not instantiate any forms at startup. The problem is that the Created event is firing on a separate thread and when I try to create an instance of a form in the Created event I get an ThreadStateException that states I need to be running in SingleThreadApartment. I think I need to set the FileSystemWatcher.SynchronizingObject property but don't know what to use since there is no main form in my application.
You will have to call Application.Run() in your Main() method to get the Windows Forms synchronziation machinery in place so that FileSystemWatcher can properly marshal the call to the main thread. The problem you'll have then is that the main form will become visible. Fix that by pasting this code into the class:
protected override void SetVisibleCore(bool value) {
if (!this.IsHandleCreated) {
this.CreateHandle();
value = false;
}
base.SetVisibleCore(value);
}
There are no restrictions on what your form looks like if you do this.
You don’t need to pass any forms to Application.Run at all. Then you can save having to mess around with form visibility. Just do this:
var InvokerForm = new Form();
var dummy = InvokerForm.Handle; // force handle creation
Application.Run();
Just one gotcha there - the form handle creation needs to be forced by accessing it once.
The simplest way to do this is to make a hidden form and pass it to Application.Run.
You can then set the SynchronizingObject property to the hidden form.
To make sure it's a hidden form, set the ControlBox and ShowInTaskbar properties to false.

manually firing the event in c#

i want to fire an event manually using c#. For instance, say if i want to fire a Form_closing event of Form A from Form B. How to do it ??
After getting some comments. I think i need to explain more on this.
Since my Form A is reference to a .dll which creates a custom taskbar in the desktop,
There is a situation for me to close the that custom taskbar from Form B.
i already tried FormA.Close() from Form B. when i do this, the .dll is unloaded from the app domain and due to this the space occupied by the custom task bar is blocked.
But that is not the case when i click the close button in the custom task bar. when i do this the space is freed up.
This is the reason i want to fire the close event of Form A manually from Form B which will solve my issue.
Thanks.
We did the following in one project:
There was a GlobalNotifier class, which defined events we wanted to use in different modules of the application, like this
public static class GlobalNotifier
{
public static event VoidEventHandler EnvironmentChanged;
public static void OnEnvironmentChanged()
{
if (EnvironmentChanged != null)
{
EnvironmentChanged();
}
}
}
Then, you could raise this event anywhere when you needed to let the rest of the application know that the environment has changed, like this
GlobalNotifier.OnEnvironmentChanged();
And also you could subscribe to this event wherever you wanted to be notified about the fact that the environment has changed.
public ReportingService()
{
GlobalNotifier.EnvironmentChanged += new VoidEventHandler(GlobalNotifier_EnvironmentChanged);
}
void GlobalNotifier_EnvironmentChanged()
{
//reset settings
_reportSettings = null;
}
So, whenever you changed the environment, you raised the event, and everyone who needed to know about that and perform some actions, was notified.
Might be similar to what you need to achieve.
Also, if you need to pass parameters, you can define the event any way you like, basically -
public static event VoidEventHandler<SomeObject, List<OtherObject>> SomethingUpdated;
public static void OnSomethingUpdated(SomeObject sender, List<OtherObject> associations)
{
if (SomethingUpdated != null)
{
SomethingUpdated(sender, associations);
}
}
// ...
MyClass.SomethingUpdated+= new VoidEventHandler<SomeObject, List<OtherObject>>(MyClass_SomethingUpdated);
// ...
void MyClass_SomethingUpdated(SomeObject param1, List<OtherObject> param2)
{
//do something
}
You would call the OnFormClosing() method of the Form class. You can do this with any event that has a paired On...() method.
It sounds to me, from your comments, like you don't want to raise one form's event from the other; you just want to handle one form's event from the other.
Yes, you can do that. FormB needs to have a reference to FormA. There are several ways you can do this; one easy way is to have a FormA type property in your FormB class, like this:
public FormA TheOtherForm { get; set; }
You set that property to your instance of FormA, and then you add the event handler as you've described in the comments.
You don't have to use FormA as the type of your property; any form has the FormClosing event, so you can just use Form as the type if you want.
Close the form. That will raise the event. If you want to raise the event separately from closing the form, you're doing something wrong; move that code to a separate method that you can call from the event and from FormB.
FormAInstance.Close()
Starting with Visual Studio 2005 (.Net 2.0), forms have automatic default instances. It sounds like that's what you're using. You should know that this feature exists mainly for backwards compatibility with VB6. You're really better off creating and using a new instance of the form. When you do that, you should just be able to call the .Close() method on that instance without it's app domain closing.
If you want to re-use this space, you might also try simply .Hide()-ing the form rather than closing it.

Categories