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.
Related
The majority of my forms for my project include an OK and Cancel button. (always positioned at the bottom right of the form). Currently I have a base class that inherits from System.Windows.Forms which contains an OK and Cancel button. All forms that use this then inherit from this base form class. Is there a better way of doing this that takes localization into consideration?
I would use MDI Child Forms for this. Parent Form Can contain OK/Cancel button where as you would have your child form in MDI container.
For More help visit
https://msdn.microsoft.com/en-us/library/aa984329(v=vs.71).aspx
You could just create a single form that has an empty panel or table layout, where you dynamically load the desired user control. It is basically the composition over inheritance principle.
public partial class MyFormWithButtons : Form
{
public MyFormWithButtons(UserControl control)
{
InitializeComponent();
control.Dock = DockStyle.Fill;
myPanel.Controls.Add(control);
}
}
Doing form inheritance is very useful in many levels:
Make a base form, and name it for ex: FrmBase.
Add the Ok, Cancel Buttons to it and set the Anchor property for both to Bottom.
Set the Buttons "Modifiers" property to "Internal", this way you can access these buttons from inherited forms:
Make as many forms as you want and make each inherit from the FrmBase ex: Form1 : FrmBase
now you can access the buttons from this from, using the properties.
Hope this being useful for you.
Whenever I drag a metroframework control onto a form window it appears in the bottom left corner of another window instead of on the form itself.
It only happens with metro controls and not regular ones. How to make the controls appear only on the form window?
You need to change the Form to a MetroForm.
Before anything else, you must have a REFERENCE and it should be like this:
using MetroFramework;
or optionally add this:
using MetroFramework.Forms;
For this change this:
public partial class yourForm1 : Form
To this:
public partial class yourForm1 : MetroForm
Among many others there is a nice tutorial here that goes through the whole process:
Downloading
Compiling
Copying the 3 DLLs you need
Creating a new project
Changing the form type (!)
Adding references
Adding the using clause
Adding controls..
i wanted to disable form control from class library, means i added one class named as clsInit method & i called this method when i'm loading the form in main project,so i need find the control which one i wanted to disable.
Is it possible to find loaded form controls in class library?
Form.Controls property is what you need.
You can pass the reference of your form into your library, and access its controls via Controls property.
You may create an object of your form, as:
MyForm frm = new MyForm();
...then select the controls to be disabled:
foreach (Control control in frm.Controls)
{
if(control.Name == "cboSomeDdn")
control.Enabled = false;
}
and then load the form (this one :
frm.Load()
or
frm.Show()
If this is a one form application, you may also set this as a starting point:
Application.Run(frm);
Please use this answer as a starting point and not as a copy-paste
solution. Also ensure to follow best-practices of development in the
language of your choice.
Hope this helps!
Vivek
I have two forms in C# Window application as frm_Stock.cs and frm_Purchase.cs.
I want to use some controls of frm_Stock in the frm_Purchase.Is it possible?IF yes then how can i do this please give me suitable example.
Thanks in advance
You will have to pass a reference to the controls/form when constructing the other form, and use that reference. A rough example,
frm_Stock = new StockForm();
frm_Purchase = new PurchaseForm(frm_Stock);
then within the purchase form code...
public class PurchaseForm : Form
{
public PurchaseForm(StockForm frm_Stock)
{
frm_Stock.SomeControl.Text = "blah";
}
}
It is possible but not recommended way of doing it, you should have pass it to the other form via some shared object(i-e StateBag ). You can make the controls of Frm_Stock public and then they will be accsible from frm _Purchase when you make frm_Stock instance.
You should create a custom user control and use it on both forms.
If you want to share the same instance of the user control across the form, then create an instance when your application starts and add it manually on both forms when they load.
If I add a component class to my project containing some common controls, how can I display an instance of it in a panel in my main form?
I use this to create an instance of my class:
Component test = new Component1();
where Component1 is the name of my Component class
Then how could I do something like:
panel1.Controls.Add(test); ?
Or is there a way to do this without using Panels?
You are using the wrong class. A Component cannot be a child of a panel or a form, it doesn't have a visual presentation. The missing Handle property is the important one.
You need a Control. Derive your class directly from Control or from one of the built-in control classes (Button, Label, etc). You'd do the latter if you want to customize their behavior and make it re-usable.
Then how could I do something like: panel1.Controls.Add(test); ?
This is the correct way.
Or is there a way to do this without using Panels?
You could add your control to something other than a panel; for instance, to the form itself: Controls.Add(test).