-1

I have COM server application which controls another application CANoe. I want to show a progress bar on Form2 of COM application. The value of progress bar should be updated in the EventHandler. The eventHandler calls a method of form2 which will update the value of progress bar. The EventHandler is in main form.

private void mCANoeProgProgressChangedInternal(string sysvarName, object Value) // in main Form
{
    if (mCANoeMeasurement != null && mCANoeMeasurement.Running)
    {
        ProgressBarForm.Prog_progress(Value);
    }
}

And in Form 2 -

public void Prog_progress(object value)
{
    progressBarProg.Value = (int)value;
}

it is showing an error

"An object reference required for the non-static field, method or property 'Form2.Prog_progress(object)'" at - ProgressBarForm.Prog_progress(Value); in main form.

Please provide your comments.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
A.BHi
  • 33
  • 4
  • Welcome to StackoverFlow. Please [take the tour](https://stackoverflow.com/tour), read about [how to ask good questions](https://stackoverflow.com/help/how-to-ask) and learn about [how to create a Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve). – Gaurang Dave Apr 05 '18 at 07:34
  • Welcome to stack overflow! Before posting a question here you are encouraged to do a thorough research. The error message is pretty clear in the first place. In case of any further questions after that, I'm sure a simple search with a search engine of your choice would have produced the answer quicker than writing your question. – Sefe Apr 05 '18 at 07:38
  • ProgressBarFrom is a class name of form2. – A.BHi Apr 05 '18 at 08:27

2 Answers2

1

In form 1 you need to execute Prog_progress method on an instance of Form2, not on the class (in a static way).

In Form1:

private ProgressBarForm _progressForm = new ProgressBarForm();
(...)
private void mCANoeProgProgressChangedInternal(string sysvarName, object Value) // in main Form
{
     if (mCANoeMeasurement != null && mCANoeMeasurement.Running)
     {
         _progressForm.Prog_progress(Value);
     }
}
Miq
  • 3,931
  • 2
  • 18
  • 32
0

Probably you are missing instantiating the Form2,

// This is for your parent Form1

public partial class Form1 : Form
{ 
    private void mCANoeProgProgressChangedInternal(object sender, EventArgs e)
    {
        ProgressBarForm frm = new ProgressBarForm(); 
        frm.DoSomething(value);
    }
}
  • Instantiating a new form every time something changed would lead you to swamping your memory with unnecessary, duplicated objects. – Miq Apr 05 '18 at 08:28
  • Agreed. Better to use singleton pattern perhaps to keep it checked – CodingLearner Apr 05 '18 at 08:37
  • Singleton might be too much, field or property in the parent class would be sufficient. Nevertheless, living object is needed :) – Miq Apr 05 '18 at 08:40