Creating balloon tooltip in C# - c#

Can i know how can i make a popup bubble message in my application coded in C#.
Like example, when i start my application, it'll popup saying "Welcome to UbuntuSE App".
And yea, The popup is not the message box popup, it's the popup in the traymenu.
Something similar to this:
PS,
If i'm not wrong, this is called Balloon Tooltips. But how can i use this in my codes.

If you're using Winforms you have the NotifyIcon class. This object has a ShowBalloonTip method which will show a balloon tip:
var icon = new NotifyIcon();
icon.ShowBalloonTip(1000, "Balloon title", "Balloon text", ToolTipIcon.None)

you must be looking for the Notify Icon Control
another CodeProject Example
here is a full example in MSDN

You can use the NotifyIcon control that's part of .NET 2.0 System.Windows.Forms.
Check : Using the NotifyIcon control
From the msdn,
NotifyIcon : Specifies a component that creates an
icon in the notification area. This
class cannot be inherited.

NotifyIcon.BalloonTipIcon

You have to set the property "icon" or it won't pop up
NotifyIcon ballon = new NotifyIcon();
ballon.Icon = SystemIcons.Application;//or any icon you like
ballon.ShowBalloonTip(1000, "Balloon title", "Balloon text", ToolTipIcon.None)

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace ShowToolTip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btBallonToolTip_Click(object sender, EventArgs e)
{
ShowBalloonTip();
this.Hide();
}
private void ShowBalloonTip()
{
Container bpcomponents = new Container();
ContextMenu contextMenu1 = new ContextMenu();
MenuItem runMenu = new MenuItem();
runMenu.Index = 1;
runMenu.Text = "Run...";
runMenu.Click += new EventHandler(runMenu_Click);
MenuItem breakMenu = new MenuItem();
breakMenu.Index = 2;
breakMenu.Text = "-------------";
MenuItem exitMenu = new MenuItem();
exitMenu.Index = 3;
exitMenu.Text = "E&xit";
exitMenu.Click += new EventHandler(exitMenu_Click);
// Initialize contextMenu1
contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });
// Initialize menuItem1
this.ClientSize = new System.Drawing.Size(0, 0);
this.Text = "Ballon Tootip Example";
// Create the NotifyIcon.
NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);
// The Icon property sets the icon that will appear
// in the systray for this application.
string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\setup-icon.ico";
notifyIcon.Icon = new Icon(iconPath);
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon.ContextMenu = contextMenu1;
notifyIcon.Visible = true;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipTitle = "Morgan Tech Space";
notifyIcon.ShowBalloonTip(1000);
}
void exitMenu_Click(object sender, EventArgs e)
{
this.Close();
}
void runMenu_Click(object sender, EventArgs e)
{
MessageBox.Show("BallonTip is Running....");
}
}
}

Thanks for the info!
Made something like this and worked!
private void NotifyBaloon(string text, string tooltip, string title, bool show)
{
notifyIconMain.Text = text;
notifyIconMain.BalloonTipText = tooltip;
notifyIconMain.BalloonTipTitle = title;
if (show)
notifyIconMain.ShowBalloonTip(1000);
}

Related

How do I access the text in a text box from a button click event handler

Im trying to write this simple winform menu and I need to add the contents of the NBox text box to a string so I can display it when a button is pressed, however I keep getting the error that NBox does not exist in the current context. So, how would i got about making the contents of the text box available at the press of a button?
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
public MainM(){
Text = "Adventures Main Menu";
Size = new Size(400,400);
//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
//Title Label
Label title = new Label();
title.Text = "ADVENTURE THE GAME";
title.Location = new Point(145, 30);
title.Size = new Size(200,60);
title.Font = new Font(defaultFont.FontFamily, defaultFont.Size, FontStyle.Bold);
//The main menu Buttons and all that jazz
Button credits = new Button();
Button start = new Button();
//Credits Button
credits.Text = "Credits";
credits.Size = new Size(75,20);
credits.Location = new Point(145,275);
credits.Click += new EventHandler(this.credits_button_click);
//Start Button
start.Text = "Start";
start.Size = new Size(75,20);
start.Location = new Point(145,200);
start.Click += new EventHandler(this.start_button_click);
//Control addition
this.Controls.Add(title);
this.Controls.Add(credits);
this.Controls.Add(start);
this.Controls.Add(NBox);
}
public void test(){
//The Main Window
}
private void credits_button_click(object sender, EventArgs e){
MessageBox.Show("Created by: Me");
}
private void start_button_click(object sender, EventArgs e){
this.Hide();
string name = NBox.Text;
MessageBox.Show(name);
//Process.Start("TextGame.exe");
}
public static void Main(){
Application.Run(new MainM());
}
}
//}
First, you need to name the control, that name will be its key in container's Controls collection :
//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
NBox.Name = "NBox"; //Naming the control
Then you will be able to retrieve it from the container:
private void start_button_click(object sender, EventArgs e){
this.Hide();
TextBox NBox= (TextBox)Controls.Find("NBox", true)[0];//Retrieve controls by name
string name = NBox.Text;
MessageBox.Show(name);
//Process.Start("TextGame.exe");
}
You declared NBox in consturctor and it is visible only inside constructor. You need to move it outside of constructor.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
TextBox NBox;
public MainM(){
Text = "Adventures Main Menu";
Size = new Size(400,400);
//NameBox
NBox = new TextBox();
...

NotifyIcon hides app from taskbar. How to avoid that?

I have an application that goes to system tray when I minimize it. I created a notify icon to handle some secondary options for the app using right click from mouse using that notify icon.
But I would like the application not to disappear from task bar when I minimize it and mantain the notify icon on system tray.
Is there any way to acomplish thtat?
EDIT: When I minimize the application, I use the Hide() command to use the NotifyIcon. But I want it to remain on task bar.
See code here:
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized) return;
Hide();
ShowInTaskbar = true;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(2000);
}
Note: This NotifyIcon is embeded on a WPF container programatically like this:
DrawNotifyIcon();
private void DrawNotifyIcon()
{
try
{
string source = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MainWindow)).CodeBase);
string tmpSource = source + #"\Resources\mainico.ico";
tmpSource = tmpSource.Replace(#"file:\", "");
// notify Icon
notifyIcon = new NotifyIcon
{
BalloonTipTitle = Cultures.Resources.Title,
BalloonTipText = Cultures.Resources.NotifyIconExecuting,
BalloonTipIcon = ToolTipIcon.Info,
Icon = new System.Drawing.Icon(tmpSource)
};
notifyIcon.DoubleClick += notifyIcon_DoubleClick;
notifyIcon.Click += notifyIcon_Click;
notifyIcon.MouseUp += notifyIcon_MouseUp;
// Create ContextMenu
contextMenu = new ContextMenuStrip();
contextMenu.Closing += contextMenu_Closing;
// Exit item
menuItemExit = new ToolStripMenuItem
{
Text = Cultures.Resources.Exit,
Image = Cultures.Resources.close
};
menuItemExit.Click += menuItemExit_Click;
// Restore item
menuItemRestore = new ToolStripMenuItem
{
Text = Cultures.Resources.Restore,
Image = Cultures.Resources.restore1
};
menuItemRestore.Click += menuItemRestore_Click;
// Active or inactive log
menuItemActive = new ToolStripMenuItem
{
Text = Cultures.Resources.On,
Image = Cultures.Resources.green,
Checked = true
};
menuItemActive.Click += menuItemActive_Click;
menuItemActive.CheckStateChanged += menuItemActive_CheckStateChanged;
// Order of appearance of ContextMenu items
contextMenu.Items.Add(menuItemActive);
contextMenu.Items.Add("-");
contextMenu.Items.Add(menuItemRestore);
contextMenu.Items.Add(menuItemExit);
notifyIcon.ContextMenuStrip = contextMenu;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Any idea on how to keep both icons for WPF?
Well, Its not possible to show forms in taskbar which is in hidden state. Still you can forcefully minimized the form. try below modified code :
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized) return;
this.ShowInTaskbar = true;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(2000);
this.WindowState = FormWindowState.Minimized;
}

C# NotifyIcon stays in tray after the Balloon Tip has been closed

after the Ballon Tip of my NotifyIcon has been close the icon still stays in tray.
It vanishes only when I hover it with my mouse cursor.
I have a class Notification.cs which has one component - NotifyIcon.
I call it from my Console Application as I only want to show the notification when certain conditions are fulfilled.
Some code:
how I call the notification from within my program:
Notification not = new Notification("Error occured, do this or that", "Some error", System.Windows.Forms.ToolTipIcon.Error);
not.getIcon().ShowBalloonTip(1000);
the notification class:
public Notification(string baloonTipText, string baloonTipTitle, System.Windows.Forms.ToolTipIcon icon) : this()
{
this.icon.Visible = true;
this.icon.BalloonTipText = baloonTipText;
this.icon.BalloonTipTitle = baloonTipTitle;
this.icon.BalloonTipIcon = icon;
}
public System.Windows.Forms.NotifyIcon getIcon()
{
return this.icon;
}
private void icon_BalloonTipClosed(object sender, EventArgs e)
{
this.icon.Visible = false;
this.icon.Dispose();
}
Any ideas?
Because you're running in a console application, your icon_BalloonTipClosed handler will not be invoked when the balloon tip is closed (no message pump). You will need to call Dispose manually either as your application exits or set a timer (System.Threading.Timer, System.Windows.Forms.Timer will not work) with a timeout longer than the balloon tip's timeout. e.g.:
timer = new Timer(state => not.getIcon().Dispose(), null, 1200, Timeout.Infinite);
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace ShowToolTip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btBallonToolTip_Click(object sender, EventArgs e)
{
ShowBalloonTip();
this.Hide();
}
private void ShowBalloonTip()
{
Container bpcomponents = new Container();
ContextMenu contextMenu1 = new ContextMenu();
MenuItem runMenu = new MenuItem();
runMenu.Index = 1;
runMenu.Text = "Run...";
runMenu.Click += new EventHandler(runMenu_Click);
MenuItem breakMenu = new MenuItem();
breakMenu.Index = 2;
breakMenu.Text = "-------------";
MenuItem exitMenu = new MenuItem();
exitMenu.Index = 3;
exitMenu.Text = "E&xit";
exitMenu.Click += new EventHandler(exitMenu_Click);
// Initialize contextMenu1
contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });
// Initialize menuItem1
this.ClientSize = new System.Drawing.Size(0, 0);
this.Text = "Ballon Tootip Example";
// Create the NotifyIcon.
NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);
// The Icon property sets the icon that will appear
// in the systray for this application.
string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\setup-icon.ico";
notifyIcon.Icon = new Icon(iconPath);
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon.ContextMenu = contextMenu1;
notifyIcon.Visible = true;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipTitle = "Morgan Tech Space";
notifyIcon.ShowBalloonTip(1000);
}
void exitMenu_Click(object sender, EventArgs e)
{
this.Close();
}
void runMenu_Click(object sender, EventArgs e)
{
MessageBox.Show("BallonTip is Running....");
}
}
}

C# Tooltip on TabPage

How can I show Tooltip text on my own TabPages extends from TabPages just added some extra variables and using DrawItem event.
I've generate them in method.
ISIMtabPage newTab = new ISIMtabPage();
// setting up newTab
string contactName = getContactName(object);
chatFormTabs.TabPages.Add(newTab);
toolTip1.SetToolTip((ISIMtabPage)chatFormTabs.TabPages[chatId], contactName);
But if I hover on the tab it doesn't do anything.
I've TabControl.ShowToolTips enabled.
What should I do?
Thanks in advance.
I would check toolTip1.SetToolTip
example for Tooltip use:
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private void MyTabs()
{
this.tabControl1 = new TabControl();
this.tabPage1 = new TabPage();
this.tabPage2 = new TabPage();
this.tabControl1.Controls.AddRange(new Control[] {
this.tabPage1,
this.tabPage2});
this.tabControl1.Location = new Point(35, 25);
this.tabControl1.Size = new Size(220, 220);
// Shows ToolTipText when the mouse passes over tabs.
this.tabControl1.ShowToolTips = true;
// Assigns string values to ToolTipText.
this.tabPage1.ToolTipText = "myTabPage1";
this.tabPage2.ToolTipText = "myTabPage2";
this.Size = new Size(300, 300);
this.Controls.AddRange(new Control[] {
this.tabControl1});
}
public Form1()
{
MyTabs();
}
static void Main()
{
Application.Run(new Form1());
}
}
Actually kinda tricky, so make sure that from the tabControl1 properties "ShowToolTips" is True.
the form1 has a tooltip1 and a TabControl named tabControl1 with multiple tabs for testing
tabControl1 has a collection of tabs
edit the collection and set the tab you want to have the tooltip "ToolTip on toolTip" and ToolTipText to "your message here"

Disable Minimize/Maximize component from RichTextBox

This maybe a simple question...I want to disable Minimize/Maximize component from RichTextBox.
I tried to check the properties of RichTextBox, I couldn't succeed to disable these components.
I want to keep the x close component but I want to disable Minimize/Maximize component from RichTextBox.
private void button1_Click(object sender, EventArgs e)
{
ShowRichMessageBox(FileSelected, File.ReadAllText(FileSelected));
}
private void ShowRichMessageBox(string title, string message)
{
RichTextBox rtbMessage = new RichTextBox();
rtbMessage.Text = message;
Form RichMessageBox = new Form();
RichMessageBox.Text = title;
RichMessageBox.Controls.Add(rtbMessage);
RichMessageBox.ShowDialog();
}
The idea is to display a message in a RichTextBox of the read file.
Use the MinimizeBox and MaximizeBox properties of the Form.
Try this:
private void ShowRichMessageBox(string title, string message)
{
RichTextBox rtbMessage = new RichTextBox();
rtbMessage.Text = message;
Form RichMessageBox = new Form();
RichMessageBox.Text = title;
RichMessageBox.MaximizeBox = false;
RichMessageBox.MinimizeBox = false;
RichMessageBox.Controls.Add(rtbMessage);
RichMessageBox.ShowDialog();
}
RichTextBox control doesn't have Minimize button, only form has it. If you wont to remove form minimize and maximize button add this lines before showing a form :
RichMessageBox.MaximizeBox = false;
RichMessageBox.MinimizeBox = false;

Categories

Resources