accessing tabcontrol from myclass.cs - c#

Why can't I access the tabcontrol from within myclass.cs?
The tabcontrol is in form1.cs and my code that tries to create a new tabpage is in myclass.cs. I've even tried setting tabcontrol property to public but that didn't work.

just change the modifier on the control to public.
Once you do that and you have Form myForm = new Form();
you will be able to do:
myForm.myTAB.TabPages.Add(myTabPage);
(You will need to create the TabPage of course.

I'd suggest to wrap the TabControl in an public readonly property on the form's codebehind file, rather than making the control itself public. Just for the sake of code security (like being able to reassign your TabControl to something new).
public TabControl MyTabControl {
get {
return privateTabControl;
}
}
Also, don't forget you'll need an instance to your form in MyClass, otherwise you can't access instance members like "MyTabControl" or even public instance fields if you've chosen so.

I suggest you embed a reference to the TabControl on the Form in the Class myclass.cs.
You could do this either by defining a constructor for myclass that took a TabControl as a parameter, or, by defining a Public Property in myClass that holds a reference to a TabControl. Both ways are illustrated here :
public class myclass
{
// using "automatic" properties : requires C# 3.0
public TabControl theTabControl { get; set; }
// parameter-less 'ctor
public myclass()
{
}
// optional 'ctor where you pass in a reference to the TabControl
public myclass(TabControl tbControl)
{
theTabControl = tbControl;
}
// an example method that would add a new TabPage to the TabControl
public void addNewTabPage(string Title)
{
theTabControl.TabPages.Add(new TabPage(Title));
}
}
So you can set the TabControl reference in two ways from within the Form with the TabControl :
myclass myclassInstance = new myClass(this.tabControl1);
or
myclass myclassInstance = new myClass();
// do some stuff
// now set the TabControl
myClassInstance.theTabControl = this.tabControl1;
An alternative is to expose a Public Property of type TabControl on Form1 : but then you have to think about how myclass will "see" the current "instance" of Form1 ... if there is more than one instance of Form1 ? In the case there is one-and-only-one TabControl you could use a static Property on Form1 to expose it.
In this case "who is creating who" becomes of interest : if the Form is creating myclass; if myclass is creating the Form; if both myclass and the Form are being created by another entity in the application : I think all these "vectors" will bear on what is the best technique to apply.

Are you creating an instance of form1 in myclass.cs and checking the existence of tabControl on that instance? If you want to access form1.tabControl in myclass.cs then you will need to make the tabControl as public static in form1.cs

This might be an overly simple suggestion, and you've probably solved this by now, but did you remember to include using System.Windows.Forms at the top of the class file? Visual Studio may not include that reference automatically when adding a new class file.

Related

Changing visibility properties of a form from a class

I have a multi-page program which changes its displayed elements by changing its visibility, which is written in the mainForm class(renamed from Form1) . Now as my program is getting more and more complicated I was thinking to make some kind of an external class in wich all these states would be changed becouse i have 26 text boxes and 3 options of displaying them which means I have 78 lines of textBox1.visibility = true; lines.
I have tried creating a class and changing the modifiers of all the textboxes to public and created an instance of form1 in my interfaceClass (MainForm mainform = new MainForm();) but this does totally not change anything to my form at all although no syntax or runtime errors happen.
I just want maybe some suggestions on how this can be realized if it can be at all.
You don't need a separate class for this; just write a single method in your form called CheckVisibility() and call that wherever you need to make sure your controls are shown or hidden properly. Include all of the necessary conditions and show/hide changes in that method.
Set the control to public in the designer:
public System.Windows.Forms.Button button1;
Create a new class and for example rename it to exampleClass
public class exampleClass
{
public static Form1 frm;
public static void HideButton()
{
frm.button1.Visible = false;
}
}
Add this after Form1 InitializeComponent:
exampleClass.frm = this;
Now you can hide the button from anywhere you want:
exampleClass.HideButton();

Circular dependency when adding reference (Delegates)

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.

c# Forms Project: Passing Information to User Control

I have created a forms application which uses a TabControl. For each tab I want to place a single UserControl (created in the same project) which contains all the other controls. However, I will need to pass some information from the primary form to the UserControl for it to work property with events, methods, etc. How can/should I do this?
I tried creating a constructor with parameters but then Designer fails and I have to go in and delete out the added UserControl references.
Thanks!
The constructor parameter is the correct method. However, there must still be a default constructor in order for the Designer to be able to construct (and draw) a copy of the object.
My usual workaround is to put a clause in the default constructor, checking to see that we are in "design mode" and throwing an exception if not:
public class MyForm: Form
{
public MyForm()
{
if(!DesignMode) throw new InvalidOperationException("Cannot use default constructor in production code");
}
public MyForm(MyDependency dependent)
{
...
}
}
you can pass information by a function that created in usercontrol.cs file.
for example in usercontrol.cs
public string name;
public void SetName(string pname)
{
this.name = pname;
}
or maybe you want to change button name
Button mybutton = new Button();
public void SetButtonName(string btname)
{
this.mybutton.Text = btname;
}
Now you can call these functions in your mainform.cs
Myusercontrol usc = new Myusercontrol();
usc.SetName("this is string for 'name' string");
usc.SetButtonName("this is string for button text");
Try creating your constructor, but also creating a default parameterless constructor.
Take a look at this question

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