Sharing objects between classes - c#

I have a program written in c# that contains a listbox with an observable collection. I want a balloon to pop-up for the user (in the system tray) when the collection has an item added. I have a class that contains a method for notifying the listbox when an item is added (see below):
void OnFileCreated(object sender, FileSystemEventArgs e)
{
if (!base.Dispatcher.CheckAccess())
{
base.Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(FileSystemEventHandler)OnFileCreated,
sender, e);
}
else
{
// Ignore new directories.
if (File.Exists(e.FullPath))
{
Debug.WriteLine("File Created: " + e.FullPath);
_files.Add(new ObservableFileInfo(e.FullPath));
//Alert users to new request
string title = "Access Request";
string text = "A new access request has been submitted";
//show balloon with built-in icon
tbi.ShowBalloonTip(title, text, BalloonIcon.Error);
}
}
}
The code works exactly as intended apart from the fact that the balloon will only show if I create a new instance of the balloon within the OnFileCreated method (not seen above as I took the code out). For reference, I am using the system tray icon .dll from the following project: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon?fid=1540774&select=4624878&fr=51#xx0xx
My issue is that I already have a system tray icon initiated in the MainWindow class, but I cannot call on it to show a balloon from my OnFileCreated method. I am not sure how I can "share" this information across classes.
If anyone has any ideas that would be great.

I ended up using FindResource to locate the resource defined in the xaml resource document. This was specified in the project code I was referencing to, however FindResource was not working by itself, I needed to add App.Current:
notifyIcon = (TaskbarIcon)App.Current.FindResource("NotifyIcon");
notifyIcon.ShowBalloonTip(title, text, BalloonIcon.Error);

Related

ContextMenu.SourceControl property doesn't exists

I am making a WPF .NET 4.8 Exe application.I placed an TreeView control and made a void that is firing when window is loaded.This void adds all the sub directories and all of the files in all the sub directories and adds them to TreeView as TreeViewItem, and when they are creating their ContextMenu property is binding to a premade ContextMenu control.It also binds a click event to the MenuItems that ones ContextMenu contains.
I scripted the click event's void as RoutedEventArgs and wrote the code below (tried to access the TreeViewItem i created before).
I made some changes in my code to fix that problem but it did not work.
The code before:
void showInExplorerFolder_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("event triggered");
MenuItem menuItem = (MenuItem)sender;
//string tag = menuItem.Tag.ToString();
ContextMenu parent = (ContextMenu)menuItem.Parent;
var baseParent = parent.SourceControl;
Console.WriteLine("got success parent; ; ; ; ; ;" + menuItem.Name);
}
The code after:
void showInExplorerFolder_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("event triggered");
MenuItem menuItem = (MenuItem)sender;
//string tag = menuItem.Tag.ToString();
ContextMenu parent = (ContextMenu)menuItem.Parent;
TreeViewItem baseParent = (TreeViewItem)parent.SourceControl;
Console.WriteLine("got success parent; ; ; ; ; ;" + menuItem.Name);
}
And after theese changes i could not find any solution.
And the error code is here:
CS1061 'ContextMenu' does not contain a definition of 'SourceControl' and no inaccessible extension method 'SourceControl' is available that accepts a first argument of type 'ContextMenu' (maybe you are missing a usage or annotation reference?)
I could not find any solution.If anyone could help me, i will be thankful.
What you have done here is mixed WinForms code sample with WPF application.
To solve your problem you need to change
(TreeViewItem)parent.SourceControl; which is WinForms as SourceControl is part of ControlMenuStrip which lives in System.Windows.Forms.dll MSDN link, this is very important as WPF doesn't use that dll. It is a separate technology which uses different operating system components to render UI. For example WinForms is using GDI+ where WPF uses DirectX to render onto screen. Because of that you can't mix the 2 together as they are fundamentally different.
Solution
Use this ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as it is using WPF Controls and it is the equivalent of SourceControl. Here is a link to MSDN that will show you, it is in a different assembly.
Hope this helps

Updated icon image won't show in document tab

I just found DockPanel Suite and am learning to use it.
I downloaded v2.9 and am using it with C# in VS2013. I created a simple Windows Forms MDI app. It has one type of child form which is a simple form with only a rich text box control in it. I loaded a few of these as documents. I added an icon to the document's tab to indicate whether the rich text box text has been saved or not.
I change the tab icon anytime the user types in the rich text box through the use of the text_Changed event:
private void txtCode_TextChanged(object sender, EventArgs e)
{
//The text has changed, set the warning icon.
this.Icon = MyApp.Properties.Resources.Warning;
}
The problem I have is that the icon does not update when running at full speed. It updates fine when I am single stepping through the above event. It also updates fine when I load a file within the form's Load event;
private void frmCode_Load(object sender, EventArgs e)
{
//Get the full file path.
string sFile = Path.Combine(cCoCoIDE.CodeBaseRootFolder, Path.GetFileNameWithoutExtension(cCoCoIDE.CodeBaseFile), this.Text);
//Load the source file into the code window.
//A few validations.
//Make sure the file exists.
if (File.Exists(sFile))
{
//File is there. Is it empty? Load only if not empty.
if (new FileInfo(sFile).Length > 0)
{
txtCode.LoadFile(sFile);
//I had to add the following because the text changed
//event fires when I load the file and I need to start
//off with the green checkmark icon.
this.Icon = CoCoIDE.Properties.Resources.GreenChk;
}
}
}
I tried the following after I change the icon (in the Text Changed event):
this.ShowIcon = true;
this.Refresh();
this.Update();
Note: I tried these one at a time. The above is just a list of methods.
Can anyone help me with this? Thank you! SgarciaV
I found the solution. See here:
Dockpanel Suite: Image not updating

Set ADXOlExplorerLayout as DockRight

I am developing an Outlook plugin using add-in-express. I have added an adxOlFormsManager there. Which contains a Forms collection named ‘adxOlFormsCollectionItem1’. I have set the form class name to as “FlowOutlook.Plugins.Chat.ChatExplorerPane”. Yes, ChatExplorerPane is my ADXOlForm.
I implement a custom event for the my ADXOlform using following code:
private void AddinModule_AddinStartupComplete(object sender, EventArgs e)
{
try
{
var currentChatTypeForm = AddinModule.CurrentInstance.adxOlFormsCollectionItem1.FormInstances(0) as ChatExplorerPane;
currentChatTypeForm.OnChatTypeSelected += currentChatTypeForm_OnChatTypeSelected;
}
catch (Exception ex)
{
Debug.DebugMessage(2, "AddinModule : Error in AddinModule_AddinStartupComplete() : " + ex.Message);
}
}
My Problem is :
If I explorer layout as “RightSubpane” this works fine. But after I changing the explorer layout as dock right (using Properties window), adxOlFormsCollectionItem1.FormInstanceCount is 0. (Which means “currentChatTypeForm” will be null).
What I am supposed to do :
private void ChangeExplorerLayout(AddinExpress.OL.ADXOlForm form)
{
if (form == null) return;
form.XXX = AddinExpress.OL.ADXOlExplorerLayout.DockRight;
}
I wrote above method to change the explorer layout by code. I hope I may be able to call that safely inside AddinModule_AddinStartupComplete, after initializing my custom method. But I need to know the code for replacing ‘XXX’ to complete the method and have a try.
Please advice me to change explorer layout as dock right according to my requirements.
Kushan Randima.
Below is a citation from the manual - see section Accessing a Form Instance in the PDF file in the folder {Add-in Express}\Docs on your development PC.
It is essential that Add-in Express panes are built on the windowing of the host application, not on the events of the application's object model. This means that getting an instance of an Add-in Express pane in a certain event may result in getting null (Nothing in VB.NET) if the call is issued before the pane is shown or after it is hidden. For instance, it is often the case with WindowActivate/WindowDeactivate in Excel, Word, and PowerPoint.
...
So, you may encounter a problem if your add-in retrieves a pane instance in an event above. To bypass this problem, we suggest modifying the code of the add-in so that it gets notified about a pane instance being shown or hidden (instead of getting the pane instance by handling the events above). Use the ADXBeforeTaskPaneShow event of the task pane class (Excel, Word, and PowerPoint) and the ADXOlForm.ADXBeforeFormShow (Outlook) event to be notified about the specified pane instance being shown. When the form becomes hidden, you'll get ADXOlForm.ADXAfterFormHide (Outlook) and the ADXAfterTaskPaneHide event of the task pane class (Excel, Word, and PowerPoint).
That is, instead of getting a form instance in the AddinStartupcomplete event, you can handle the ADXOlForm.ADXBeforeFormShow event.
Hope this helps.

C# Dragdrop events not working

i created a little form in C# and registered some drag drop events to it. But i cant get it to work properly!
Goal: Dragging files from explorer.exe into the form, and handeling it, and photos dragged into the form. I test the code by dragging a .txt file from my desktop into the form. It shows the (/) cursor when doing so
Code:
public partial class Viewer : Form
{
public Viewer()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Viewer_DragEnter);
this.DragDrop += new DragEventHandler(Viewer_DragDrop);
}
public void Viewer_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragEnter");// Line has breakpoint
}
public void Viewer_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragDrop: "+e.Data.GetFormats());// Line has breakpoint
}
}
What i have tried:
Try-catch over the constructor => nothing
Running succesfull build outside of visualstudio(2010)
Running explorer.exe as administrator when dragging items into the form
Thanks for reading my question
EDIT:
when testing the files were on the desktop(%userprofile%\Desktop). but it shouldnt matter where they are stored. the images are supposed to be dragged from the browser or a word document into the form
ANOTHER EDIT:
When trying to run it without visual studio, i get an InvalidOperationException, that occurs on the AllowDrop = true; but i dont get a reaction when try-catching it
SOLUTION:
OMG... so.... i kinda moved the public static Main() into the viewer.cs file.... and i forgot to add the STAThread attribute.
I found this when executing the build outside of visual studio again, and in the exception form it showed that the thread needed to be a STAThread but that part of the message was hidden and hard to find
anyway: ALWAYS USE STATHREAD :P

Silverlight 4 application freezes without throwing any exception

I'm developing a Silverlight 4 RIA application. There is a DataGrid storing data and two buttons: add a new item and remove an item. After creating a new item for the second time the application freezes like this - I'll explain the strange behaviour below.
The scenario of creating a new item looks like this:
After clicking, the child window appears. The reference to the domain data source used on the parrent page is being
passed to the child window in the constructor.
The user chooses a file.
The file is send to a web service. In response the web service returns some data from that file.
A new data object is being created and inserted to domain data source.
The child window causes the entire application to freeze only when it's called twice, but the first call requires object creation. I can open and close the child window repeatedly and everything will work fine until a sequence of: open.create -> open.close / open.create occurs. I tried to trace all exceptions with VS tool (alt ctrl e) but there are none.
A breakpoint on
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
does not show anything either. Any ideas?
Page code.
Child window code
Remove the following and everything will be fine.
private void ChildWindow_Closed(object sender, EventArgs e)
{
this.DialogResult = false;
}
And to evaluate a bit more, ChildWindow_Closed is the outcome of setting the DialogResult at the first place.
By re setting it unexpected things happen.
I did a bit more research after you helped me with this issue. Seems its a SL4 bug.
This should also help. Topic about this on SL forums.
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
}

Categories

Resources