Question on popup window - c#

In my code I am using a popup window to display extra information. I also have the ability to export the information in the main window to Excel.
The problem is, after the window pops up -> I see the info -> I close the popup window -> but if I try the export to Excel button, it throws the exception "null object referrence" (if I use a try/catch, the exception doesn't occur - but I don't get any information).
In the export function I am doing something like this:
{
//some code .... here
con.close();
session["dss"] = mydataset;
}
In the export button click event:
system.data.dataset dss = (system.data.dataset)session["dss"];
//then some work on this
I think, probably when the popup window opens it ends the execution and that's why when I come back to the main window and try the export button the values for the tables and all goes out of scope.
Also, if I refresh the main page after closing the popup window I don't have any issue and can export the data.
Can you please help me with this?
Thanks,
Rahul

How are you opening the popup window? Javascript? A link?
I think, probably when the popup
window opens it ends the execution
This can't be true, any opening of pop-ups happens client-side this can't possibly stop any execution server-side.
it throws the exception "null object
referrence"
Which object? Have you instantiated it? If so have you possibly nullified it?
if I use a try/catch, the exception
doesn't occur - but I don't get any
information
This isn't true - I expect you're just handling the exception (within your catch) so that it no longer being left unhandled.
If you can answer these questions I suspect you'll figure out the problem yourself. However I suspect you're posting back (and getting the server to provide the popup code) and by doing this you're inadvertently affecting your excel object. So direct your attention there.

Related

Designer error when redirecting a button click within a custom user control

I've created a custom user control, that is essentially a custom button within my windows form app. I managed the redirect of the click event to using the following code:
Control[] customButtonControls = button.Controls.Find("buttonInUserControl", false);
Button nestedButton = (Button)customButtonControls[0];
nestedButton.Click += new System.EventHandler(this.button_click_handling_function);
I've appended this to the Window_Name.Designer.cs file below the generated code for the control with my button_click_handling_function being defined in my Window_Name.cs file.
The issue is that when I then click back to the Window_Name.cs[Design] page, I am met with an error page. I will include screen shots to better show the errors. Basically it is a super unhelpful page. It tells me that I have an index out of range error on my Array, but the stack call makes no sense.
If I try to build my Solution, I am met with NO compile errors and my program acts exactly as intended. The click event triggers the function just as before.
Thanks in Advance.
Portions of the designer code are run at design-time. The index out of range error is probably because at design-time there are no controls found yet by that Find call so the array is empty. You are not checking for 0 length so when you de-reference it you get the error.
It works at run-time because at that point the controls have been instantiated.
The secondary problem though is you should not put things into the Designer.cs files since that code is auto generated by the designer and could be regenerated at some point and your added code lost. Put that code in the Window_Name.cs after the InitializeComponent call.

Odd behaviour when opening a WPF Window from WinForms

When displaying a WPF window from an Excel addin, I'm encountering odd behaviour whenever I show it with myWindow.Show() rather than myWindow.ShowDialog(). Thus far everything has worked fine when using the latter. However, it would be nice to be able to display a window such that the user can interact with Excel at the same time - i.e. the behaviour I'd expect from Show().
The problem is that controls in my form start acting very oddly quite quickly. ComboBox dropdowns collapse immediately, and textbox input ends up in whatever cell is selected in the Excel worksheet that's active.
I've noticed that with ShowDialog, Snoop is able to attach to my window as well, whereas with Show, I get an error amounting to "Could not find a PresentationSource to attach to". I'm not, however, completely sure if that's related.
Obviously one solution would be to stop directly showing a WPF window from WinForms; I expect the problem to largely go away if I change my window into a UserControl and chuck it into an ElementHost. However, I'd rather avoid that if I can.
Current code (roughly)
public void DoOpenWindow(Office.IRibbonControl button)
{
var myWindow = new myWindow();
// This hasn't addressed the issue, though may be sensible to include:
//ElementHost.EnableModelessKeyboardInterop(myWindow);
// This *also* didn't work, and essentially set my window to
// be always on top of Excel
//var hwSrc = HwndSource.FromVisual(myWindow );
//var ownerHelper = new WindowInteropHelper(myWindow );
//ownerHelper.Owner = (IntPtr)Globals.ThisAddIn.Application.Hwnd;
// with ShowDialog() this works fine...
myWindow .Show();
}
Current thoughts are:
I'm getting window messages from Excel forwarded to myWindow, some of which it isn't expecting.
Excel is intercepting messages meant for my window (keyboard and mouse), which is probably what ElementHost.EnableModelessKeyboardInterop(myWindow) is intended to solve (but either I'm using it wrong, or it's not the whole solution).

MMC DisconnectedContext error

I have an issue with a MMC snapin developed using C#. Using the code from Microsoft's MMC sample for an snapin using a property page I have noticed a DisconnectedContext error popping up after displaying a modal messagebox from a property page.
I can reproduce this every time, and I can "fix" it with a bit of a hack.
using the sample code for CanApplyChanges() in the property page's user control, I display a messagebox if there is a validation error:
public bool CanApplyChanges()
{
if (this.password.Text != this.passwordConfirm.Text)
{
MessageBoxParameters mbp = new MessageBoxParameters();
mbp.Text = "Passwords do not match";
userPropertyPage.ParentSheet.ShowDialog(mbp);
}
}
Commenting out this messagebox stops the error. I can change this to display the messagebox parented to the MMC console instead, so (I assume) it doesn't block the property page message pump, but obviously this allows the user to continue interacting with the page which is not the best UI.
Has anyone else seen this problem - I open the property page, cancel it, open it again, make it pop the modal messagebox (using the recommended way of displaying them) when you click the Apply button, then click cancel and re-open the page - I always get the DisconnectedContext error.
I don't do anything fancy in the constructor or RefreshData() methods of my control - I fetch simple data from an EntityFramework object and place it in a few textbox and checkbox controls.
Obviously there is a problem with the thread that show the property sheet interacting badly with the rest of the snapin, but I have no threads of my own, and there is no interaction between my control and the only place I interact with the parent property sheet is to call that ShowDialog method.
Any ideas will be welcomed at this point, as would any information leading to a successful apprehension of the bug!

C# Custom Form throwing COMException on exit

I've created a custom form that has a couple buttons and a Text box.
This custom form is opened by a click event button I have created on a Microsoft outlook add-in that I am working on attached to a 'Ribbon'. (Not sure if this matters, no problem here).
On the windows form, The first button simply saves the contents of the text file to disk.
The other button attempts to close the form. In this buttons click event I have tried the following two lines, separate and together
this.Close(); and this.Dispose();
When I use this button or the Form exit (the 'x' located in the upper right of a windows form) I receive the follow error,
COMException was unhandled by user
Exception from HRESULT: 0x800A01A8
When this error is thrown, it takes me to the 'Connect.cs' files following method,
public void OnBeginShutdown(ref System.Array custom)
{
this.toolbarButton.Delete(System.Reflection.Missing.Value);
this.toolbarButton = null;
}
I'm not sure how to begin troubleshooting this. I have done a fair amount of research but unfortunately haven't found much. I'm sure the problem might be the fact I have created a custom form with no experience and there are some 'housekeeping' or 'best practices' that I have not done or are aware of.
Anyone have insight into this?
I'm guessing a little here but that HResult for a COMException means Object Required,
So, when you are calling CommandBarButton.Delete one of two things is wrong.
Either you have already disposed the button instance or you should be passing true or false to the Delete call.
The button shouldn't be disposed until after it has been removed from the toolbar, and when it is disposed you may need to do a Marshal.ReleaeComObject to dereference it properly.

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

Categories