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")));
Related
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.
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?
I want to make a button that can drop down a multi-line label or form which contains help documentation for the user.
I have searched and I can't find anything that is for C# Winforms. Do any free controls out there exist for this or will I have to create it myself?
Many thanks,
Richard
Using ToolStripControlHost and ToolStripDropDown controls can provide this for you:
private void button1_Click(object sender, EventArgs e) {
var helpInfo = new StringBuilder();
helpInfo.AppendLine("This is line one.");
helpInfo.AppendLine("This is line two.");
var textHelp = new TextBox() { Multiline = true,
ReadOnly = true,
Text = helpInfo.ToString(),
MinimumSize = new Size(100, 100)
};
var toolHost = new ToolStripControlHost(textHelp);
toolHost.Margin = new Padding(0);
var toolDrop = new ToolStripDropDown();
toolDrop.Padding = new Padding(0);
toolDrop.Items.Add(toolHost);
toolDrop.Show(button1, button1.Width, 0);
}
Result:
I think it will be a bad user experience to see a tooltip on click of a button. However, you can use this if you really want to
var b = new Button();
b.Click += (sender, args) => new ToolTip().Show("Help documentation", b.Parent, new Point(b.Location.X, b.Location.X + 10));
Last night, I was working on my project in C# by visual studio 2012. Suddenly I encountered a few errors from visual studio and then menu strip went into hiding. Now I haven't menu strip in my form and I lost all it visual option, but I have all it code in my formdesigner.cs file. I can't make all option again because it is hard and Time-consuming and I must create a menu strip by new names.and create all sub items by new names.
How I can resume my lost menu strip to form?
This is a part of my designer code:
this.Main = new System.Windows.Forms.ToolStripMenuItem();
this.userOptionTtm = new System.Windows.Forms.ToolStripMenuItem();
this.changePasswprdTSM = new System.Windows.Forms.ToolStripMenuItem();
this.CalenderOption = new System.Windows.Forms.ToolStripMenuItem();
this.CalenderOption2 = new System.Windows.Forms.ToolStripMenuItem();
this.CalenderOption1 = new System.Windows.Forms.ToolStripMenuItem();
this.hollydays = new System.Windows.Forms.ToolStripMenuItem();
this.ExitTsm = new System.Windows.Forms.ToolStripMenuItem();
this.useroption = new System.Windows.Forms.ToolStripMenuItem();
this.ReportsTSM = new System.Windows.Forms.ToolStripMenuItem();
this.loanListTsm = new System.Windows.Forms.ToolStripMenuItem();
this.FeutureJobsTSM = new System.Windows.Forms.ToolStripMenuItem();
and i have properties for all sub items of menu , that i was created (or defined ) previously. for example :
//
// Main
//
this.Main.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.userOptionTtm,
this.CalenderOption,
this.ExitTsm});
this.Main.Font = new System.Drawing.Font("Segoe UI", 9F);
this.Main.Name = "Main";
this.Main.Size = new System.Drawing.Size(62, 20);
this.Main.Text = "تنظیمات";
//
// userOptionTtm
//
this.userOptionTtm.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.changePasswprdTSM});
this.userOptionTtm.Font = new System.Drawing.Font("Segoe UI", 10F);
this.userOptionTtm.Image = global::TimeManagment.Properties.Resources._0079;
this.userOptionTtm.Name = "userOptionTtm";
this.userOptionTtm.Size = new System.Drawing.Size(165, 24);
this.userOptionTtm.Text = "تنظیمات کاربر";
this.userOptionTtm.Click += new System.EventHandler(this.chengePasswordTtm_Click_1);
and in my form code, I have all code of this menu. for example:
private void FeutureJobsTSM_Click(object sender, EventArgs e)
{
FeutureReportForm.isJobs = true;
FeutureReportForm fr = new FeutureReportForm();
fr.ShowDialog(this);
}
or
private void changePasswprdTSM_Click(object sender, EventArgs e)
{
chengePasswordForm cpf = new chengePasswordForm();
cpf.ShowDialog();
}
thx everybody. I added these lines to Form.Designer.cs file and my problem fixed.
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.menuStrip1.BackColor = System.Drawing.Color.Transparent;
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
{ this.Main,this.ReportsTSM });
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.menuStrip1.Size = new System.Drawing.Size(589, 24);
this.menuStrip1.TabIndex = 10;
this.menuStrip1.Text = "menuStrip1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.Controls.Add(this.menuStrip1);
private System.Windows.Forms.MenuStrip menuStrip1;
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);
}