I want to change text in textbox on parentform from childform.
I set textbox
modifiers= public
i have extra written a function in parentform
public TextBox txtbox
{
get
{
return mybox;
}
set
{
mybox= value;
}
}
in child form on writing
this.ParentForm. ( can't see mybox).
what i am missing.
regards,
Since ParentForm will return a Form and not your form, you need to cast it before you can access any of your custom properties:
((MyForm)this.ParentForm).textbox = "new text!";
Additionally, you are setting the whole control, not just the text.
Try this, to expose the text property only:
public string txtbox
{
get
{
return mybox.Text;
}
set
{
mybox.Text = value;
}
}
I think the problem is that ParentForm is of type Form which does not have a member txtbox. You need to cast ParentForm to your form (suppose it is Form1), like:
((Form1)this.ParentForm).txtbox
Random guess without seeing any actual code: mybox is likely not declared public.
Edit: Or, ah, yes, as Andrei says - you havn't cast the ParentForm to your parent form's type.
Related
I have a user control that will supposedly pass value to its parent form which is form1.
I used the code below.
User Control
public int _control;
public int control
{
get{return _control;}
set{_control=value;}
}
Form1 assign value to UserControl
UserControl1 uc=new UserControl1();
uc.control=1;
User Control Button_Click
var parent = this.Parent as Form1;
//MessageBox.Show(_control.ToString());
parent.userNo=_control;
Form1
public int _userNo;
public int userNo
{
get{return _userNo;}
set{_userNo=value;}
}
The problem is when i used messagebox.show, it will appear displaying 1 but when i used
parent.userNo=_control;
it returns a Null Reference Exception.
Please help!!!
That means that parent is Null. this is due to the fact that the parent is NOT an instance of the class Form1.
in fact, this cast:
this.Parent as Form1
returns NULL when this.Parent is not of the type Form1, but is another container. alternatively, if the parent is not set. to fix this, you need to get a reference of the Form to the user control or alternatively, set the parent. something like:
UserControl1 uc=new UserControl1();
uc.control=1;
uc.Parent = this;
IDE: Visual Studio, C# .net 4.0
I have two identical user control uc1 and uc2 and both are having a textbox called txtbox1
now see this code and textbox1 is public in designer so it is assessable in form1.cs, Form 1 is simple windows form which is having uc1 and uc2.
In form1 see this function which i am calling in onLoad_form1 method.
UserControl currentUC; //Global variable;
public void loadUC(string p1)
{
//Here I want:
if(p1 == "UC1)
{
currentUC = uc1;
}
if(p1 == "UC2)
{
currentUC = uc2;
}
}
Than another function which calls update the textbox1 based on currentUC value
//on load
currentUC.textbox1.text = "UC1 called";
//Here I am getting error "currentUc does not contains definition for textbox1"
If i do:
uc1.textbox1.text = "UC1 text";
uc2.textbox1.text = "UC1 text"; //it works, But based on p1 string variable I want to make control as uc1 or uc2 than I want to access its child control. please suggest how to perform this.
please don't tell if else blocks, because This functionality I have to use in various places.
Thanks.
#Lee Answer: - works just for textbox, but I am having two usercontrols i.e. two different usercontrols not instance of it. UserControlLeft and UserControlRight and both are having same textboxes, listboxes etc (with minor design changes), and I want to access/load this based on some string "left" and "right".
Since the textboxes have the same name you can look them up in the Controls collection:
TextBox tb = (TextBox)currentUC.Controls["textbox1"];
tb.Text = "UC1 called";
a better solution would be to add a property to your user control class which sets the internal text property e.g.
public class MyUserControl : UserControl
{
public string Caption
{
get { return this.textbox1.Text; }
set { this.textbox1.Text = value; }
}
}
I think you're mixing a couple of things here.
First of all, you say that you have 2 exactly the same usercontrols, do you mean the ascx files are the same, or that you have 2 instances of the same usercontrol on the page?
Let's go with all the valid options:
1. To find a control and cast it:
Assume you have the following aspx snippet:
<div>
<uc1:MyCustomUserControl id="myControl" runat="server" />
<uc1:MyCustomUserControl id="myControl2" runat="server" />
</div>
If you now want to access the control, you should do the following:
public void Page_Load()
{
var myControl ((MyCustomUserControl)FindControl("MyControlName"));
// On 'myControl' you can now access all the public properties like your textbox.
}
In WPF you can do it like this:
//on load MAINFORM
public void SetText(string text)
{
CLASSOFYOURCONTROL ctrl = currentUC as CLASSOFYOURCONTROL ;
ctrl.SetText(text);
}
// in your control SUB
public void SetText(string text)
{
textbox1.text = "UC1 called"
}
i think this should work in winforms also. And is more clean than accessing the controls from your sub-control directly
#Lee's method is good. Another method will be to use a public property with a public setter (and textbox doesn't need to be public this way).
or an interface (this way you don't care what class you have at the given moment - and no ifs):
public interface IMyInterface
{
void SetTextBoxText(string text);
}
public partial class UC1: UserControl, IMyInterface
{
public void SetTextBoxText((string text)
{
textBox1.Text=text;
}
//...
}
public partial class UC2: UserControl, IMyInterface
{
public void SetTextBoxText((string text)
{
textBox1.Text=text;
}
//...
}
using the code:
((IMyInterface)instanceOfUC1).SetTextBoxText("My text to set");
((IMyInterface)instanceOfUC2).SetTextBoxText("My text to set");
I am using two windows form application, and I want to set 1st form label's value from 2nd form.
But when I access 1st form label in 2nd form then application show this error
Object reference not set to an instance of an object.
I am using this statement to access
login_form.ActiveForm.Controls["label_name"].Text = "Hello World";
sometime i worked fine but some time show this error
Please solve my problem. I will be very thankful to you.
You would be wise to either:
Expose a property in the first form through which you can enact a change to the label text.
Expose a method in the first form that you can call to affect a label text change.
Example #1:
public class Form1 : Form
{
public String LabelText
{
get { return label_name.Text; }
set { label_name.Text = value; }
}
}
//from Form2...
login_form.LabelText = "Hello World";
Example #2:
public class Form1 : Form
{
public void SetLabelText(String TextToSet)
{
label_name.Text = TextToSet;
}
}
//from Form2...
login_form.SetLabelText("Hello World");
I would not advise simply changing the control to be Public. Indirect access is preferable.
i have opened a mainform and call a child form like
Form4 f = new Form4();
f.Owner = this;
f.Show(this);
in form4, user selects a text file, the contents of which are to be displayed in a textBox1 of mainform
i was trying something like
Owner.textBox1.Text = "file contents";
but it does'nt work
The best way to link different forms together is via events. Create an event in Form4 like FileSelected and then do something like this:
Form4 f = new Form4();
f.FileSelected += (owner, args) => {
textBox1.Text = args.FileName;
};
f.Show(this);
Besides this is really bad design, you need to make textBox1 a public member of your main form and cast f.Owner to the main form type.
Like:
Form4 f = new Form4();
f.Owner = this;
f.Show(this);
// Inside Form4
MainForm main = this.Owner as MainForm;
if (main != null) main.textBox1.Text...
A best practice would be to define yourself a property that would itself set the Text property of your private control. Here's an instance:
public partial class MainForm : Form {
public string ContentDescription {
set {
textBox1.Text = value.trim();
}
}
}
Then after, you'll be able to access this property through type-casting to your particular type:
public partial class SecondaryForm : Form {
public MainForm OwnerForm {
get {
return (MainForm)this.Owner;
}
}
public void someMethod() {
OwnerForm.ContentDescription = "file contents";
}
}
Remember that in C#, every Control is declared private. So, to access it, the best practice is to define a property that will grant you the required access to it. Making a member public is generally not a good idea, depending on what you're trying to achieve.
EDIT For the parse method, perhaps should you consider making it public or internal so that you may access it through the correctly type-casted Owner property of your child form.
Making a hlper class might be the right solution though, so it is not GUI dependent.
In Form4 you can cast Owner to the correct type:
var o = (Form1) this.Owner;
o.textBox1.Text = "file contents";
For this to work, the owner must be of type Form1 and textBox1 on that type must be a public member or property.
As Andrew already gave the correct solution for event driven, there is also a sync (or blocking) method available:
Form4 f = new Form4;
if(f.ShowDialog() == DialogResult.OK)
{
textBox1.Text = f.FileName;
}
You will need to set the "modifiers" to at least public for the properties of the control to be able to have access to it.
alt text http://gabecalabro.com/gabe/Capture.PNG
I'm dynamically adding a custom user control to the page as per this post.
However, i need to set a property on the user control as i add it to the page. How do I do that?
Code snippet would be nice :)
Details:
I have a custom user control with a public field (property ... e.g. public int someId;)
As I add a UserControl to a page, its type is UserControl. Do i just cast it into MyCustomUCType and set a property on a cast control?
p.s. looks like I've answered my own question after all.
Ah, I answered before you added the additional clarification. The short answer is, yes, just cast it as your custom type.
I'll leave the rest of my answer here for reference, though it doesn't appear you'll need it.
Borrowing from the code in the other question, and assuming that all your User Controls can be made to inherit from the same base class, you could do this.
Create a new class to act as the base control:
public class MyBaseControl : System.Web.UI.UserControl
{
public string MyProperty
{
get { return ViewState["MyProp"] as string; }
set { ViewState["MyProp"] = value; }
}
}
Then update your user controls to inherit from your base class instead of UserControl:
public partial class SampleControl2 : MyBaseControl
{
....
Then, in the place where you load the controls, change this:
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
to:
MyBaseControl uc = (MyBaseControl)LoadControl(controlPath);
uc.MyProperty = "foo";
PlaceHolder1.Controls.Add(uc);
"As I add a UserControl to a page, its
type is UserControl. Do i just cast it
into MyCustomUCType and set a property
on a cast control?"
That's what I'd do. If you're actually using the example code where it loads different controls, I'd use an if(x is ControlType) as well.
if(x is Label)
{
Label l = x as Label;
l.Text = "Batman!";
}
else
//...
Edit: Now it's 2.0 compatible
Yes, you just cast the control to the proper type. EX:
((MyControl)control).MyProperty = "blah";