System tray notification doesn't appear after execution via c# - c#

I am trying to create a sample for showing System Tray Notifications in a simple windows forms application. Basically, it looks like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Move(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
notifyIcon1.ShowBalloonTip(1000, "Important Notice", "Something important has come up. Click to view more", ToolTipIcon.Info);
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
}
After execution and minimizing, I can't find the icon on the right side in notification window.
Can you help me in order to visualize where I am wrong?
Thank you!

You have to set an icon manually to your NotifyIcon, otherwise it will not be shown (usually we think its default icon will be enough but unfortunately it is not!!)

Related

Putting web Browser control and tool strip in user control...cannot control them from main form

I am building a simple web browser in C#. In order to have all of my buttons such as the go, forward, back, refresh button, along with textbox input in a single tab, I have decided to put a tool strip and web browser control in a single user control that I created. This will enable me to just drop 1 control into a tab. Unfortunately when I try to use my user control it does not work. I know that my code inside the user control is fine, because when I had it in my main form, it functioned properly. I think the main piece that I am missing is I do not understand how to properly call the user control from the main form. Can someone guide me in the right direction here?
The main form.
namespace WebBrowser.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("random text.");
}
}
}
And the User Control
namespace WebBrowser.UI
{
public partial class adkinsBrowser : UserControl
{
public adkinsBrowser()
{
InitializeComponent();
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
webBrowser1.Navigate(toolStripTextBox1.Text.ToString());
}
}
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(toolStripTextBox1.Text.ToString());
}
}
}

How do I prevent a Form from opening multiple times in C#, while keeping a connection to the parent Form that came before it?

private void button4_Click(object sender, EventArgs e)
{
LogoutQuestion log = new LogoutQuestion(this);
log.Show();
}
This is the code in the Menu Form. Basically what I want to do is to ask the user if he wants to leave the program, and if so to close the LogoutQuestion Form and the parent, Menu Form. Any ideas on how to accomplish that?
namespace Project
{
public partial class LogoutQuestion : Form
{
Form FormParent = null;
public LogoutQuestion(Form parent)
{
FormParent = parent;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
this.FormParent.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
The above is the whole LogoutQuestion Form I spoke of. Any help will be greatly appreciated. :-)
Make LogoutQuestion a dialog(log.ShowDialog();) This way you can also
retrieve the result of the users response, since this will return a
DialogResult.
With ShowDialog you make the form modal. This means that it is tied to the parent form that showed it. This is just like when you try to save a file in other windows programs. This also means that the user can't proceed with anything else, until this form is closed. This also gives you the option to use the results of the users actions when the form closes.
private void button4_Click(object sender, EventArgs e)
{
LogoutQuestion log = new LogoutQuestion();
DialogResult dr = log.ShowDialog();
if(dr != DialogResult.Cancel)
{
this.Close();
}
}

Object reference is required for the non static field

namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
PongForm.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Can someone explain why I'm getting an error? I've had a look online and think it should work. I'm trying to change to a new form on button click.
In this function you should refer form, not PongForm:
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
Change "PongForm.Show();" to "form.Show().
To eloborate: you are attempting to call the class, not the instance you created.
to just add to what others have said. you probably don't want multiple of the same forms open. I cant comment or I would have done that instead. hope this solves your problem.
if (Application.OpenForms["PongForm"] != null)
{
Application.OpenForms["PongForm"].WindowState = FormWindowState.Normal;
Application.OpenForms["PongForm"].BringToFront();
}
else
{
PongForm form = new PongForm();
form.Show();
}

Multiple Window Problems - C# WPF

I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.
Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.
Here is code for my windows:
First Window
public partial class Presenation_1 : Window
{
Presentation_2 p2 = new Presentation_2();
MainWindow M = new MainWindow();
public Presenation_1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
}
Second Window
public partial class Presentation_2 : Window
{
Presentation_3 p3 = new Presentation_3();
Presenation_1 p1 = new Presenation_1();
public Presentation_2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p1.Show();
}
}
Third Window
public partial class Presentation_3 : Window
{
Presentation_4 p4 = new Presentation_4();
Presentation_2 p2 = new Presentation_2();
public Presentation_3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p4.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
}
Fourth Window
public partial class Presentation_4 : Window
{
Presentation_3 p3 = new Presentation_3();
MainWindow M = new MainWindow();
public Presentation_4()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
}
Thanks in advance
Don't create your Windows before the button is clicked, you can create them in the event handler:
private void btnForward_Click(object sender, RoutedEventArgs e)
{
var p2 = new Presentation_2();
this.Close();
p2.Show();
}
When you create a windows, you create 2 other windows with
new Presentation_X()
This new windows is automaticaly show and itself open 2 other windows.
You can create this windows once in the Mainwindow (auto hide this one), pass the reference in constructor and not close these windows. Quick example (not tested) :
public partial class Presenation_X : Window
{
private Window preview;
private Window next;
public Presenation_X(Window w1, Window w2)
{
this.preview = w1;
this.next = w2;
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.next.Show();
this.Hide();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.preview.Show();
this.Hide();
}
}

How to propagate the name change of Form's UI controls's throughtout all the .cs in project

In C# , Winform, I have created a form and bunch of UI controls on it. I have changed the name of the controls through Properties windows but the following automated generated code did not update automatically. However, the InitializeComponent code is automatically updated though. My problem is now that I don't remember which box or whihc label I renamed to certain name.. Two questions : How could I have done this more efficiently to begin with? Question 2) Is there anything I could do now to make it automatically change the corresponding names? I have heard of refactoring but I don't know if I could have used it here and how? I appreciate any help.
public partial class frmMyInterface : Form
{
public frmMyInterface()
{
InitializeComponent();
}
private void frmMyInterface_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Rename each these event handlers and then on the property window, reassign the events selecting from the dropdrown. Or delete these event handlers and double click on each event in the property window and this time it will update it for you

Categories

Resources