16-09-2005, 10:41
|
|
|
|
חבר מתאריך: 23.11.03
הודעות: 4,041
|
|
אוקיי, אז קצת סדר בבלאגן
כדי לגשת לרכיבים/מאפיינים של מחלקות אחרות, אתה צריך להגדיר את ה- Getter וה- Setter לאותו הרכיב, ואז לקרוא לו מבחוץ.
הסיבה שאתה עף ב- NullReference היא כי אתה לא עושה new באף מקום
זאת הדרך הנכונה לעשות את זה. אתה יוצר מחלקה חדשה (new Srv), חושף את האובייקט שלה (richText לצורך העניין), ואז מלביש אותו על האובייקט הלוקאלי.
קוד PHP:
public partial class Form1 : Form
{
// init new instance of Srv()
private Srv srv = new Srv();
// create local rich text object
private RichTextBox richT;
public Form1()
{
InitializeComponent();
// set the richText object from Srv() to local richT;
// now, you can get and set text to the richText in Srv()
// from local richT object
this.richT = this.srv.RichText;
// next, place this control on our form
this.Controls.Add(this.richT);
// set text..
this.richT.Text = "Started";
}
private void button1_Click(object sender, EventArgs e)
{
// stop :)
this.srv.StopSrv("Stopped");
}
}
public class Srv
{
private RichTextBox richText = new RichTextBox();
public RichTextBox RichText
{
get { return this.richText; }
set { this.richText = value; }
}
public Srv()
{
}
public void StopSrv(String message)
{
this.richText.Text = message;
}
}
נערך לאחרונה ע"י hmemcpy בתאריך 16-09-2005 בשעה 10:48.
|