Circular dependency when adding reference (Delegates) - c#

Basically I have 2 projects, a form and a user control.
I need both of them to be in different projects but the form need to refer to the user control as it is using the user control. And the user control will need to refer to the form as it is using one of the form class. When I add the second one because it need the , VS will complain circular dependency which is understandable. How do I solve this?

Logically the form should depend on the user control. You could create an interface to replace the form within the user control project, and then have the form implement that interface.
Example user control project;
public interface IForm
{
string MyString { get; }
}
public class MyUserControl : UserControl
{
public IForm Form { get; set; }
private void ShowMyString()
{
String myString = Form.MyString;
...
}
}
Example Form project
public class MyForm : Form, IForm
{
public MYString { get "My String Value"; }
}

I think the root cause of your problem is that you haven't separated your concerns between the form and the control properly.
Since you have a (somewhat generic) control, it shouldn't depend on the form. All of the logic of the control should reside within the control itself. The form should only black-box consume the control: add it, set public fields, call public methods, etc. anything else is a violation of encapsulation.
Sometimes, controls may need to know things about their parent form. In this case, I would suggest something as simple as adding a Parent field to the child control.
if you need something more specific from the form, you can always add an interface; the interface should only list those things that the control needs from the form. For example, if you need the size, you can add:
public interface IControlParent {
int Width { get; }
int Height { get; }
}
This way, you clearly see the dependencies (what the control needs from the parent), and if the parent type/contract changes, you don't need to do as much to change your control class.

You must sepárate your code, its never a good idea to have a reference to an application assembly, if you try to reuse it in the future, the applications exe should go with the control.
So, take the class from the form project and move it to the control project or create a library project, put the class on it and reference it from your control and your app projects.

You should use an event (delegate). Let's assume that inside your form project you created one class: Form1. And inside user control you defined UserControl1.
UserControl1 needs to instantiate and call a method from Form1:
public class Form1
{
public void Execute(string sMessage)
{
Console.WriteLine(sMessage);
Console.ReadLine();
}
}
UserControl1:
public class UserControl
{
public Func<object, object> oDel = null;
public void Execute()
{
oDel?.Invoke("HELLO WORLD!");
}
}
And from the class that instantiate UserControl, let's call it ParentClass:
public class ParentClass
{
public void Execute()
{
UserControl oUserControl = new UserControl();
oUserControl.oDel = Form1Action;
oUserControl.Execute();
}
public object Form1Action(object obj)
{
string sObj = Convert.ToString(obj);
Form1 oForm = new Form1();
oForm.Execute(sObj);
return null;
}
}
This approach gives the responsibility of handling an event to the high level class.

Related

Error adding panel to usercontrol: Object reference not set to an instance of an object

I have a problem. I am trying to access a panel in a user control.When I access it in a form it works. Earlier on I did this.
I accessed a panel in a form from a user control and it worked. Below is the code I used:
Form1 form = Application.OpenForms.OfType<Form1>().FirstOrDefault();
form.Panel1.Controls.Clear();
ManageControl user = new ManageControl();
form.Panel1.Controls.Add(user);
But when I try to use the very same concept in a user control it does not work.
It throws a null error: Object reference not set to an instance of an object.
Below is the code:
//this is in ManageControl.cs
public Panel Panel2
{
get { return panelmanage; }
}
//this is in another userControl.Trying to access panelImage
ManageControl form = Application.OpenForms.OfType<ManageControl>().FirstOrDefault();
form.Panel2.Controls.Clear();//it throws the error here
ReportControl user = new ReportControl();
form.Panel2.Controls.Add(user);
What am I doing wrong because I am using the same concept?
EDIT:
This is my ManageControl.cs
public partial class ManageControl : UserControl
{
public ManageControl()
{
InitializeComponent();
}
public Panel Panel2
{
get { return panelmanage; }
}
This is how I try to access it in BookingListControl
public partial class BookingListControl : UserControl
{
ManageControl form = Application.OpenForms.OfType<ManageControl>().FirstOrDefault();
public BookingListControl()
{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ManageControl form = Application.OpenForms.OfType<ManageControl>().FirstOrDefault();
ReportControl user = new ReportControl();
form.Panel2.Controls.Add(user);
}
ManageControl is a UserControl not a Form. Thus, when you look for open forms of type ManageControl, you get nothing (no surprises here). Then, we you call FirstOrDefault it returns null (since there were no matching elements in the empty collection), and your next line blows up.
This approach is doomed from the start, because even if you had a whole bunch of forms overlaying each other and could make it work (bad idea), it would break once you had two ManageControl objects and needed to access the second.
Instead, first ask yourself, "Why do my UserControl objects need to access each other?". This is an important question, because in general UserControls are independent. They likely have methods to give data back to their parent, but thats it. They certainly don't interact with other UserControls.
If you decide that you really need this dependency, then I would pass the parent Form object to both UserControls and have a public property on the Form that allows them to see the other UserControl. From here you can access it normally (without needing any OpenForms nonsense). Honestly though, this is a massive code smell and it sounds like the whole design should be looked at to see where you have dependencies that could be removed.
To do this you need to expose the ManageControl on the form:
public class ParentForm : Form
{
public ManageControl Manager { get { return manageControlInstance; } }
...
}
Then access it in your child control. The easiest way would be through the Parent property, but you could pass it on the constructor or an Init function as well.
public class ChildControl : UserControl
{
private void SomeFunction()
{
(Parent as ParentForm).Manager.Panel2.Controls.Add(new ReportControl());
}
}
The code's pretty ugly, and I wouldn't recommend it (its also not safe if you put the ChildControl into anything other than a ParentForm). That being said it would work.
Assuming that the NullReferenceException occurred based on accessing the "Panel2" property, your issue is that "panelmanage" is null. Is the code that finds and populates "form.Panel2" in a form or control constructor? If so, try restructuring it to run after ManageControl is fully initialized - perhaps by putting it into a Loaded event.

c# Accessing WinForm control properties from another class

How does one access WinForm controls such as ProgressBar properties from another class?
Please see my code below. I know this might not be the best option to expose WinForm class and its members as public but I am trying to clear the concept at this point.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
Class1 c = new Class1();
c.loop();
}
public void PBSetup()
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}
public void PBUpdate(int recno)
{
progressBar1.Value = Class1.recno;
}
}
}
namespace WindowsFormsApplication1
{
class Class1
{
public static int recno;
public void loop()
{
//How do I access Form1.PBSetup()??
for (recno = 0; recno <= 100; recno++)
{
//How do I access Form1.PBUpdate(recno)??
}
}
}
}
You do not want your business logic (your class) interacting with your UI (your form). The business logic should be agnostic of the presentation layer.
If you want the form to respond to things that happen inside the class, you could consider exposing an Event inside the class that the form could subscribe to, much like it would subscribe to a button's click event. The class instance could fire off the event completely unaware of who might be listening, and any subscribers would be notified.
This looks like a big time code smell :).
You would need an instance of Form1 inside of Class1 in order to PBUpdate.
Something tells me what you are doing is just not right.
Explain what you are trying to do and we can help. Otherwise there is no way to access PBUpdate unless you either made it a static function where you could call it like Form1.PBUpdate() or you had an instance of Form1 within your class Class1
You can change the access modifiers of the progress bar from private to Internal or public , you can do this operation from properties pane .
Keep in mind that you have to pass to the second class the instance of the form and then you can change the value of the progress bar directly from the second class.
However is a tricky solution, the best should be keep the presentation layer implementation separated and work with an event.
I do not recommend to use this method, for simple reason as mentioned here by one of the comments. But if you really want to access that form control, here is how:
1) Select that control and set its access modifier to internal.
2) Assume your form id is "Form1" and control id is "control1"
Inside your method:
Form1 form = (Form1)Application.OpenForms["Form1"];
// form.control1 should now be available.

In a Windows Forms (.Net 4) application, how can I define a base form style for all forms?

For example, I'd like all of my forms to have the same Icon and StartPosition. However I also need to be able to define things in each form how you normally would, dragging and dropping controls, etc.
Is this possible?
Create a form and set the Icon and StartPosition properties the way you want them. Compile. This will be your base form. Now use Project + Add New Item, Windows Forms node and pick the Inherited Form item template. The IDE will prompt you to select the base form.
Antoher way to go, is to make an extension method where you set all the parameters:
public static class FormExtentsions
{
public static void SetDefault(this Form form)
{
form.Icon = new Icon("path");
form.StartPosition = FormStartPosition.CenterScreen;
}
}
The use it like this:
public partial class Form1 : Form
{
public Form1()
{
// Put it here if you want to be able to override everything
this.SetDefault();
InitializeComponent();
// Put it here if you want the defualt to override "local" settings
this.SetDefault();
}
}
Simply create your own base Form class:
class FormBase : Form
{
public FormBase()
{
Icon = SomeIcon;
StartPosition = StartPosition.Whatever;
}
}
You could have a static Icon and Position, initialize it with a static constructor, and then make a constructor where you initialize the instance's Icon and Position properties with the static Icon and position:
Fx.
class Foo : Form {
static Bitmap sIcon { get; private set; }
static Point sPosition { get; private set; }
static Foo() {
sIcon = /* Load from external source */
sPosition = new Point( x, y ); //Insert x and y
}
public Foo()
: base() {
Icon = Foo.sIcon;
Position = Foo.sPosition;
}
}
Then use "Foo" as your base form when creating your forms.
I didn't check references for the "Icon" and "position" so I don't know if they exists, but you get the idea :)
Yes, but bear in mind that forms inheritance is somewhat flaky in terms of designer support. Just keep in mind that any controls that need to be accessible to child forms must have their modifier changed to Protected (Internal will work for forms in the same assembly, but will also expose the control to ANY class in the same assembly; Public should be avoided). This includes things like panels or other containers, which you'll likely want to use if your base form has to define some basic presentation elements, so you'll want to contain the child form to a particular area.
To this, just create your base form as you would any other form, then when you go to create new forms, choose "Inherited Form" instead of "Form", and select your base form.

c# how do i access my form controls from my class?

I have a class in the same namespace as my form. Is it possible for me to access methods/properties of my form controls from my class? How can I accomplish this?
You need to make your controls public, however I wouldn't do that. I'd rather expose just what I need from my controls. So say if I needed access to the text in a text box:
public class Form1 : Form
{
public string TextBoxText
{
get{return this.textBox1.Text;}
set{this.textBox1.Text = value;}
}
}
One way is to pass the form into the class like so:
class MyClass
{
public void ProcessForm(Form myForm)
{
myForm.....; // You can access it here
}
}
and expose the Controls that you want so that you can access them but really you should pass only what you need to the class, instead of the whole form itself
If you pass a reference of your form to your class you should be able to access methods and properties of your form from your class:
public class MyClass
{
private Form form;
public void GiveForm(Form form)
{
this.form = form;
}
}
You need to generate stubs.
For that
In your custom class write a constructor
public YourClass(Main main)
{
// TODO: Complete member initialization
this.main = main;
}
then in main class/form class, initialize your class
YourClass yesMyClass = YourClass(this);
If your want to access form components on your custom class then,
this.main.label1.Text="I done that smartly'

C#: How to access a button outside the form class

I want to either enable or disable a button from another file,what should I do?
This is the form class declaration:
public partial class Form1 : Form
I tried with
Form.btnName.enabled = false/true
but there's no btnName member.
Thanks in advance!
Simply expose a public method:
public void EnableButton(bool enable)
{
this.myButton.Enabled = enable;
}
Correction:
public void EnableButton()
{
this.myButton.Enabled = true;
}
You need to expose the btnName member to other classes by making it public or using a property of sorts. For example add the following code to Form1
public Button ButtonName { get { return btnName; } }
Now you can use form.ButtonName for any instance of Form1
I really suggest to read more information on how forms fit in .net. You have a couple issues in that sample code "Form.btnName.enabled = false/true"
Your form is called Form1, it inherits from Form.
Forms are instances, in fact you can have different form instances in an application belonging to the same class.
Because of the above, it would not make sense to access Form1.btnName. You have to do it through the specific instance.
Form's controls are not public by default, define a method for that.
Windows forms projects, usually have a main that runs the form. There you can access the form instance and hand it to something else in the app.
The above answers the specific question. Note that there are multiple ways to achieve different scenarios, and what you really want to do might not need the above approach.
This is because by default, the controls on a form are not public (unlike in VB6 which all controls were exposed publicly).
I believe you can change the visibility accessor in the designer to public, but that's generally a bad idea.
Rather, you should expose a method on your form that will perform the action on the button, and make that method accessible to whatever code you want to call it from. This allows for greater encapsulation and will help prevent side effects from occurring in your code.
You'll have to specify it on your specific instance of Form1.
Ie: If you have something like Form1 myForm = new Form1(...);, then you can do myForm.btnName.Enabled = false;
This will also require that btnName is public. It would be "better" to make a property or accessor to retrieve it than directly provide public access to the, by default, private button field member.
You need to add a public property, or method to set the button.
public void DisableBtnName()
{
this.btnName.Enabled=false;
}
public Button BtnName
{
get { return this.btnName;}
}
In Form1, create a object for external class(add button name in the parameter)
Class1 obj_Class1 = new Class1(btnName);
In Class1 , create a private button
private System.Windows.Forms.Button btnName;
In Class1 Construct
public Class1(System.Windows.Forms.Button btnName)
{
this. btnName = btnName;
}
then you can access your button like,
btnName.enabled = false/true;

Categories