WinForms - Can't add picture to picturebox - winforms

I pretty much just started using Windows forms and I'm trying to add a picture to a picture box. First I tried the most obvious way, that is - clicking on the little arrow in the top right corner of a picturebox and clicking "choose image" in the small dialog box, but no matter what I do, the "choose image" button does utterly nothing. I know it should open some dialog box where I could choose my picture, but litterally nothing happens, not even an error message pops up.
Next I tried importing the picture through the properties of the picturebox, but after clicking on the three dots next to the image property an error message pops up, saying "value cannot be NULL, Name o parameter: docData".
I tried following this answer (How to attach a resource (an image for example) with the exe file?) and going to the properties of my project and then to the resources tab, but I have this tab completely empty, saying just that my project does not contain any default resources and a "click to create them" button. However when I click it, another error message appears saying "system cannot find the file. Exception based on value HRESULT:0x80070002"
I guess the problem must be somewhere in my resources file (which might be missing or whatever), but I have a default .resx file there that I haven't touched since the creation of this project which I had thought should be fine.
Afterall I pretty much just started with WinForms and I don't have much clue what's going on and this is quite a significant setback for me. I would be grateful for any help.

Related

Form1 [Design] loads a blank template after renaming an event handler

In VS2013, I renamed one of my button event handlers and wanted to drop into the [Design] to change the property. I got a huge warning telling me that things are not as they seem (expected, since I changed the delegate's name) so I hit Ignore and Continue. This is the first time I've hit this button.
To my horror, what I saw was ... a blank template as opposed to my original winform. I have backups of my code, so I'm not too worried, but I was wondering how I'd go about restoring my Form1.cs[Design] without going back a code rev.
This is what my [Design] looks like now:
Did your error say this?:
By renaming the delegate in your code file, you hung the designer out to dry. Everytime you switch to the designer tab, it parses InitializeComponent() to draw your controls for editing, and since the reference to the original callback doesn't exist anymore, the designer doesn't know what to do when it encounters the unknown reference. By forcing its hand, it simply craps out like it was telling you it was going to do. The blank panel is just a result of it failing and drawing nothing.
Pop open your Form1.Designer file and find where the button's properties are set. The unknown reference is probably highlighted in red. Change the name to whatever you changed in your other file. Here's an example, but with a panel instead of a button:
And then your designer should return to normal. No worries about rolling anything back, it's a minor fix.

Tree node selection in winform app not working properly

I am working in Winform app (ticketing app) where people can create support ticket,close etc.
once you select a particular tree node in left pane (either manually or programatically) it's associated control/view gets displayed in right pane of form.
In few scenarios, even though the particular node is getting selected properly (though code) it's associated control not getting displayed/loaded. To brief,
Under root node, I have a open incident, once I close the incident it disappears from opened section and gets added to history section under treeview.
once click on "create incident" button a new tree node gets created with new ticket number and it's associated control gets displayed on right side of form.
So problem is, if I close a open incident and then click on "create incident"; even though new node getting created and it's associated control getting loaded but the control not getting displayed; instead the closed incident view only shown.
Somehow, the new nodes control getting hidden/jammed inside.
I am new to winform/treeview control.I debugged to see if anything going wrong but couldn't find any; neither Google'ng helped me.
Anyone here have any idea what's going wrong (OR) have you faced the same issue. Please help and let me know how to solve it.
PS: Sorry can't post any code and I know without code it's bit difficult to answer but if you can provide any pointer that would help me too.
When you select a treenode and the associated control is not getting loaded, one of four things is probably happening:
The code to view the control is not executed,
The code to view the control is being executed but does not work properly,
The code to view the control is working properly but it is being executed again or hide the control, or
The code to view the control is working properly but some other code is being executed to hide the control.
Set breakpoints in the code to view the control to see which of these is the problem. Once you have narrowed it down, it should be easier to fix.

Page in WPF losing focus

I have a Page in WPF which has a text box. This text box is set to be focused on load.
There is an application running in the background which takes a string value of a biometrics scan result and places it in that text box. This is a 3rd party application, and we have been told that it looks for a text box with a certain name, in an application with a certain name and if that text box is focused on, it will write that string in.
This works perfectly fine, but below is the problem I am having.
We have another piece of software which is a custom on screen keyboard. The text box can also be written into so the user can click a button to bring up this keyboard then start typing. However when this keyboard comes up, it seems to lose focus to my application and it doesnt come back again. For the lifecycle of our application running, the biometrics doesn't work because it doesn't see my application as the one currently in focus.
How do I force my application to come back in focus, but only when I want it to. I don't want it ALWAYS to be in focus and on the top, else the custom keyboard would never show.
I looked into Application.Activated Event with no joy, and even tried the following
public static extern bool SetForegroundWindow(IntPtr hWnd);
Getting a but stuck, does anyone have any advice?
Have you tried Activate() and Focus() methods?
This thread has a bunch of answers on topic Bring a window to the front in WPF
This particular one worked for me https://stackoverflow.com/a/7559766/305020

In forms application, is there any Alternative to MsgBox?

I like how MsgBox stops all further processing until it's clicked but I don't like how it pops that little msgbox in the middle of the screen. Is there a way to do something like a msgbox but keep it on my Form (which is always in view) so I can go ahead and click a button on the form instead of having to bring the little msgbox window on top of windows that may be covering it.
I use the msgbox to inform me that a certain situation has happened, which I manually fix and when I'm done I click the MsgBox to continue processing. It'd be nice to have this button right on the form.
which I then have bring to the front if there is a window covering it
That shouldn't happen, but can happen if you display the message box from a thread in your program. The window has the desktop as the parent and has no Z-order relationship with the windows in your user interface. And yes, can easily disappear behind the window of another app, including your own.
There's a MessageBoxOptions option that isn't exposed in Winforms, MB_TOPMOST, which ensures the window is top-most. You'd use it like this:
MessageBox.Show("text", "caption", MessageBoxButtons.OK,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
(MessageBoxOptions)0x40000); // use MB_TOPMOST
But by far the best thing to do is to display the message box on your UI thread. Use Control.Invoke() to do so. That way the other windows of your app are disabled, no way for the user to not notice the box.
Still one problem with this, the user won't expect the box to show up since it is shown asynchronously from anything she does. Which means the box can easily get dismissed by accident when the user just happened to press the Enter or Space key. Or clicked at just the wrong spot. Nothing much you can do about that.
Centering the box in your main window is technically possible, but fugly to do. Check this answer.
Do you mean that the form shall exchange its contents with a message plus an OK button?
This would be similar to the way a text mode interface typically works.
You can make it happen by adding a disabled panel or UserControl with message and button topmost on the form and enable it when you wish to alert the user. However, I am puzzled how to implement the blocking behavior similar to MessageBox.Show() and Dialog.Show().

WPF PrintVisual - Selecting XPS in print dialog causing errors

I've spent some time searching for related topics on this, but haven't found any...
My problem is that I am getting a few errors when trying to select the Print to Microsoft XPS Document Error.
If I choose Print, I'll get the Save dialog. If I select a file and hit OK, it'll properly save my XPS file, but I noticed my WPF window turns all white, like it crashed. When trying to open up the XPS file from explorer, the content is correct.
If I choose Print, and then hit Cancel on the Save dialog, I'll get "Win32 error: The data area passed to a system call is too small".
The code I am doing is
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == true)
{
dialog.PrintVisual(this.myStackPanel, string.Empty);
}
I have seen other posts about writing much more in depth printing code, but right now this all the code is doing (I inherited this from another developer). Does anyone have a fix for this issue? Thanks.

Resources