Okay, so some of you may be familiar with the Krypton Toolkit by Component Factory. If not, check it out here.
Anyways, you can change the form style completely using this toolkit by opening the designer code for your form and changing this line:
Inherits System.Windows.Forms.Form
To this:
Inherits ComponentFactory.Krypton.Toolkit.KryptonForm
That is Visual Basic though. I would like to achieve this using C#.
In the form's code-behind file instead of this:
public partial class Form1 : Form
type this:
public partial class Form1 : ComponentFactory.Krypton.Toolkit.KryptonForm
Note, that this isn't a changing of form's style, this is changing of base class. To open code-behind, you should press F7 in designer by default.
in C# you have 2 cs files for your form.
Say your form is called Form1, you'll have to go to the file Form1.cs and change this line:
public partial class Form1 : Form
to
public partial class Form1 : ComponentFactory.Krypton.Toolkit.KryptonForm
that should do the work
Related
My project built a panel with a pictureBox. This panel should be possible to maximize (for pictures displaying). I want to do that by creating a class like that:
public partial class ImageDisplay : UserControl
and in designer I add a picturebox and later do something with that.
It's not working properly, coz I cannot maximize ImageDisplay class (while it inherits by UserControl). I tried to do that just by changing it on:
public partial class ImageDisplay : Form
and then change the form's properties.
But I can't do that in that way, coz after I built my project, I use created .dll file in another program. And this program is working properly, when I use the class inheriting from UserControl. But when it inherits from the form, this program crushs.
I don't know why that is working in that way, I just get this code yesterday, when I've never coded in C# before.
So every hint or help or maybe good link would be helpful.
I'm following this tutorial on winforms, and so far the tutorial is coding the form without using the toolbox. I believe it'll introduce the toolbox in more depth shortly.
Following the tutorial, I've made a partial class in the following two pieces of code:
First file:
using System;
using System.Windows.Forms;
public class Numeric : System.Windows.Forms.TextBox
{
public Numeric()
{
}
}
public partial class Exercise
{
private Numeric txtbox;
System.ComponentModel.Container components;
}
Second file:
using System;
using System.Windows.Forms;
public partial class Exercise : Form
{
private void InitializeComponent()
{
txtbox = new Numeric();
Controls.Add(txtbox);
}
public Exercise()
{
InitializeComponent();
}
}
public class program
{
public static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
When I run the code with F5, everything looks fine: The form pops up with the textbox.
But for some reason, when I right-click on the second file and choose "View Designer", I get an error which says "The variable 'txtbox' is either undeclared or was never assigned". I can choose to "Ignore and Continue", which leads me to a form with no textbox on it.
Why does this occur? I know some of you think I should just use the toolbox, and it's probably the most sensible thing to do, but I would still like to understand why this happens.
How does the Windows Forms Designer work?
When you open a Form in windows forms designer, the designer looks into the first class in the file. If the file has a Designer.cs containing the other partial part of the class, also includes it and tries to deserialize those file contents. In the process of deserialization and loading the design time of your form, it creates an instance of the base class of your form and looks in those files for component declarations and InitializeComponents method. If find them creates components and sets properties of them using deserialized codes and add components to the instance of base class which created.
Some useful facts:
Codes in constructor of your Form will not execute at design-time, but the constructor of base class of your form will execute in design-time.
Codes in InitializeComponent will not execute at design-time, but those codes will be deserialized and will be used to create designer of the form.
The designer can not show a form which has an abstract base class. (solution)
The designer can not show a form which has generic class. For example it can not show MyForm:SomeForm<SomeClass>, but it can show SomeForm<T>:Form. (solution)
If you define a new property for your form, the properties will not show in properties window. The properties window, shows the properties of base class but with values of your form.
When a file contains 2 class, if the form was not the first class the designer can not load and you receive a warning that says the form should be first class to show in designer.
Above rules will apply also to UserControls.
Example
Take a look at below code, which has some serious problems:
The class has different constructor than class name
The statement int i="x";
There is no semicolons while this is a C# class
The InitializeComponent method didn't call in constructor
But the interesting news is you can see the form in designer, even with those errors!
Just create a file in your project and put below codes in the file and save the file and close it. Then without trying to build the solution, open the form in designer. Here is code:
using System
using System.Windows.Forms
namespace SampleApplication
{
public class MyForm:Form
{
public NotMyForm()
{
}
public void InitializeComponent()
{
int i="x";
textBox1 = new TextBox()
textBox1.Text = "Hi"
this.Controls.Add(textBox1)
}
private TextBox textBox1
}
}
And here is screenshot of designer:
More information
To find more information, take a look at this link:
How does the Windows Forms designer in Visual Studio load a Form? (The original website is off - You can find the archive here)
Solution for your question
As a solution, it is enough for you to move private Numeric txtbox; and put it your seccond file in Exercise class.
The declaration of the controls should be put into Designer.cs files in order Visual Studio can just compile this unit and display it.
When you launch the app, the compiler takes into account all parts of your partial class then it finds txtBox declaration.
Try leave only the form class with its graphical declarations in a single file.
This single file should have InitializeComponent methdod, construcutor and field declarations of UI components initialized in InitializeComponent().
I was working on a WinForm, and for an unknow reason, I can't open it in design mode anymore, any (only that specific form). Is there anyway to tell Visual Studio 2013 that it's a WinForm and not a Class ?
You can "unload" the project, and then edit the .csproj file. Search for your .cs file, and insert a SubType node... <Compile Include="Scraper.cs"><SubType>Component</SubType></Compile>. Save the .csproj and Reload your project.
For me I had a class defined before the partial class, i.e.:
public class MyClass { ... }
public partial class MyForm : Form { ... }
Defining MyClass after MyForm fixed the problem.
Same happens to me, when I did some base class for my forms and manually change to it in generated forms.
Solution was to add
[System.ComponentModel.DesignerCategory("Form")]
attribute to base class definition.
P.S.: same way around, you can use
[System.ComponentModel.DesignerCategory("Code")]
if you want some own derived control to never open in designer, like if it is a user conhtrol.
Right-click on your file in Solution Explorer, "Open with.." and reselect the designer in the list. Maybe? Or maybe your Designer.cs file has an issue or was somehow corrupted as mentionned by the replier above.
If you remove the Form inheritance after your form class you experience the behaviour described
public partial class MyMainForm : Form
{
}
here, if you remove the inheritance from the base Form class Visual Studio shows your form code instead of the designer when you double click on the form name in the Solution Explorer
Make sure your namespaces match on the form class StateRuleDetailTriggerForm.cs and its designer class StateRuleDetailTriggerForm.Designer.cs.
I need to make a UserControl that can be used for multiple projects. But it needs to be a Form so the user can just add a reference to the library and call the form.
I've seen third party companies like Telerik and DevExpress use custom forms that can be added to a project.
How would I accomplish this? I've been looking through SO and various posts from Google, but have not been successful in my searches.
EDIT I was assuming it had to be a UserControl for some reason. But it doesn't. I took the suggestion of just adding a form and calling it from that namespace. Works exactly as needed. Thanks everyone.
Just create the form in your library, make it public, and you can call it from anywhere.
Methods to create and call form are:
YourFormClassName FormForUser = new YourFormClassName();
FormForUser.Show();
FormForUser.ShowDialog();
Maybe I don't understand. If I do, then it's straight forward.
Add a project (ProjectWithReusedForm) to your solution that contains the form to be reused.
Add a reference to ProjectWithReusedForm in the second project where you want to use the form
Add a 'using ProjectWithReusedFormNamespace' to the code where you want to use the form
You then can add the statement ReusedForm myForm = new ReusedForm();
You can create BaseForm (either add it into a project directly by adding .cs file or reference something compiled - class library to example). Then just add a new form to a project
namespace MySolution
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
and chang Form to BaseForm
public partial class Form1 : BaseForm
Just Create form with all controls. and create empty user control
Ex:
do this code inside usercontrol constructor after initialize function
dim obja as new RegForm()
obja.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
obja.Visible = true
obja.Dock = System.Windows.Forms.DockStyle.Full
me.Controls.Add(obja)
You have to be careful here. Your tag lists winforms, so I am assuming you are using .net and UserControls. Winforms only allows a single form per page. You can add multiple UserControls to a page. If you go with the base form route, the programmer will have to add everything else to your base page. UserControls will offer a little more flexibility in that they can be added to an existing page.
I have a class
public abstract class BaseFormClass : UserControl
and another class:
public class DerivedFormClass : BaseFormClass
if i open design view on baseformclass it show 3 components at the top.
but if i open derivedformclass, it does NOT show these 3 components, I have tried declaring
InitializeComponent virtual and overriding, but this made no difference.
Its annoying as if i run it then it displays fine, but i want it to work in designer so it easier to work with in future.
any ideas?
I have found out what works,
'both' class definitions need to extend the base class
so designer and general
public partial class BaseFormClass : UserControl
...
partial class DerivedFormClass: BaseFormClass
...
then the DerivedFormClass's designer will show the components from the BaseFormClass,
an alternative is to just define the baseformclass and then user that control in teh derived form class,
but this depends on what you need to implement.