开发者

accessing controls on parentform from childform

开发者 https://www.devze.com 2023-02-01 23:59 出处:网络
I want to change text in textbox onparentform from childform. I set textbox modifiers= public i have extra written a function开发者_如何转开发 in parentform

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消