Disposing of Notifications C# - c#

I have an app where when there is a change to a label it creates a clickable notification. It all works perfectly however the notification icon stays in the system tray and you end up with loads of them. I know I can use the notification.dispose but I don't know where to use it. Here is the code:
private void label1_TextChanged(object sender, EventArgs e)
{
string strNumber = label1.Text;
if (strNumber.StartsWith("0") || strNumber.StartsWith("+44") & strNumber.Length < 17 & strNumber.Length > 8)
{
var notification = new System.Windows.Forms.NotifyIcon()
{
Visible = true,
Icon = System.Drawing.SystemIcons.Question,
BalloonTipTitle = "Click here to dial",
BalloonTipText = Clipboard.GetText(TextDataFormat.Text),
};
notification.ShowBalloonTip(5000);
notification.BalloonTipClicked += new System.EventHandler(notification_BalloonTipClicked);
// notification.Dispose();
}
}
I have commented out the notification.Dispose at the bottom because if I do that it disposes of it straight away and I can't click it. So I don't know where else I am supposed to add this code so that it removes the icon for it in the tray either when you click the notification or if you just wait and let it time out. I can't place it outside of this block of text as it's a local variable.
I'm a complete novice at coding so any help would be appreciated.

You need to subscribe to the events that the NotifyIcon presents.
Create functions for handling timeout and click then
notification.BallotTipClicked += MyClickFunction;
notification.BallotTipClosed += MyCloseFunction;
private void MyClickFunction(Object sender, EventArgs e) {
System.Windows.Forms.NotifyIcon notification = sender as System.Windows.Forms.NotifyIcon;
notification.Dispose()
}

Related

C# form show notifications without any tray icon

I want to use NotifyIcon, but I don't want any icons to appear in the lower right field, is this possible?
Or does anyone have an alternative solution suggestion?
I want to send a notification until the user sees the notification, the visibility will be turned on, but there will not be any icon in the lower right part. It will only appear in the notification panel on the right.
I tried to hide icon.But couldn't.
Set NotifyIcon.Visible = false
alternatively you could use the Windows Toast notification if you are using .net5.0 or higher.
As described on Microsofts site, it only works with specific TFs.
.net6 is given on MS Site, for .net5 you can use <TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>
A workaround would be to only show the NotifyIcon during the time the notification is displayed and hide it immediately after. Here's a complete example:
public Form1()
{
InitializeComponent();
notifyIcon1.Visible = false;
notifyIcon1.BalloonTipClosed += notifyIcon1_BalloonTipClosed;
}
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(1000, "Title", "Some text here", ToolTipIcon.Info);
}
private void notifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
notifyIcon1.Visible = false;
}

Pop-up window in a WPF application

I've a C# WPF application which show uses Grid control in the xaml(P screen).For every row in the grid, I've a column called Details.Clicking on item in this column shows a pop-up windows which also has a grid in the xaml(C screen).
My item click event in the P's viewmodel has the following code:
var myChildWindow = new MyGridView();
myChildWindow.Show();
If the user clicks on the item multiple times, I just want to highlight the existing C pop-up window.If there's no existing windows open, then only I want to open a new windows.
I've worked on a similar requirement for Winforms applicaiton.How do I go about this for a WPF application please?
Thanks.
First you'd need to declare myChildWindow outside of the click event so that it is accessible from multiple events. So,
MyGridView myChildWindow;
goes outside the click event, probably as a private variable.
Then, in your click event see if it's null, and if it is, create it.
if (myChildWindow == null)
{
myChildWindow = new MyGridView();
myChildWindow.Show();
}
You could keep a reference to the window and get rid of this when the window is closed:
MyGridView myChildWindow;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (myChildWindow == null)
{
myChildWindow = new MyGridView();
myChildWindow.Closed += MyChildWindow_Closed;
myChildWindow.Show();
}
else
{
myChildWindow.Activate();
}
}
private void MyChildWindow_Closed(object sender, EventArgs e)
{
myChildWindow.Closed -= MyChildWindow_Closed;
myChildWindow = null;
}

WPF Detecting Window Close Event and coupling with a button event

For two days, I have tried various methods for making it so that on loadButton click, it opens a secondary window and disables the loadButton; Then, when that secondary window has been closed, the loadButton will be re-enabled. Although, obviously all of my attempts have been unsucessful.
I have been reading about using the isClosing event, although, I haven't figured out how to properly implement it. So I decided to go with this route.
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
if (richWindow.IsActive != true)
{
loadButton.IsEnabled = false;
richWindow.Show();
}
else
{
loadButton.IsEnabled = true;
}
}
Issue here is, the first half is executed. Once I click the loadButton, it does disable. However, on closing the new Window, the loadButton is still disabled.
Could anyone point me in the right direction on where I need to go with this?
I think what you want is something like this:
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
richWindow.Closed += (s, e) => loadButton.IsEnabled = true;
loadButton.IsEnabled = false;
richWindow.Show();
}
Basically, disable the button before opening the window. Then, listen for the window to close and enable the button again.
richWindow.Loaded +=
{
loadedButton.IsEnabled = false;
};
richWindow.Closing +=
{
loadedButton.IsEnabled = true;
}

Do NOT hide soft keyboard while tapping controls in WP

I have this piece of code for button in my Windows Phone 8.1 Store App project (not the Silverlight):
private void CursorRightButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(QueryTextBox.Text)) return;
QueryTextBox.Focus(FocusState.Keyboard); //also i tried FocusState.Pointer
QueryTextBox.Select((TextBox.SelectionStart + 1) % (TextBox.Text.Length + 1), 0);
}
As you can see, I am tring to move cursor to right in text programmatically and the problem is that it hides soft keyboard and then shows it again after tapping button. I need to have keyboard on while tapping this button.
I tried to tinker with Focus() methods for sender and TextBox objects but I couldn't find any possible solution.
So the question is, how do you force keyboard not to loose focus/not to hide while tapping on controls?
I found out with Sajeetharans help that I need to set IsTabStop value on controls to false. Then keyboard will stay there. I did it in constructor of my page like this
public MainPage()
{
InitializeComponent();
CursorLeftButton.IsTabStop = false;
CursorRightButton.IsTabStop = false;
}
and my button method
private void CursorRightButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(TextBox.Text)) return;
TextBox.Select((TextBox.SelectionStart + 1) % (TextBox.Text.Length + 1), 0);
}
Add a loaded event to your container say grid,
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
set focus on the control , say a textblock
Txtblock1.Focus();
}
How To Programmatically Dismiss the SIP (keyboard)

Trouble with POPUP and Events in Windows Phone 7.1

I have a button that activate a service but before that service is activate i need to force the user to enter a text, for that task i create a user control that allow the user to enter text and validate it, on that control i have 2 buttons OK and cancel, OK button is working fine but the cancel is supposed to hide the popup, but the event don't get triggered, i need to hit the button several times before that happen, i am also getting a similar problem if the person hits back i cant get rid of the popup without closing the app. What is the best approach to work with popup and in this case why is the event is not getting trigger on the first hit
public partial class TicketView
{
Controls.TextInputBox textInputControl;
private void InitCloseTxtInput()
{
textInputControl = new Controls.TextInputBox(8, 0);
textInputControl.CancelBtn.Click += new RoutedEventHandler(CancelBtn_Click);
textInputControlForCamara.CancelBtn.Click += new RoutedEventHandler(CancelBtn_Click);
}
private void AppBarBtn_Click(object sender, EventArgs e)
{
InitCloseTxtInput();
PivotContainer.IsEnabled = false;
this.popup = new Popup();
this.popup.Margin = new Thickness(0, 150, 0, 0);
this.popup.Child = textInputControl;
this.popup.IsOpen = true;
}
void OkBtnOnPopUP_Click(object sender, RoutedEventArgs e)
{
//Call Service
}
void CancelBtn_Click(object sender, RoutedEventArgs e)
{
this.popup.IsOpen = false;
this.PivotContainer.IsEnabled = true;
}
}
If anyone have any out of question recommendation for this piece of code, Please tell me.
This line was the problem don’t exactly know why but when trying to re do de control i comment this line and all start to work just fine
this.popup.Margin = new Thickness(0, 150, 0, 0);
At least in the code, it does not seem like you are hooking CancelBtnPopUP_Click anywhere.
Is that the entirety of the code?

Categories

Resources