0

related to this topic:

if condition for MDI Parent Control

i need to call text from my label in DtextEditoR (form) to another label in frmMain (form) . and i use timer instead of button since i follow the answer here: Communicate between two windows forms in C#

which leads to:

frmMain.cs:

private DtexteditoR a;
        public frmMain(Form callingForm)
        {
            a = callingForm as DtexteditoR;
            InitializeComponent();
        }

        private void timercountline_Tick(object sender, EventArgs e)
        {
                a.lblcl = lblcountline.Text;
        }

DtexteditoR.cs

public string lblcl //rich
        {
            get { return lblcountline.Text; }
            set { lblcountline.Text = value; }
        }

but the thing was this error always shows: enter image description here

what should i do to remove that error? pls help thanks a lot!

Community
  • 1
  • 1
Elegiac
  • 366
  • 10
  • 25
  • So you have a `Label` object on each of your forms called `lblcountline`? – Adrian May 10 '13 at 06:51
  • Which line throws the error? Where is Dtexteditor initialized? Your code/question isnt clear at all. – bobbyalex May 10 '13 at 06:51
  • @Adrian, yes both label for diff form have the same name – Elegiac May 10 '13 at 06:52
  • @BobbyAlexander the a.lblcl = lblcounline.Text – Elegiac May 10 '13 at 06:53
  • check in exception line lblcountline and Text of that , all ways end to lblcountline is null !!! check that ! use F11 to step into and line to line trace ! – mojtaba May 10 '13 at 07:16
  • It was voted down because your question is not clear and is missing information. For eg: where is callingForm created? – bobbyalex May 10 '13 at 07:18
  • 1
    Its probably a race condition: Your timer might be firing before the callingForm is initialized. Temporarily disable the timer and do the same inside a click event handler. If it doesnt throw an exception then it is a timing issue. – bobbyalex May 10 '13 at 07:22
  • @BobbyAlexander i use button sir but still same result – Elegiac May 10 '13 at 07:33
  • You still havent answered my question: where is callingForm initialized? Have you moved the InitializeComponent in that form? – bobbyalex May 10 '13 at 07:50
  • @BobbyAlexander calling form initialized in fromMain.cs sir ... no sir – Elegiac May 10 '13 at 08:06
  • 1
    This should be easy enough to figure out by putting break points. Put a break point on the line and figure out which variable is null. If its a then your form is not being initialized. If its lblcountline then the text box is not initialized. Trace back up your calling stack to figure out why. This is very academic, you just need to put in some effort. – bobbyalex May 10 '13 at 08:25
  • Button click can cause a race condition the same as the timer, because they are both doing the same job--the job is to fill `a.lblcl` prior to it being called by `a = callingForm as DtexteditoR;`, which is your first line of code. – vapcguy Jun 21 '16 at 16:09

2 Answers2

1

check followings :
1- check variable a in constructor not null
2-after InitializeComponent you can get design objects :

public frmMain(Form callingForm)
{
InitializeComponent();
a = callingForm as DtexteditoR;
}

3- your assignment both side seems that the same : a.lblcl = lblcountline.Text that means :

lblcountline.Text = lblcountline.Text  : lblcountline.Text = value !!

4- check timer interval and enable it after InitializeComponent ( set in designto false )

public frmMain(Form callingForm)
{
InitializeComponent();
a = callingForm as DtexteditoR;
timer1.enabled=true;
}
mojtaba
  • 339
  • 1
  • 4
  • 17
  • your timer time interval is very small , try bigger interval and debug a variable that not null or null – mojtaba May 10 '13 at 06:56
  • still no change even interval small or big that error still exist :/ @mojtaba – Elegiac May 10 '13 at 06:57
  • i need a specific answer pls not this :/ i already do all this but still nothing happens . but still thanks for the help :/ – Elegiac May 10 '13 at 07:10
1

You can't expect a = callingForm as DtexteditoR to get filled from either a timer or button click fast enough for it not to be null when you are calling it as the first line in your frmMain() function. Skip these. You have to, instead, fill the variable on the old callingForm form code-behind before calling for it on the new one in frmMain(). Assuming lblcountline is on callingForm, you'd set a.lblcl = lblcountline.Text; in that code, there, then, in your frmMain(), it should be available to you.

vapcguy
  • 7,097
  • 1
  • 56
  • 52