I get an error saying "Missing partial modifier on declaration of type 'projectName.Main'; another partial declaration of this type exists.
From what i can read about this error, its because i have classes with same name. It does work if i modify the main class to public partial class Main : Form but i want to know why it gives me this error. I need to initializeComponent within Main(), tried creating a method start() and then calling main.Start() in a load event, but then the form loads blank.
namespace projectName
{
public class Main : Form
{
public Main() // Method: Starts the main form
{
InitializeComponent();
}
public void Main_Load(object sender, EventArgs e)
// On load of main class, handle events and arguments
{
Main main = new Main();
main.getCurrentDomain();
}
public void getCurrentDomain() // Method: Get current domain
{
Domain domain = Domain.GetCurrentDomain();
}
} // Ends the main class
}
Assuming this is a Windows Forms app, the problem is that the Visual Studio WinForms designer has created another file (e.g. Main.designer.cs) with:
public partial class Main : Form
to contain designer-generated code.
Your partial class source is effectively merged with that - but it can only happen when both source files declare that the class is partial. Otherwise, you're just trying to declare two classes with the same name in the same namespace, which is prohibited by C#.
There is another file with the name .designer.cs. This file contains the partial definition of class and that is where your InitializeComponent function is defined. You need to add partial modifier to your class.
Related
You should be able to create a generic form:
public partial class MyGenericForm<T> :
Form where T : class
{
/* form code */
public List<T> TypedList { get; set; }
}
Is valid C#, and compiles. However the designer won't work and the form will throw a runtime exception if you have any images stating that it cannot find the resource.
I think this is because the windows forms designer assumes that the resources will be stored under the simple type's name.
Yes you can! Here's a blog post I made a while ago with the trick:
Designing Generic Forms
Edit: Looks like you're already doing it this way. This method works fine so I wouldn't consider it too hacky.
I have a hack to workaround this, which works but isn't ideal:
Add a new class to the project that inherits the form with its simple name.
internal class MyGenericForm:
MyGenericForm<object> { }
This means that although the designer is still wrong the expected simple type (i.e without <>) is still found.
You can do it in three steps.
1) Replace in Form1.cs File
public partial class Form1<TEntity, TContext> : Formbase // where....
2) Replace in Form1.Designer.cs
partial class Form1<TEntity, TContext>
3) Create new file : Form1.Generic.cs (for opening design)
partial class Form1
{
}
If paleolithic code doesn't affraid you
public static MyForm GetInstance<T>(T arg) where T : MyType
{
MyForm myForm = new MyForm();
myForm.InitializeStuffs<T>(arg);
myForm.StartPosition = myForm.CenterParent;
return myForm;
}
Use it
var myFormInstance = MyForm.GetInstance<T>(arg); myFormInstance.ShowDialog(this);
I have a Visual Studio 2010 Windows Forms app which includes a Form base class that other classes will inherit. The base class' constructor takes a parameter that the child classes will pass to the base class.
Example:
public partial class BaseForm : Form
{
public BaseForm(int number)
{
InitializeComponent();
}
}
public partial class ChildForm : BaseForm
{
public ChildForm(int number)
: base(number)
{
InitializeComponent();
}
}
The problem that I'm running into is, when I attempt to open the ChildForm in VisualStudio's Design View mode, I receive the following error:
Constructor on type 'MyProject.BaseForm' not found.
Note: regardless of the error, the project compiles and runs fine.
I can avoid the error if I overload the constructor with one that does not contain any parameters.
Example: (This gets rid of the error)
public partial class BaseForm : Form
{
public BaseForm(int number)
{
InitializeComponent();
}
public BaseForm()
{
InitializeComponent();
}
}
public partial class ChildForm : BaseForm
{
public ChildForm(int number)
: base(number)
{
InitializeComponent();
}
}
My question is, how can I create a base class that does not include a parameterless constructor and avoid the Design View error?
That is completely impossible.
The form you see in the design view is an actual instance of your base class.
If there is not default constructor, the designer cannot create that instance.
You can mark the constructor with the [Obsolete("Designer only", true)], and make it throw an exception if called when not in the designer, to prevent other people from calling it.
You need to adjust your BaseForm output type, In the properties for the project, change the Output type from Windows Application to Class Library.
ref:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/walkthrough-demonstrating-visual-inheritance
I'm using Visual Studios. I wrote a method in a form1.cs file in a partial class
private void TestMethod1()
{
}
I want to call this method in form2.designer.cs, in the same partial class. I tried this:
TestMethod1();
but I got the error method not found.
this is the form.cs
namespace classA
{
public partial class A : B
{....
private void TestMethod1()
{
}
}
}
this is the form.designer.cs
namespace classA
{
partial class A
{
private void InitializaCOmponent()
{
.....
}
(where I call my function)
TestMethod1();
}
}
If the situation is as you described, then the compiler should not generate the error message as it is valid code.
However, if you try to use the visual editor, and you insert the call in your code inside the InitializeComponent method you will get an error.
This is caused by the Form editor not being able to call functions that are defined within the class you are actually editing - it is a bit restrictive about what you can do within that scope.
I am developing a C# windows form application and on the main form I have a TabControl. It is declared in the Form1.Designer.cs file as follows:
public System.Windows.Forms.TabControl logFileCollectorTabControl;
In another class file in my project I want to use this TabControl as follows:
logFileCollectorForm.logFileCollectorTabControl.TabPages.Add(newTabPage);
But I get the error 'An object reference is required for the non-static field, method or property error'. So my question is, there must be an object of the Form class declared somewhere because the form launches when I launch the application, so how do I find out what that is, or how can I solve this issue, any help is greatly appreciated!
This is usually overcome by passing in an instance of Form1 to the constructor of the calling class, then keeping it in a field until needed.
//somewhere in Form1
OtherClass other = new OtherClass (this);
// OtherClass.cs
class OtherClass {
Form1 _opener;
public OtherClass(Form1 opener) {
_opener = opener;
}
}
Is your other class aware of logFileCollectorForm?
If you do not pass a reference to the form to the other class, then the other class does not know what Is logFileCollectorForm is referencing.
//example of another class
class AnotherClass
{
Form1 logFileCollectorForm;
public AnotherClass(Form1 logFileCollectorForm)
{
this.logFileCollectorForm = logFileCollectorForm;
}
public DoSomething(String newTabPage)
{
logFileCollectorForm.logFileCollectorTabControl.TabPages.Add(newTabPage);
}
}
There is probably no need to pass an instance of an entire form, you could pass a reference to your TabControl only. But it's still bad design in my opinion. Your logic should be separated from UI. If your class performs some computations, database operations or what not, it shouldn't really have to "know" about your window at all, because this is inflexible. Implement an event instead.
Another option is to keep a static reference to the main form in the Program class.
static class Program
{
internal static Form1 MainForm { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1();
Application.Run(MainForm);
}
}
class OtherClass
{
public void AddNewTab(TabPage newTabPage)
{
Program.MainForm.logFileCollectorTabControl.TabPages.Add(newTabPage);
}
}
You should be able to create a generic form:
public partial class MyGenericForm<T> :
Form where T : class
{
/* form code */
public List<T> TypedList { get; set; }
}
Is valid C#, and compiles. However the designer won't work and the form will throw a runtime exception if you have any images stating that it cannot find the resource.
I think this is because the windows forms designer assumes that the resources will be stored under the simple type's name.
Yes you can! Here's a blog post I made a while ago with the trick:
Designing Generic Forms
Edit: Looks like you're already doing it this way. This method works fine so I wouldn't consider it too hacky.
I have a hack to workaround this, which works but isn't ideal:
Add a new class to the project that inherits the form with its simple name.
internal class MyGenericForm:
MyGenericForm<object> { }
This means that although the designer is still wrong the expected simple type (i.e without <>) is still found.
You can do it in three steps.
1) Replace in Form1.cs File
public partial class Form1<TEntity, TContext> : Formbase // where....
2) Replace in Form1.Designer.cs
partial class Form1<TEntity, TContext>
3) Create new file : Form1.Generic.cs (for opening design)
partial class Form1
{
}
If paleolithic code doesn't affraid you
public static MyForm GetInstance<T>(T arg) where T : MyType
{
MyForm myForm = new MyForm();
myForm.InitializeStuffs<T>(arg);
myForm.StartPosition = myForm.CenterParent;
return myForm;
}
Use it
var myFormInstance = MyForm.GetInstance<T>(arg); myFormInstance.ShowDialog(this);