How to access the control attribute in UserControl - c#

I add a userControl page. named ModifyUC.ascx, in the control there is a label and textbox.
When I use the userControl in the page named RangeCreate.aspx. I want the set the label and textbox is invisible.
When I use the method
protected ModifyUC createUC;
((ExtendedLabel)createUC.FindControl("RangeCodeLable")).Visible = false;//show error
((RadTextBox)createUC.FindControl("RangeCodeText")).Visible = false;
the error is that
(ExtendedLabel)createUC.FindControl("RangeCodeLable") is null.
so ((ExtendedLabel)createUC.FindControl("RangeCodeLable")).Visible.
Object reference not set to an instance of an object.

This will work out cleaner if you expose RangeCodeLable and RangeCodeText as public properties of ModifyUC, or better, if you expose a public method to hide them both.
Then, when you use ModifyUC elsewhere, just typecast it (if you need to), and then access the public properties/methods.

One thing you can do here is first of all create 2 public properties in your user control's code behind page.
Say
public bool RangeCodeLabelVisible {get;set;}
public bool RangeCodeTextVisible {get;set;}
and on the page load event of this UC just check these values and make you label and text box visible\invisible.
Now you need to set these properties from the page in which the UC is used.
As these properties are public, it would be accessible to you.
Not set the values here and this should work fine.
Rhanks,
Rahul

Related

Pass form to another class and extract controls and properties

This is more so a general question than an issue I have. I have a form with 30+ controls that I will use to populate a model, which eventually ends up in a database.
I was wondering, however, if I could just pass the whole form object to another class, and pull the contents out in the other class without setting up a whole bunch of getters and setters.
Let's say I have a form Form1, and I make this call:
OtherClass.Validate(this)
Then, in the OtherClass (which is in a different project in the same solution) I have:
public static void Validate(Form1 myForm)
I have played around with this a little. In the Validate() method, if I put a watch on myForm, I can see all the form controls and properties, but I don't know if there is a way to just pull them out. If I type myForm., intellisense shows me all the standard form methods and properties, but not the controls and properties specific to the form. Has anybody tried this successfully?
You may grab controls from a form object using
myForm.Controls
This gives you a collection of controls within the form. You may iterate through them with a foreach loop.
Example with this form containing two buttons. You may use the following code to get the controls text.
public static void Validate(Form1 myForm)
{
foreach (Control control in myForm.Controls)
{
string text = control.Text;
Console.WriteLine(text);
}
}
Triggering the above function prints the following to the console. (Using this form)
button2
button1
This method works for TextBox and other controls too. However, it may be trickier if you have controls within controls. You may solve that by making a recursive function.
Each control on a form has a property called "Modifiers":
It is "Private" by default (for a reason: UI is the most likely subject for changes, any logic outside the form should not depend on controls. One control can be replaced with another, a group of controls can be replaced with a custom control. When controls are public, such change is not incapsulated and triggers changes in many parts of the system)
Controls which are added on a form, are serialized into C# code in form.designer.cs file. Code for textBox1, when it has modifier Private:
private System.Windows.Forms.TextBox textBox1;
Change it to Public like shown on a screenshot, and it will become
public System.Windows.Forms.TextBox textBox1;
Public controls will be accessible like any other public fields:
public static void Validate(Form1 myForm)
{
if (String.IsNullOrEmpty(myForm.textBox1.text))
{
// do smth about empty field
}
}

C# Windows Forms: Access Controls(Labels, Buttons) in other Classes

I need to know, how I can access lables or buttons other than in my "Form1"-Class.
My Problem:
I created for example labels, buttons via the design viewer. Now I can access
them in my Form1 Class. (testlabel.Enabled == true) just for example.
What I CAN'T do: Access those labels, buttons in another class! Let's say
I have a class "second-class" and I want to have a method there, that changes
the property of a label to
`testlabel.Enabled == false`
That's not possible, because in that "second-class" it's not visible.
So, is there an obvious easy solution to make those controls accessible in other classes?
Create a method in that (Second class) which takes that component (Label or Button or whatever you want to modify) as parameter into that method.
public void disableLabel(Label inputLabel)
{
inputLabel.Enabled == false
}
Create a method like the above.
Now in the form1 class you just to need to call that method and pass your Label into that method to Disable it.
SecondClass objSecondClass = new SecondClass();
objSecondClass.disableLabel(testlabel);
Every control in a form class is created by default with its property Modifiers set to Private
If you change it to Public you could access the control instance from another class.
However this is really a bad practice to follow. Messing with the visibility of the control is dangerous and could cause very complicated bugs to resolve.
If you really need to change something in your form class then provide a public method and call this method to change the internal functionality of the target form

Create property of UserControl Instances (not new)

I have a usercontrol where I want a property that can list all the other instances of the same usercontrols in the Windows Form.
Eg. I have a simple usercontrol (sidebarbutton). I drag-drop 2 instances of it in a UserForm. Now I want a property (in the usercontrol itself) that can list both of them.
I have written this property. However, when used in Property Browser Window of Visual Studio, it allows me to add new instances of sidebarButton control.
private List<SidebarButton> _sidebarButtons;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Category("Roshan")]
public List<SidebarButton> SidebarButtons
{
get { return this._sidebarButtons; }
set { this._sidebarButtons = value; }
}
How to add the 2 instances that I drag-droped in the form in this property.
I know the property needs to be modified but don't have a proper direction to take. Please HELP me.
One way of doing this could be that upon addition of the user control to the form, the user control notifies all other user controls of that type that it now exists. Your user control can implement an interface like so:
public interface INotifiable
{
public void AddToList(INotifiable newButton);
}
When the user control is added to the form you iterate through all the controls and check whether they implement this interface. If they do you call the AddToList method and pass the newly added control to it.

custom windows form properties

I have created a customize windows form and I just don't know how should I set properties to it.
for example I've created a form with a progress bar, button, and a label and want to set the text of the label, the value of the progress bar, and to get access to the buttonClick Event method form the windows form application that uses the control.
In other words just get access to all the default properties of each control inside.
Is it possible? and how should I do it?
thanks very much!
If I want to to get access to the buttonClick Event method how should I do it?
You need to cast from Control to the type of your custom control before you can access the properties you have defined.
var myCtrl = (MyControl)controlRef;
myCtrl.MyProperty = xxxx;
This code assumes that MyProperty has been declared as public.
If I understand your question correctly, you want to expose controls on a form to outside code.
One way to achieve this would be to declare accessible properties on the form, for example:
public ProgressBar MyProgressBar
{
get { return progressBar1; }
}
If you wish to only expose certain properties of the controls, you could also have properties that access these directly, like so:
public int MyProgressBarValue
{
get { return progressBar1.Value; }
set { progressBar1.Value = value; }
}

Settings properties of a child in a composite control in an .aspx page

I've got a composite control (class) that exposes an asp:Label through a get-property. Is is possible to set the Text property of the Label through aspx-code?
I'd like to do something like this:
<cfw:MyCompositeControl runat="server" Label.Text="Test" />
One solution is adding each property to the composite class (like a public LabelText), but I'd like to set any property of any child control. So as new features of child controls become available, I'd like to be able to use them on my composite control. So basically I would like to set any property of the exposed child control.
You could do it with inner properties:
[ParseChildren(ChildrenAsProperties = true)]
public partial class MyControl: UserControl
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public TestClass TestLabel
{
get;set;
}
}
public class TestClass
{
public string Field1
{ get; set; }
public string Field2
{ get; set; }
}
Markup:
<MyTag:MyControl runat="server">
<TestLabel Field1="a" Field2="b" />
</MyTag:MyControl>
I've never actually done this with a simple property before - usually, you are using collections. In playing with this example on my own, in the markup window, Visual Studio will allow you to create more than one instance of TestLabel inside <MyTag:MyControl> - that is, it doesn't seem to care that it's a simple property rather than a collection, I suspect if you put in more than one entry just the last one would result.
FYI... if you haven't done this before, prepare to be annoyed by intellisense. It can be annoyingly obtuse about updating the behavior after you make changes to a class, you will need to recompile and probably wait for some arbitrary amount of time before it acts the way it's supposed to.
You need to expose it as a property in the composite control's class:-
public string LabelText
{
get
{
return Label.Text;
}
set
{
Label.Text = value;
}
}
Then you can control it from the server tag like:-
<cfw:MyCompositeControl runat="server" LabelText="Test" />
You should expose a public property that returns/sets the Text property of the Label.
MSDN
Edit:
Your idea to access all child controls of the composite control from the page is not recommended:
One approach would be to access the child controls via MyCompositeControl.FindControl(ID) (or extension methods) what would be very static and error-prone if you want to remove controls or change the IDs.
Another approach would be to make the child controls public but that is also bad design because it opens the door for misusing your control and would be also problematic if you want to change the childcontrols and pages are already accessing them directly.

Categories