I would like to dynamically add a menu item to a systray app. I already have an "Exit" and "Add more" menu. I would like to add more menus at run-time when I click on Add more.
For example, when I clicked on Add more, it automatically add a new menuItem to the tray App.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
NotifyIcon notify = new NotifyIcon();
notify.ContextMenuStrip = MainContextMenu();
notify.Icon = new Icon("Led.ico");
notify.Visible = true;
Application.Run();
}
private static ContextMenuStrip MainContextMenu()
{
ContextMenuStrip ConextMenuApp = new ContextMenuStrip();
ConextMenuApp.Items.Add("Add More", null, new EventHandler(AddMoreMenus));
ConextMenuApp.Items.Add("-");
ConextMenuApp.Items.Add("Exit", null, new EventHandler(Exit_Click));
return ConextMenuApp;
}
private static ContextMenuStrip AddMoreMenus(Object sender, EventArgs e)
{
ContextMenuStrip AddNewMenu = new ContextMenuStrip();
AddNewMenu.Items.Add("Menu One Addedd");
return AddNewMenu;
}
private static void Exit_Click(Object sender, EventArgs e)
{
Application.Exit();
}
}
}
There are many ways you can do this, however the simplest way is just hold a Reference to the MenuStrip or MenuStrip Item and pass it around.
Though in all honesty, just choose your poison:
Make a DI Service that holds the Reference
Make it Static
Pass it in as a Reference to the classes who want to manipulate it
Use Decouple Message or an Event Aggregator or some other pub/sub architecture
Related
I need to show form that will be always on a top level.
I use TopMost and TopLevel flags and call Activate method after Show.
But another window might steal Activate flag.
How I can fix it so that after creating a window, other windows cannot become active until this window is closed?
upd: it work only if execute app from output folder and don't work if run app with debug from IDE.
The following code will open a form (TestForm) when the program is started. TestForm contains a button, that when clicked, will create a new form (a new instance of TestForm in the code below, but it could be a different form, if desired)--which won't be visible until the current form is closed. When the current form is closed, the new form will become visible and the original form will be disposed.
Try the following to see if it gives you the desired result:
Create a new class (TestFormEventArgs)
In Visual Studio menu, click Project
Select Add New Item
Select Class (Name: TestFormEventArgs.cs)
Click Add
TestFormEventArgs.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Activate2ndFormWhen1stFormCloses
{
public delegate void TestFormNewFormRequestedEventHandler(object sender, TestFormEventArgs e);
public class TestFormEventArgs : System.EventArgs
{
public int CurrentFormNumber { get; private set; } = 1;
public TestForm Frm { get; private set; } = null;
public TestFormEventArgs(TestForm frm, int currentFormNumber)
{
this.Frm = frm;
this.CurrentFormNumber = currentFormNumber;
}
}
}
Create a new Form (TestForm)
In Visual Studio menu, click Project
Select Add New Item
Select Form (Windows Forms) (Name: TestForm.cs)
Click Add
Add button to TestForm (btnOpenNewForm)
In Visual Studio menu, click View
Select Toolbox
Click Button
Drag mouse over the top of TestForm, and click the mouse to add the button to TestForm
In Visual Studio menu, click View
Select Properties Window
Click the button on the form
In the Properties Window, set the following properties: (Name): btnOpenNewForm; Text: Open New Form
On TestForm, double-click btnOpenNewForm which will create btnOpenNewForm_Click event handler.
Double-click TestForm to go to the code. Modify the code for TestForm.cs:
TestForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Activate2ndFormWhen1stFormCloses
{
public partial class TestForm : Form
{
public event TestFormNewFormRequestedEventHandler NewFormRequested;
private int _currentFormNumber = 1;
TestForm _frmOther = null;
public TestForm()
{
InitializeComponent();
//set value
this.Text = "TestForm 1";
}
public TestForm(string frmText, int currentFormNumber)
{
InitializeComponent();
//set value
this.Text = frmText;
this._currentFormNumber = currentFormNumber;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void SendUpdates(TestForm frm, int currentFormNumber)
{
//check if there are subscribers
if (NewFormRequested != null)
{
//create a new instance of TestFormEventArgs
TestFormEventArgs valueArgs = new TestFormEventArgs(frm, currentFormNumber);
//raise event
NewFormRequested(this, valueArgs);
}//if
}
private void btnOpenNewForm_Click(object sender, EventArgs e)
{
string frmText = String.Format("TestForm {0}", _currentFormNumber + 1);
//create new instance
_frmOther = new TestForm(frmText, _currentFormNumber + 1);
//set properties
_frmOther.StartPosition = FormStartPosition.CenterScreen;
_frmOther.Visible = false;
SendUpdates(_frmOther, _currentFormNumber);
}
}
}
Change Program.cs code to the following:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace Activate2ndFormWhen1stFormCloses
{
static class Program
{
static TestForm _currentFrm = null;
static Queue<TestForm> _frmQ = new Queue<TestForm>();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//place form in Queue
_frmQ.Enqueue(new TestForm());
while(_frmQ.Count > 0)
{
//dequeue
_currentFrm = _frmQ.Dequeue();
//subscribe to events
_currentFrm.FormClosed += CurrentFrm_FormClosed;
_currentFrm.NewFormRequested += CurrentFrm_NewFormRequested;
Application.Run(_currentFrm);
}
}
private static void CurrentFrm_NewFormRequested(object sender, TestFormEventArgs e)
{
Debug.WriteLine("CurrentFrm_NewFormRequested: " + e.Frm.Text);
//add form to Queue
_frmQ.Enqueue(e.Frm);
}
private static void CurrentFrm_FormClosed(object sender, FormClosedEventArgs e)
{
TestForm frm = (TestForm)sender;
Debug.WriteLine("CurrentFrm_FormClosed: " + frm.Text);
try
{
//unsubscribe from events
_currentFrm.FormClosed -= CurrentFrm_FormClosed;
_currentFrm.NewFormRequested -= CurrentFrm_NewFormRequested;
_currentFrm.Dispose();
_currentFrm = null;
}
catch(Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
}
}
}
Try ShowDialog() instead of Show() method.
ShowDialog() will make the other form that is open Disabled and the current form is the only one that is Enabled.
Just started learning Visual Studio and I'm trying to make a soundboard in Windows Forms.
ButtonMaker(); is the function I use to make a button for each soundfile in my directory so I don't have to make 70 different buttons for every sound, but when I run the program nothing shows up in the forms window. Anybody know why? I've tried calling the function in Main() and in the initial Form1 class but nothing happens in either. Forms class file here:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MySoundBoard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
///Tried running it here
}
private void Form1_Load(object sender, EventArgs e)
{
///Tried running it here
}
private void ButtonMaker()
{
string[] files = Soundfiles.GetFile();
foreach (var item in files)
{
string btnName = item.ToUpper();
Button btNname = new Button();
btNname.Text = item;
int x = 40;
int y = 40;
btNname.Location = new Point(x, y);
x = x + 50;
if (x>900)
{
x = 40;
y = y + 30;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Here is the SoundFiles class:
using System.IO;
using System;
using System.Text;
using System.Diagnostics;
using WMPLib;
namespace MySoundBoard
{
class Soundfiles {
WMPLib.WindowsMediaPlayer Player;
static public string[] GetFile() {
string txtPath = #"C:\Documents\path\to\sound effects";
string[] files =
Directory.GetFiles(txtPath, "*ProfileHandler.cs", SearchOption.TopDirectoryOnly);
return files;
}
public void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.URL = url;
Player.controls.play();
}
}
}
And the main project file(not that worked with yet):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MySoundBoard
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
///Tried running it here
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
As said, I'm very new to this language and any help would be appriciated!
You create the buttons but never add them to the form. Just add
this.Controls.Add(btNname);
The next thing is, that your buttons will not do something. You'll need to add an event handler as well.
btNname.Click += ...;
In order to know which button plays which sound, you need to find a way to have that association. A hacky approach is
btNname.Tag = item;
and then evaluate Tag later
I have a Windows Forms C# application which deals with TreeView and uses a separate thread to update it.
Update launched by button click:
private void button3_Click(object sender, EventArgs e)
{
vc.ChangeTree(treeView1);
}
ChangeTree() starts UpdateTree() in a separate thread to update the TreeView asynchronously.
Here I just need to use Thread for this:
public void ChangeTree(TreeView tree)
{
Thread thread4 = new Thread(() => { UpdateTreeView(tree); });
thread4.Name = "Thread 4";
thread4.IsBackground = true;
thread4.Start();
}
UpdateTree() simply invokes the TreeView and adds nodes from another tree to it. Note that there aren't time consuming operations. The GetTree() just returns another TreeView:
private void UpdateTreeView(TreeView tree)
{
if (tree.InvokeRequired)
{
tree.Invoke((MethodInvoker)delegate
{
UpdateTreeView(tree);
});
}
else
{
tree.Nodes.Clear();
foreach (var node in GetTree().Nodes)
{
tree.Nodes.Add(node as TreeNode);
}
//Application.DoEvents(); // doesn't help
}
}
The problem is that it actually adds nodes to the tree, but form does not show it. I checked the treeView1.Nodes after i clicked the button. It contains the nodes I need.
The Application.DoEvents() does not help, even though it may, according to my search. Neither does treeView1.Refresh().
If i click the button twice, treeView1 updates as I planned. But I don't know why and, obviously, I don't need such behavior.
So here is the question. How to make it update with one click?
It turns out that TreeNode can't be used by more than one TreeView.
The solution I found is to use treeNode.Clone():
tree.Nodes.Add((node as TreeNode).Clone() as TreeNode);
But I still don't understand why the initial code worked, even though it took two times for it.
I suspect that one TreeNode cannot be changed from one TreeView to another
This is my test code that works:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
ChangeTree(treeView1);
}
public void ChangeTree(TreeView tree)
{
Thread thread4 = new Thread(() => { UpdateTreeView(tree); });
thread4.Name = "Thread 4";
thread4.IsBackground = true;
thread4.Start();
}
private void UpdateTreeView(TreeView tree)
{
if (tree.InvokeRequired)
{
tree.Invoke((MethodInvoker)delegate
{
UpdateTreeView(tree);
});
}
else
{
tree.Nodes.Clear();
foreach (var node in GetTree())
{
tree.Nodes.Add(node as TreeNode);
}
}
}
private List<TreeNode> GetTree()
{
var result = new List<TreeNode>();
result.Add(new TreeNode("changed"));
return result;
}
}
}
If I change the GetTree() function into
private TreeView GetTree()
{
var result = new TreeView();
result.Nodes.Add(new TreeNode("changed"));
return result;
}
And revert the UpdateTreeView function into what it was before, it behaves like you described
Did you try setting the focus back to the treeview? With TreeView.Focus() this causes a refresh.
Good morning,
I have several questions but I am not sure which is the important one to ask so I'll first state my overall problem. I can't close my Winform App. I have searched and found many answers but they either don't work, I don't understand or both.
If I do all my work and then call Application.Exit the form never closes. Same results if I put this.Close. However if I place a button on the form and call Application.Exit it closes the form.
I obviously do not understand the flow, I hope it is clear to someone what I am trying to do. As a non-programmer I have been piecing this project together for a few months and this is my last step - Close the form after work is complete if it was run from command line with arguments. I would try longer to work it out but my Visual Studio trial runs out this week so I turn to the experts :)
Thank you,
Todd
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(String[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//add if
if (args.Length == 0)
{
Application.Run(new Form1("Form"));
}
else
{
Application.Run(new Form1(args[0]));
}
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
public partial class Form1 : Form
{
string CLArg1;
string ReturnText;
public Form1(string Arg1)
{
InitializeComponent();
if (Arg1 != null)
{
CLArg1 = Arg1;
textBox1.Text = Display(CLArg1);
//button1.Enabled = false;
}
else
{
textBox1.Text = "click button to start";
}
Application.Exit(); //This seems to be ignored
}
public void button1_Click(object sender, EventArgs e)
{
CLArg1 = null;
textBox1.Text = Display("Hello World");
Application.Exit();
}
public string Display(string DisplayText)
{
if (CLArg1 != null)
{
ReturnText = CLArg1;
return(ReturnText);
}
else
{
ReturnText = DisplayText;
return(ReturnText);
}
}
}
}
See this question. Application.Close() only works if an application has been created. This is done by calling Application.Run(). Now. in your code you call Application.Exit() from the constructor of your form. Which is executed before the Application.Run() that is needed to create the application.
To fix this, either wait until after Application.Run(). Or, if you want to quit the application in the constructor use Environment.Exit(int statusCode). When using Environment.Exit(int statusCode) keep this in mind though.
You can't close the Application when the Form is Loading from the Program class. Try calling the Exit method after Loading the Form:
private void Form1_Load(object sender, EventArgs e)
{
if (CLArg1 != String.Empty)
Application.Exit();
}
I have three forms named frmBase(form1),frmGraph(form2) and frmBalloon(form3).
frmGraph(form2) opens if the user clicks on the button named btnShow placed on frmBase(form1).
frmBalloon(form3) opens if the user clicks on the button named btnShow placed on frmGraph(form2).
Now if the user clicks on the button named btnCancel placed on the frmGraph(form2) OR clicks on the button named btnCancel placed on the frmBalloon(form3) every form that is open should be closed except frmBase(form1).
So, every forms should be closed except mainform when user clicks on the button placed on the form2 or form3. So for that is there any solution?
Maintain references of all form objects that needs to be closed on designated event. Create and Call the function in frmBase whenever needed. The function will be responsible to close all registered forms if open.
Looks like an observer pattern case.
It is not a great solution, Application.OpenForms is a bit unreliable, but easy:
public static void CloseAllFormsButMain() {
for (int ix = Application.OpenForms.Count - 1; ix > 0; --ix)
Application.OpenForms[ix].Close();
}
Observer pattern is appropriate with some specializaiton. A more specialized version of the observer pattern for this type of scenario is the EventAggregator pattern. The event aggregator pattern is ideal for this type of scenario.
In short the event aggregator allows you to centralize the publishing/subscription of events. Therefore all subscribers and publishers talk only to the EventAggregator. Subscribers subscribe to events and publishers command the event aggregator to publish something.
The event aggregator pattern also decouples each publisher subscriber from each other. This eliminates the need for the child forms to reference the parent forms.
Jeremy Miller provides a good example in his Build Your Own Cab series. Due to my new membership I cant post the links to the sites but just a do a search for the following items.
EventAggregator by Martin Fowler
Build Your Own CAB series by Jeremy Miller (codebetter.com)
EventAggregator in PRISM
Here is a simple example I cooked up using C# and generics. This is by no means complete. It is just to illustrate a simplified example. For a more complete patter turn to Jeremy Millers example.
[code]
//Sample Custom Event args
using System;
namespace EventAggregatorPatternDemo
{
public class CloseAllFormsEventArgs : EventArgs
{
}
}
//Sample Form Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EventAggregatorPatternDemo
{
public partial class GraphForm : Form, IListener
{
public GraphForm()
{
InitializeComponent();
}
private void GraphForm_Load(object sender, EventArgs e)
{
EventAggregator.GetTheEventAggregator().Subscribe<CloseAllFormsEventArgs>(this);
}
public void Handle<TEventArgs>(TEventArgs e)
{
if (e.GetType() == typeof(CloseAllFormsEventArgs))
{
this.Close();
}
}
private void btnCloseAll_Click(object sender, EventArgs e)
{
EventAggregator.GetTheEventAggregator().Publish(this, new CloseAllFormsEventArgs());
}
private void GraphForm_FormClosed(object sender, FormClosedEventArgs e)
{
EventAggregator.GetTheEventAggregator().CancelSubscription<CloseAllFormsEventArgs>(this);
}
}
}
//Event Aggregator code
using System;
using System.Collections.Generic;
namespace EventAggregatorPatternDemo
{
public interface IListener
{
void Handle(TEventArgs e);
}
public class EventAggregator
{
static EventAggregator _TheEventAggregator;
readonly Dictionary<Type, List<IListener>> _listeners;
private EventAggregator()
{
_listeners = new Dictionary<Type, List<IListener>>();
}
public static EventAggregator GetTheEventAggregator()
{
if(_TheEventAggregator == null)
{
_TheEventAggregator = new EventAggregator();
}
return _TheEventAggregator;
}
public void Publish<TEventArgs>(object sender, TEventArgs e)
{
if(_listeners.ContainsKey(e.GetType()))
{
var listOfSubscribers = _listeners[e.GetType()];
for(int i = listOfSubscribers.Count - 1; i > -1; i--)
{
listOfSubscribers[i].Handle(e);
}
}
}
public void Subscribe<TEventArgs>(IListener listener)
{
if(_listeners.ContainsKey(typeof(TEventArgs)))
{
_listeners[typeof(TEventArgs)].Add(listener);
}
else
{
List<IListener> newListenerList = new List<IListener>();
newListenerList.Add(listener);
_listeners.Add(typeof(TEventArgs), newListenerList);
}
}
//Cancels all subscriptions
public void CancelSubscription<TEventArgs>(IListener listener)
{
Type eventArgsType = typeof(TEventArgs);
if (_listeners.ContainsKey(eventArgsType))
{
//Remove from the end
for (int i = _listeners[eventArgsType].Count-1; i > -1; i-- )
{
//If the objects are the same
if(ReferenceEquals(_listeners[eventArgsType][i], listener))
{
_listeners[eventArgsType].RemoveAt(i);
}
}
}
}
}
}
[/code]
Have you thought about using a static event? Here is a simple example.