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; }
}
Related
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.
I have a main form with some buttons, textboxes, labels, etc.
On a second form I would like to copy the text from the main forms textbox onto the second form.
Have tried:
var form = new MainScreen();
TextBox tb= form.Controls["textboxMain"] as TextBox;
textboxSecond.Text = tb.Text;
But it just causes an exception. The main screen textbox is initialised and contains text.
When I hover over form I can see all the controls are there.
What am I doing wrong?
Looking at the original code, there are two potential reasons for the NullReferenceException you are getting. First, tb is not defined in the code you provide so I am not sure what that is.
Secondly, TextBox textbox = form.Controls["textboxMain"] as TextBox can return null if the control is not found or is not a TextBox. Controls, by default, are marked with the private accessor, which leads me to suspect that form.Controls[...] will return null for private members.
While marking the controls as internal will potentially fix this issue, it's really not the best way to tackle this situation and will only lead to poor coding habits in the future. private accessors on controls are perfectly fine.
A better way to share the data between the forms would be with public properties. For example, let's say you have a TextBox on your main screen called usernameTextBox and want to expose it publicly to other forms:
public string Username
{
get { return usernameTextBox.Text; }
set { usernameTextBox.Text = value; }
}
Then all you would have to do in your code is:
var form = new MainForm();
myTextBox.Text = form.Username; // Get the username TextBox value
form.Username = myTextBox.Text; // Set the username TextBox value
The great part about this solution is that you have better control of how data is stored via properties. Your get and set actions can contain logic, set multiple values, perform validation, and various other functionality.
If you are using WPF I would recommend looking up the MVVM pattern as it allows you to do similar with object states.
PhoenixReborn is correct. The problem is that you are creating a new MainScreen, which means that new controls are created, so unless the text in your controls are initialized in the form constructor, they are going to be empty. Usually, the way to handle this is to pass the first form instance to the second form, like this:
SecondForm second = new SecondForm(this);
and in the second form:
public SecondForm (MainForm form)
{
// do something with form, like save it to a property or access it's controls
}
That way, the second form will have access to the first form's controls. You might consider making the properties you need to use public (in the designer properties pane). That way you can just do form.textboxMain.Text.
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
I have an initial value property like this:
[Category("Main")]
[Description("Intial Value")]
[DefaultValue(10)]
public int InitialValue
{
get { return m_initialValue; }
set {
m_initialValue = value;
this.TrackBar.Value = this.m_initialValue;
}
}
So in my constructor I do this for example:
this.InitialValue = 10;
To my surprise when dragging the custom control on a form the setter is not called so that my trackbar value is not synchronized.
Why ?
Only when I change the property in dialog box the setter is called.
I decided to take your advice as suggested in one of the comments:
You can try by yourself will take 2 minutes.
So I did (it took about 3 minutes), and I was unable to reproduce the behavior that you described.
Here are the exact steps that I followed:
Created a new Windows Forms Application.
Added a new User Control to my project.
Opened the new User Control in design view and added a TrackBar control (leaving the TrackBar control's properties all set to their defaults).
Added the following code to the User Control class (exactly the same as you posted above, with the addition of a private field m_initialValue that you omitted from the original example):
public class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.InitialValue = 10;
}
[Category("Main")]
[Description("Intial Value")]
[DefaultValue(10)]
public int InitialValue
{
get { return m_initialValue; }
set
{
m_initialValue = value;
this.trackBar1.Value = this.m_initialValue;
}
}
int m_initialValue;
}
Built the project.
Opened the default Form (Form1) that was created with the new project in design view.
Dragged the User Control that I had just created (UserControl1) out of the toolbox where it was automatically placed and onto the surface of the form.
The indicator on the slider bar appeared all the way to the right side (the correct and expected position given the default Maximum value of 10). Now, you tell me: What are we doing differently?
Try adding [Browsable(true)] .
The key portion of your question is here:
when dragging the custom control on a form
You're still in the designer, and the designer cheats a bit to render things. Does this still happen when you actually run the application?
I have a user control with a textbox in a win forms application.
I would like to change the property of that textbox using the properties window of visual studio .
I am using that control in various forms of same project ,is it possible?
I have set the modifier property of text box as public and set following property in the user control:
public TextBox mytextBox
{
get { return textBox1; }
set { textBox1 = value; }
}
Thanks in Advance.
What is the intent of doing this? Are you trying to have "one TextBox control shared by multiple forms" (that is not really practical). However you can set up your forms in such a way as to have all forms update in response to a single change.
[TypeConverter(typeof(ExpandableObjectConverter))]
public TextBox mytextBox
{
get { return textBox1; }
set { textBox1 = value; }
}
Notes:
From the perspective of the PropertyGrid, the setter has no benefit in this case; the properties of the already-assigned TextBox are being modified in-place.
Remember to create an initial value, and to add the TextBox to the UserControl's control-collection. If you used the VS designer to create the TextBox, this should have been done already. If you find that the VS designer method InitializeComponents() is undoing your changes, create and add the control yourself.
You may have to rebuild the project and/or reopen the Forms designer for the change to be visible.
Off-topic: Use Pascal-case for properties, and the auto-implemented get;set; pattern for readability, if at all possible.