I'm trying to access an icon from my resources and it's not available. It seems to be available in a window. How can I access them?
namespace My_Application
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
/* Compile error "'System.Collections.IDictionary' does not contain a definition for 'Resources'…" */
Properties.Resources.Application;
base.OnStartup(e);
}
}
This one works! It's in a Window Class
public partial class MainWindow : Window
{
public MainWindow()
{
/* Available */
Properties.Resources.Application;
}
}
So for some reason within App.xaml.cs, it's necessary to disambiguate using the project namespace whenever using automatic Settings or Resources classes. using directives and assembly references are verified to be irrelevant.
Related
I have the following custom Form (MyFrm) which inherits from Form.
public class MyFrm<T>: Form where T: class
{
}
And following is my Form1:
public partial class Form1: MyFrm<CONTACTS_BASE>
{
public Form1()
{
InitializeComponent();
MyInitialize();
}
public void MyInitialize()
{
}
}
as can be seen, there is nothing exceptional, However, when right click and select view designer I get the following error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:
Form1 --- The base class 'MyGym.Controls.MyFrm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
when I remove the part below and edit my Form1 accordingly I get no errors when I go to the designer mode.
: Form where T: class
Why am I facing this issue? is there a fix for this?
Thanks
I believe you need to explicitly provide the InitializeComponent() method so that the Visual Studio IDE (Designer) works properly.
public class MyFrm<T> : Form
where T : class
{
public MyFrm() : base()
{
InitializeComponent();
}
private void InitializeComponent()
{
}
}
and then chain the constructors it together
public partial class Form1 : MyFrm<CONTACTS_BASE>
{
public Form1() : base()
{
InitializeComponent();
MyInitialize();
}
public void MyInitialize()
{
}
}
Note that : base was added to the constructor. However, in this example it's a bit overkill as the base constructor would already be called implicitly. I provided this addition, due to this answer. It states you must keep the constructor parameter-less in your base class.
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 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.
I have a Window Form App project. At the moment all of my code is in Form1.cs file which is the default file. Now I have about 1300 lines of code in this single file. I want to break down this one file code into several files and I want to use the "partial" key word (I don't want to do anything drastic). So how should I add the files
Right click project name->add->new item ->class results into class1.cs, class2.cs and so on
But this file converts to a form form file after compilation. What's the correct way of adding so that the new file integrates with my existing project Form1.cs and Form1.cs[Design]?
You have to keep the namespace, the class name and mark it with partial. The file name is not really important for it to work, but it's a good practice so that the developers can identify rapidly the contents of the file.
Form1.cs
namespace TheSameNamespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
// other definitions
}
Form1.Designer.cs
namespace TheSameNamespace
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
// the rest of the designer class
}
}
Form1.Calculations.cs
namespace TheSameNamespace
{
partial class Form1
{
// calculation methods definitions
}
}
Form1.EventHandlers.cs
namespace TheSameNamespace
{
partial class Form1
{
// event handlers definitions
}
}
and so on...
The partial keyword is primarly for generated files, which can be extended by your own code - there is no use in splitting a single bloated class into multiple partials, but if you really want to do it then you have to:
Create a new class.
Rename the class to match your own class (Form1.xxx.cs)
Use the partial key-word and adjust the name and the namespace.
To clearify:
Form1.cs
public partial class Form1 { /* ... */ }
Form1.somepart.cs
public partial class Form1 { /* ... */ }
I am making a MessageBox like class (MessageBoxCustom).
I would like to have a Form with designer support in a separate file so I can modify the appearance through Visual Studio (MessageBoxCustomDialog ).
I would also like to make this MessageBoxCustomDialog unreachable by code outside MyMessageBox and I'm nesting MessageBoxCustomDialog. I would like to move it in a separate file so I'd have designer support. Maybe using a partial class? How would the hierarchy go?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace System.Windows.Forms
{
public static class MessageBoxCustom
{
public static void Show()
{
(new MessageBoxCustomDialog()).ShowDialog();
}
private class MessageBoxCustomDialog : Form
{
}
}
}
The Visual Studio Designer can not help you design nested classes. It is just not made for that. It checks the type of the first outermost class in the file and then decides which designer to use.
If it is just about designing the layout of the form I would recommend to design it as usual. When you finished your project you can then surround the class by the outer class (in both files) and make it private.
When you finshed your work just copy and paste the dialog class into the outer class and make it private. If you have to rework the design it is again just copy and paste.
MessageBoxCustomDialog.cs:
namespace System.Windows.Forms
{
// make sure this is the first class in the file (required by designer)
public partial class MessageBoxCustomDialog : Form
{
public MessageBoxCustomDialog()
{
InitializeComponent();
}
}
public static partial class MessageBoxCustom
{
public static void Show()
{
new MessageBoxCustomDialog().ShowDialog();
}
// put the MessageBoxCustomDialog class here when you are done
}
}
MessageBoxCustomDialog.Designer.cs:
namespace System.Windows.Forms
{
partial class MessageBoxCustomDialog
{
...
}
partial class MessageBoxCustom
{
// put the MessageBoxCustomDialog class here when you are done
}
}
Make your MessageBoxCustomDialog a private partial inner class
private partial class MessageBoxCustomDialog : Form
{}
You must make MessageBoxCustom partial having same scope of MessageBoxCustomDialog
File 1
using System.Windows.Forms;
namespace System.Windows.Forms
{
public static partial class MessageBoxCustom
{
public static void Show()
{
(new MessageBoxCustomDialog()).ShowDialog();
}
private partial class MessageBoxCustomDialog : Form
{
}
}
}
File 2
using System.Windows.Forms;
namespace System.Windows.Forms
{
public static partial class MessageBoxCustom
{
private partial class MessageBoxCustomDialog : Form
{
// designer code
}
}
}
You may see this link http://msdn.microsoft.com/en-us/library/wa80x488.aspx [Restrictions section]