System tray message alert when result > 0 - c#

There is a function that user can send message each other and it's data saved to table. And I wrote that simple code below that check's new message from database:
lDataParameter.Add("msg", _msgEnd);
ultragrid1.DataSource = _msgEnd.Tables[0];
if (ultragrid1.Rows.Count > 0)
{
ultragrid1.Rows[0].Selected = true;
MessageBox.Show("You have" + ultragrid1.Rows.Count.ToString() + " 1 new message");
}
It works! Now I want to display that message box on system tray however app closed...
How to get my app on system tray?

Thanks guys. I did it.
public Form1()
{
InitializeComponent();
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { this.menuItem1 });
// Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
// Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Text = "Notify Icon Example";
// Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("Icon.ico");
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (NotifyIcon example)";
notifyIcon1.Visible = true;
// Handle the DoubleClick event to activate the form.
notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
}
Now I want to create more menu this.
menuItem1.Index = 1;
this.menuItem1.Text = "I&nfo";
How to create with it?

Related

Is it possible to assign an event to a by code generated button in C# .Net

I am coding an Application to create a Mindmap like System. At the Moment I do that by Spawning a Button with specific text:
Button newButton = new Button();
this.Controls.Add(newButton);
ColorDialog MyDialog = new ColorDialog();
MyDialog.AllowFullOpen = false;
MyDialog.ShowHelp = true;
MyDialog.Color = Color.White;
if (MyDialog.ShowDialog() == DialogResult.OK)
newButton.BackColor = MyDialog.Color;
newButton.Text = inputfield.Text;
newButton.Location = new Point(70, 170);
newButton.Size = new Size(100, 50);
newButton.Show();
Now I am trying to assign a drag and drop function to it, and also have the code for it which is working, but my problem is that I cant assign a newButton_MouseDown event on it.
I tried this:
newButton.MouseDown += new MouseEventHandler(this.newButton_MouseDown);
But it doesn't work. I also couldn't find an answer anywhere else.

Create a new label if button from second form is pressed

I'm making a program that supposed to create a label on the second form when a button from that second form is pressed, I successfully added the button on the second form now that I'm struggling with making the label to be added into the second form when that button is pressed because I don't know how to do that, this is my code:
void Button1Click(object sender, EventArgs e)
{
Form form2 = new Form();
form2.Size = new Size(350,350);
// Setup
Button finish = new Button();
finish.Text = "Finish";
finish.Location = new Point(x,100);
// Utilize
form2.Controls.Add(finish);
form2.Text = "Second Form";
form2.Show();
}
I have done googling and searching through stackoverflow ended up with no solution.
This works in my small Windows Forms sample:
Form form2 = new Form();
form2.Size = new Size(350, 350);
// Setup
Button finish = new Button();
finish.Text = "Finish";
finish.Location = new Point(100, 100);
finish.Click += (s, e) =>
{
Label label = new Label();
label.Text = "Finish was clicked";
label.Location = new Point(10, 10);
label.Width = 300;
form2.Controls.Add(label);
};
// Utilize
form2.Controls.Add(finish);
form2.Text = "Second Form";
form2.Show();

How to show toast after performing some functionality in windows phone 8

I want to show something like toast after some functionality performed. i-e I have a save button and I want that when it pressed then a toast should be shown with the text Record Saved etc. I read posts that show toasts are only for back-ground agents. I know someone will give me good guidance. please specify some code.
Thanks
You can use the Toast Prompt from the Coding4Fun Toolkit to perform a toast notification via code. After referencing the toolkit (ideally via NuGet) you can use it like this:
ToastPrompt toast = new ToastPrompt();
toast.Title = "Your app title";
toast.Message = "Record saved.";
toast.TextOrientation = Orientation.Horizontal;
toast.MillisecondsUntilHidden = 2000;
toast.ImageSource = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.RelativeOrAbsolute));
toast.Show();
I prefer ProgressIndicator in my apps but you can use Popup or ToastPrompt.
Sample project.
// popup member
private Popup popup;
// creates popup
private Popup CreatePopup()
{
// text
TextBlock tb = new TextBlock();
tb.Foreground = (Brush)this.Resources["PhoneForegroundBrush"];
tb.FontSize = (double)this.Resources["PhoneFontSizeMedium"];
tb.Margin = new Thickness(24, 32, 24, 12);
tb.Text = "Custom toast message";
// grid wrapper
Grid grid = new Grid();
grid.Background = (Brush)this.Resources["PhoneAccentBrush"];
grid.Children.Add(tb);
grid.Width = this.ActualWidth;
// popup
Popup popup = new Popup();
popup.Child = grid;
return popup;
}
// hides popup
private void HidePopup()
{
SystemTray.BackgroundColor = (Color)this.Resources["PhoneBackgroundColor"];
this.popup.IsOpen = false;
}
// shows popup
private void ShowPopup()
{
SystemTray.BackgroundColor = (Color)this.Resources["PhoneAccentColor"];
if (this.popup == null)
{
this.popup = this.CreatePopup();
}
this.popup.IsOpen = true;
}
// shows and hides popup with a delay
private async void ButtonClick(object sender, RoutedEventArgs e)
{
this.ShowPopup();
await Task.Delay(2000);
this.HidePopup();
}
using Windows.UI.Notifications;
var toastXmlContent = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var txtNodes = toastXmlContent.GetElementsByTagName("text");
txtNodes[0].AppendChild(toastXmlContent.CreateTextNode("First Line"));
txtNodes[1].AppendChild(toastXmlContent.CreateTextNode("Second Line" ));
var toast = new ToastNotification(toastXmlContent);
var toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.Show(toast);

Add controls to another Winform programatically C# .NET

I am having mother form now, I want to create a new form programatically. I created the new form but I couldn't add controls to the form.
private void CreateWindows()
{
newWindow = new Form();
Application.Run(newWindow);
newWindow.Activate();
newWindow.Size = new System.Drawing.Size(40, 40);
Label label1 = new Label();
newWindow.Controls.Add(label1);
label1.Text = "HI";
label1.Visible = true;
label1.Size = new System.Drawing.Size(24, 24);
label1.Location = new System.Drawing.Point(24, 24);
}
I have tried the codes above, the new form showed but I couldn't see the label1.
I appreciate any helps.
Try putting the add controls after setting up the properties of label and then Show the new window.
private void CreateWindows()
{
newWindow = new Form();
newWindow.Activate();
newWindow.Size = new System.Drawing.Size(40, 40);
Label label1 = new Label();
label1.Text = "HI";
label1.Visible = true;
label1.Size = new System.Drawing.Size(24, 24);
label1.Location = new System.Drawing.Point(24, 24);
newWindow.Controls.Add(label1);
newWindow.Show();
//use this if you want to wait for the form to be closed
//newWindow.ShowDialog();
}
First: add the controls to newWindow.Controls.
Second: Do it before Application.Run because it will show the form and then wait for it to close (note: the way the designer does it is to add them at the constructor of a class that derived from Form).
private void CreateWindows()
{
newWindow = new Form();
//Application.Run(newWindow); //Not here
//newWindow.Activate(); //Wont do anything
newWindow.Size = new System.Drawing.Size(40, 40);
Label label1 = new Label();
newWindow.Controls.Add(label1); //Good
label1.Text = "HI";
label1.Visible = true;
label1.Size = new System.Drawing.Size(24, 24);
label1.Location = new System.Drawing.Point(24, 24);
Application.Run(newWindow); //Here instead
}
Third: if you have already used Application.Run in the current thread (say because you are doing this from a form), then there is no point to call it here. Use Show or ShowDialog instead.
Also consider adding controls this way:
private void CreateWindows()
{
newWindow = new Form();
newWindow.Size = new System.Drawing.Size(40, 40);
newWindow.Controls.Add
(
new Label()
{
Text = "HI",
Visible = true,
Size = new System.Drawing.Size(24, 24),
Location = new System.Drawing.Point(24, 24)
}
);
Application.Run(newWindow);
}

Set system tray notifyicon.icon to a pic in images folder

I've tried several things and eventualy just put the image directly inside C:\Users\Gebruiker\Documents\Visual Studio 2012\Projects\FolderMonitor\FolderMonitor\bin\Debug. This works for now, but idealy I'd like to set the notifyIcon.Icon = new Icon("folder.ico") to an image inside a images folder in the solution. But I can't figure out how this works..
public FolderMonitorApplicationContext()
{
this.monitor = new Monitor();
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("folder.ico");
notifyIcon.Text = "Folder Monitor";
notifyIcon.Visible = true;
contextMenu = new ContextMenuStrip();
openMonitor = new ToolStripMenuItem();
exitApplication = new ToolStripMenuItem();
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
openMonitor.Text = "Open";
openMonitor.Click += new EventHandler(OpenMonitor_Click);
contextMenu.Items.Add(openMonitor);
exitApplication.Text = "Exit..";
exitApplication.Click += new EventHandler(ExitApplication_Click);
contextMenu.Items.Add(exitApplication);
}
So it's currently working, but not how it i'd like it to work. Hope you can help me out here, thanks in advance.
After adding the picture file to your project, bring up the item properties by clicking on it, and pressing F4. Under Build Action, change it to "Embedded Resource".
You can access embedded resources in Stream form like this:
public FolderMonitorApplicationContext()
{
this.monitor = new Monitor();
notifyIcon = new NotifyIcon();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"<project namespace>.<folder path>" + "filename.ico"))
{
notifyIcon.Icon = new Icon(stream);
}
notifyIcon.Text = "Folder Monitor";
notifyIcon.Visible = true;
contextMenu = new ContextMenuStrip();
openMonitor = new ToolStripMenuItem();
exitApplication = new ToolStripMenuItem();
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
openMonitor.Text = "Open";
openMonitor.Click += new EventHandler(OpenMonitor_Click);
contextMenu.Items.Add(openMonitor);
exitApplication.Text = "Exit..";
exitApplication.Click += new EventHandler(ExitApplication_Click);
contextMenu.Items.Add(exitApplication);
}
Use the same method to insert the icon on the form.
To find the same code in your application to copy in your systemtray please:
check if, in the file .resx, there is the icon called with $ symbol(example:"$this.icon")
find your code to include the icon on the [name of form].desiner.cs.
example of code in my application:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(systemtray));
trayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

Categories