Windows Form not recognized C# - c#

I am trying to add a new form to look back at printed labels. Simple form to keep track of the Job# printed. However when I try and code the button click to showdialog for the form, nothing happens. It does not recognize that I created the form at all.
I added this EditProductsForm above the JobRecord just to see if there was something wrong with the Button Click. This works fine. On my screen the EditProductsForm is light blue and is shown in the prediction.
EditProductsForm editProductsForm = new EditProductsForm();
editProductsForm.Show();
The JobRecord is not show in prediction and has the red line underneath showing an error?!
JobRecord jobRecord = new JobRecord();
jobrecord.Showdialog();
Any help as to what might have been changed?

You haven't imported the proper namespaces where those class file resides like
using <namespace_name_where_form_class_defined>;

Related

C#/WinForms tooltip stops displaying

I'm facing the following issue during setting up tooltips on a WinForms / C# desktop application (using .NET Framework version 4.5).
Application has hundreds of form elements where I would like to display a tooltip.
Current implementation as the following:
I have one toolTip object on my main form
Language file has been loaded and saved in an array
There is a method which suppose to assign the tooltip texts to the different elements accordingly by calling the SetToolTip method on the toolTip object.
E.g.
toolTip.SetToolTip(backBtn, LocalizationFile[0]);
toolTip.SetToolTip(myTextBox, LocalizationFile[1]);
It works fine, tooltips are displayed correctly.
As soon as I reach ca. 30 calls it stops working.
Calling ~30 times SetToolTip method to setup the required tooltips, cause to completely stop displaying the tooltips.
The previously worked tooltip texts are not getting display anymore.
There is no exception or any error message.
Can you please explain me why the toolTip object just stops displaying the texts after calling the SetToolTip method several times? Is there any workaround out there to apply in such cases?
EDIT-1
Following workaround works, but I'm still unsure what is the original problem.
I have created a method to call SetToolTip on the toolTip object, after calling the method, I re-create the toolTip instance using the "new" operator. That solves the issue. However as I want to disable anytime the tooltips on my application I also store all the references in a List. On that list I can iterate over to enable / disable all toolTip references according to what the user wants.
Basically there is a button to toggle the tooltips.
Do you have any idea, why this actually solves the original issue?
List<ToolTip> storeToolTipReferences = new List<ToolTip>(); //store tooltip references
//method begin called to set the tooltip on a control, store the reference of the tooltip and create a new instance
private void SetMyToolTip(Control ctrl, string toolTipText)
{
toolTip.SetToolTip(ctrl, toolTipText);
storeToolTipReferences.Add(toolTip);
toolTip = new ToolTip();
}
//called as the application loads or the user changes the language
private void SetAppLanguage(string[] LocalizationText)
{
storeToolTipReferences.ForEach(e => e.RemoveAll());
SetMyToolTip(ctrl1, LocalizationText[1]);
SetMyToolTip(ctrl2, LocalizationText[2]);
SetMyToolTip(ctrl3, LocalizationText[3]);
.....
}
//logic for tooltip enable/disable in my application
storeToolTipReferences.ForEach(t => t.Active = tooltip_control.Checked);
Thank you!

How do I show/activate the text cursor in open document? [C#, EnvDTE]

I'm trying to set the cursor in an open document, but it doesn't show. I can see that the line is "marked" and I can "navigate" the lines, but the cursor is not shown, and thus I'm not able to write anything. Also it seems like the document doesn't really fully load, since guidelines and the navigationmap is not shown either.
Which makes me believe that the focus isn't being set completely inside the document.
I have confirmed that the focus indeed is not set in the document in the window, since If I have the output window focused before, it's still focused after the window.Activate() method has been called.
I've used the common way of opening the document through ProjectItem.Open(Constants.vsViewKindCode), activating it, and using the TextSelection.GotoLine(1,false) method.
This correctly shows the document and sets the line correctly, but I have to manually click inside the document for the cursor to appear.
The code I have:
Window window = projItem.Open(Constants.vsViewKindCode);
window.Activate(); <----- this does not focus the window.
TextSelection textSelection = window.Document.Selection as TextSelection;
textSelection.GotoLine(1, false);
I want to not have to manually click inside the document for it to load completely and for me to be able to write in it.
Hope someone can help me.
Oh well, another issue I had to solve by myself... Two for two. I guess people don't know that much in here like I'd hoped. Oh well.
Anywho, I discovered that if you use DTE.ItemOperations.OpenFile(path); and nothing else,
the file will receive focus correctly, just like it should have when you use .Activate().

c# WPF scroll view Prints based on scroll location

I have built an app on WPF C# that holds invoices and I have created a usercontrol that is assigned to a scrollview.
Everything works perfect until I notice this really anoying printing bug.
So the user control is in the size of an A4. If the scroll view is scrolled to the top at the time of printing :
**PrintDialog pd = new PrintDialog();
pd.PrintVisual(scrollView, "print this off");**
No issues occur. However, if the scroll view is in middle or bottom it prints with an offset.
I have tried to remedy it by moving the scrollview to the top before printing and it does so but the prints come out wrong...
**scrollView.ScrollToHome();
PrintDialog pd = new PrintDialog();
pd.PrintVisual(scrollView, "print this off");**
and yet if I Hit the print button again it will print correctly. I have no idea why it prints before moving it to top as thats the only explanation I have for it...
PLEASE HELP
Call UpdateLayout() after scrolling home:
scrollView.ScrollToHome();
scrollView.UpdateLayout();
(from the docs of the UpdateLayout() function: "Ensures that all visual child elements of this element are properly updated for layout.")
this worked like a charm and simpler than implementing threading. Both good answers
I could not repro the problem with a very minimal sample WPF app consisting of just a ScrollViewer and a Panel. A shot in the dark: try to call scrollView.UpdateLayout() after your call to scrollView.ScrollToHome(). The docs of the UpdateLayout() function say, "Ensures that all visual child elements of this element are properly updated for layout.", and that sounds like what we want. – dlatikay
I have same problem before when I change layout of a Visual right before print action. If I print immediately, I will get wrong result. I guess that the Visual don't have enough time to render properly new layout before you print. I fixed like this:
using System.Threading.Tasks;
async void Print() // Use async
{
PrintDialog pd = new PrintDialog();
scrollView.ScrollToHome();
await Task.Delay(TimeSpan.FromSeconds(1.0)); // Wait 1 second scrollView being properly rendered.
pd.PrintVisual(scrollView, "print this off");
}

Why is my Form on top when I hover over a GMap.NET control?

I have a GMap.NET control on my main form, and then I have a second form that will show when the user clicks on a button. The problem is, that when the user leaves the second window and hovers over the GMap control from the main form, the main form comes to the front. This does not happen when i hover over the rest of the main window with the second window on the front. Also I have another form that will show while the program searches for some data, and this one will also disappear when the user hovers over the map.
I hope I explained it well enough, basically when I hover over the map the main window comes to the front. And it does only happen when there is another form of my project in front of it, not when there is any other window like firefox or something in the front.
So my question is: why does this happen and can I change this behavior? Is it a standard of the GMap.NET control? I am using the Version 1.7.0.0 control btw.
you must use
GMap1.DisableFocusOnMouseEnter = true;
i added this code to my program and problem solved.
It would help to see some code as to how you are showing the forms, but it sounds like you are doing something like secondwindow.Show() instead of secondwindow.ShowDialog(), and then an event from GMap for OnEnter is doing a Focus().
Then, when you have the "other form" in front of it, it's opened in a way that will restrict access to the parent form (ShowDialog(), etc).

WPF TextBox doesn't take input, space and backspace works

I have textbox inside a usercontrol and I add the usercontrol to the MainWindow with the following XAML:
<Views:MyUserControl />
I have one TextBox in MyUserControl, the problem is that the TextBox doesn't take any input. Backspace och space works, but if I press the letter och numbers no text is added to the TextBox.
I have made sure that the text is not just hidden in the TextBox.
I have also tried to add a RichTextBox to MyUserControl with the same result, it doens't take any input (beside space och backspace).
I have also tried to add a TextBox to the MainWindow with the same result; it doens't take any input (beside space och backspace).
Also MyUserControl is added in a TabControl and TabItem.
Any clues?
Edit: Additional information
Forgot to write that I'm opening/creating the WPF Window from a WinForm application.
When I set my startup project in VS10 to be my WPF-project it work great with the keyboard input to the TextBox.
How come?
Im opening/creating my WPF windows with the following code:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
mw.Show();
Edit: Solution
So I guess my real problem was that is was opening the WPf project from a WinForms application.
I added the following code:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();
"The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms."
http://weblogs.asp.net/jdanforth/archive/2008/07/29/open-a-wpf-window-from-winforms.aspx
Answer to my own question (if someone else run into the same problem):
If you open a WPF-form from a WinForms application you have to do the following to get keyboard input:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();
"The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms." http://weblogs.asp.net/jdanforth/archive/2008/07/29/open-a-wpf-window-from-winforms.aspx
maybe your user control is getting the keyboard event instead of your textbox? try to search in this way, it happens with mouse buttons.
I had the same problem but my Environment was built such that I could not apply the solution above. As it turned out that wasn't the problem. It was much more easier than expected.
I host a WPF User Control inside WinForms. My WinForms MainView (Startup form) did override the "ProcessCmdKey". Because of logical errors it returned nothing.
Nevertheless I received all key events in code behind in my XAML file but this events never updated the "Text" of my text box. Only space and backwards did so and this were the key events I didn't receive in code behind.
So in case you don't think of the simplest solution here it is for VB.NET.
STRG+F "ProcessCmdKey" and make sure that you Return the correct value of you may completely uncomment it just to verify that it isn't causing you the same trouble as it did to me.
I would like to add my own answer just on the off chance it can help anyone else with the same or similar problems. My particular use case was a .Net windows form created by an addin. The addin was running out of Autodesk Inventor which I think may be WPF (but don't take my word for it.) The addin had several windows forms some docked within the application and with some acting as popups or a separate secondary window. The solution was to set the windows forms owner to one of the docked forms. Doing this allowed text to be edited and entered into its textboxes. Very niche solution to a niche problem but hopefully it may help someone at some point in the future.
private void CreatePanels(DockableWindows DockableWindows)
{
//Create the dockable window that the panel form will live on.
DockableWindow SideWindow =
DockableWindows.Add(SidePanelNames.GUID, SidePanelNames.InternalWindowName, SidePanelNames.Title);
//Configure the windows settings.
SideWindow.ShowTitleBar = true;
SideWindow.ShowVisibilityCheckBox = true;
SideWindow.DisableCloseButton = false;
SideWindow.Visible = true;
SideWindow.AddChild(SidePanel.Handle); //Add the forms as children and then display to the user.
SidePanel.Show(); //Display the panel to the user.
}
/// <summary>
/// Creates the Display window were order and patient information is displayed to the user.
/// This method makes the adjust device form the owner of the display window. Because of this
/// dependecency it should be run after the panels have been created.
/// </summary>
private void CreateDisplayWindow()
{
DisplayForm = new DisplayForm //Create the display form.
{
Owner = SidePanel //Doing this allows text boxes to have values entered into them.
};
DisplayForm.Show(); //Show the display form to the user.
}
//Adding the side side panel as the owner of the display form was effectively the only change I needed to make.
I will add a few words of warning for anyone attempting this solution. Simply put I've since found out it's not great. It prevents the form or any of its controls from detecting certain key presses (enter and tab) which matters a lot for my usage. You'll find a much better and solution here https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/td-p/9115997

Categories

Resources