I am developing a C# Windows Form project with Visual Studio 2019. I have inherited a new control, called NewGroupBox, from System.Windows.Forms.GroupBox, the control is under name space MyProject.NewCtrls and was added through the standard procedure How to: Inherit from Existing Windows Forms Controls. I did not do any change on the code generated except changing public partial class NewGroupBox: Control to public partial class NewGroupBox: GroupBox
In the main frame, I have a GroupBox object called groupbox_Table.
groupBox_Table = new System.Windows.Forms.GroupBox();
The form designer could open normal. However, if I manually changed GroupBox with NewGroupBox
groupBox_Table = new MyProject.NewCtrls.NewGroupBox();
The form designer cannot be opened properly. The error message shows "Could not find type 'MyProject.NewCtrls.NewGroupBox' ...". Of course the type is defined in my project and there is no error when building the project.
How do I fix this problem so that form designer can open normally? Thanks a lot!
Related
I have 2 forms that inherit a control from the class below:
public class AInbox: Form
{
public FlowLayoutPanel InboxItems;
}
The forms inherits as such:
public partial class Inbox : AInbox
{
...
}
In the Designer.cs file i commented out the original "InboxItems" control declaration and everything compiles and runs fine... except the GUI designer. When i open the Designer I get the error "The variable 'InboxItems' is either undeclared or was never assigned."
Is there any way to use this inheritance and still have the designer work?
I'd recommend against inheriting a form with generated code (like you're doing with Inbox).
If you want the child class (Inbox) to add additional controls, I wouldn't use the designer directly on the child class, because I don't think the visual studio form designer will play nicely when half of the form was designed in the parent class. If you need to reuse certain parts of your form in a different form, you might want to consider moving that part of the form to a separate user control. You can use the designer on this user control and later put the user control in the forms.
If you just need to have the same form, keep an instance of the form in your other class. Move your logic away from your form (view) and in your other class (controller).
When I load solution and there is an opened designer tab for some window, then this window static constructor is not executed.
Perhaps my conclusion is wrong (because I am absolutely clueless how designer load things), but here is a test case:
Create new WPF project.
Create simple extension
public class MyExtension : MarkupExtension
{
public static bool Test;
public override object ProvideValue(IServiceProvider serviceProvider) => Test.ToString();
}
Add to main window
<TextBlock Text="{local:My}" />
and
static MainWindow()
{
MyExtension.Test = true;
}
Now compile it (F6), TextBlock should show True in designer.
Do not close designer window. Close VS.
Start solution (double-click sln file).
As soon as designer loads window you will see TextBlock display False.
WTF? Can someone confirm that (or is it my VS 2015 bug)?
I would really like to know how designer works: how window is loaded, which events/methods are used, etc. It seems window constructor (non-static one) is not executed (anything put there is not happening in design time), how is the window then created and displayed?
I know how it works for Visual Studio 2010 and reading your question I suppose that the same principles may be also applied to Visual Studio 2015.
When a control is rendered inside the XAML designer (regarding VS2010 it is called Cider ) as the main control (i.e. a Window) its constructor is not run. On the other side, if a control is a child of another control which is rendered inside the XAML designer, the first control's constructor is executed (i.e. a UserControl inside a Window). You can read more about it here.
So you need to move the My.Test initialization into a customized control, for example:
public class MyTextBlock : TextBlock
{
static MyTextBlock()
{
MyExtension.Test = true;
}
}
Then use it inside your Window:
<local:MyTextBlock Text="{local:My}" />
After you compile your project, you will see the "True" text in the designer. I repeat: it works for Visual Studio 2010, so I hope it can represent an hint to solve your issue.
I am subclassing ListView in a Windows Universal App Project. I create a new UserControl in Visual Studio 2015 RC and then change the UserControl type to ListView in both XAML and codebehind.
When I insert the control and run the application I get a 'Xaml Parsing Failed' exception without any further information.
Upon researching I came across the issue where the project name contains a dot, but my project name does not contain such characters.
Any Idea how to research this further?
EDIT: I also tried subclassing ListView, GridView, ListBox and Itemscontrol. Itemscontrol does not generate the error but all others do.
EDIT 2:
Created a new Universal App Project in VS2015 RC
Add -> UserControl
Changed Base Class into:
public sealed partial class ListViewEx : ListView
{
public ListViewEx()
{
this.InitializeComponent();
}
}
Changed XAML UserControl tag into ListView
The answer from MSDN forums: use themed control (custom control) for this case.
However, this approach works in WPF, it surprises me.
This question already has answers here:
How to programmatically create a WPF window in a WinForm application
(2 answers)
Closed 5 years ago.
I added to my WindowsForm app a new WPF window called novoLogin.
After adding it, I added the system.xaml reference....debug fine.
Now I'm trying to open this new window from the existing windowsForm.
novoLogin nl = new novoLogin();
nl.show();
The compiler is giving this error:
Error 1 'WindowsFormsApplication1.novoLogin' does not contain a
definition for 'show' and no extension method 'show' accepting a first
argument of type 'WindowsFormsApplication1.novoLogin' could be found
(are you missing a using directive or an assembly reference?)
This brief article explains how you can achieve this.
If you find yourself in need to open a WPF Window from a WinForms program, this is one way to do it (works for me):
Create/Add a new project of type WPF Custom Control Library
Add a new Item of type Window (WPF)
Do your thing with the WPF Window
From your WinForms app, create and open the WPF Window
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
Have a look to this: http://www.mobilemotion.eu/?p=1537&lang=en
Summary:
Open the project’s manifest file (the one with the .csproj or .vbproj extension) in any text editor.
The top node usually contains several tags, one for each build configuration and a global one. In the global
node (the one without Condition attribute), search for
the sub-node or create one if it does not
exist. This node should contain two GUIDs:
FAE04EC0-301F-11D3-BF4B-00C04F79EFBC, which stands for a C# project,
and 60dc8134-eba5-43b8-bcc9-bb4bc16c2548 which stands for WPF. The
full line should look as follows:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
(If you’re interested in details, codeproject holds a complete list of
potential project GUIDs:
http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs)
Reload the project in Visual Studio, and open the Add New Item wizard.
Since the project is now officially classified as WPF project, this
wizard should now contain the WPF window option. By the way, since
there is no WinForms project GUID that could be overwritten, this
approach does not harm the existing project components.
I just tried this approach for a VB.NET project and it works!
Using VB.NET obviously you have to edit above lines substituting the GUID from {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} to {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
I wanted to show wpf form in windowForm and there was some resource problem...
(because I used resources..). Finally I used this code in my windowsForm project:
First create a global instance of your app class like this :
WPFTest.App app;
why this is global?
because this class is singleton and you can not create more than one instance in the same AppDomain
Now for example you have a button event to show wpf form. At the button event we have :
private void button1_Click(object sender, EventArgs e)
{
if (System.Windows.Application.Current == null)
{
app = new WPFTest.App()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
app.InitializeComponent();
}
else
{
app = (WPFTest.App)System.Windows.Application.Current;
app.MainWindow = new WPFTest.YourWindow();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(app.MainWindow);
app.MainWindow.Show();
}
}
note : WPFTest is name of your project and YourWindow() is window that you wanna to show
Firstly, I have a project with a Windows Form that references another project with WPF forms. The windows form has an elementhost which child is one of the WPF documents in the other project.
Now, on this WPF document I want to have a button that upon a click can open another wpf form. Either as a new standalone WPF form, as a modal or whatever.
I cannot, on the button click event, say
WPFform2 WPFform2=new WPFform2();<br>
WPFform2.Show();
... as many other threads on the net suggest, since the show method does not exist.
My solution does not allow some sort of call that changes the main Form´s elementhost, so that is not an option for me.
All my WPF forms derives from UserControl:
public partial class WPFform1: UserControl
The form must be derived from Window to have the Show() method.
Just create a new window that contains only the form you want to show and call Show on it. Or change the control's base class to Window (you will have to rewrite it both in XAML and in the code behind), nothing should really change, Window supports most of UserControl's features.