0

My first C# console program works well, so I am moving now to a program using Forms. I have a label1, lable2 a button1, and I want to create a button2. everything works, but can't figure out how to use the same COM object in button2 without having to define it again.

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        dynamic scope = Activator.CreateInstance(Type.GetTypeFromProgID("LeCroy.XStreamDSO"));
    }

    public void label1_Click(object sender, EventArgs e)
    {

    }

    public void button1_Click(object sender, EventArgs e)
    {
        label2.Text = "reading back instrument ID";
        button1.Text = " 2s pause between successives commands...";
        dynamic scope = Activator.CreateInstance(Type.GetTypeFromProgID("LeCroy.XStreamDSO"));
        dynamic Replay = scope.InstrumentID;
        var Replayvalue = Replay.Value;
        var Replaytype = Replay.type;
        label1.Text = "You are connected to scope model:" + Replayvalue;
        label2.Text = "Going to recall default setup";
        dynamic CMD = scope.SaveRecall.Setup.DoRecallDefaultPanel.ActNow();
        System.Threading.Thread.Sleep(2000);
        label2.Text = "  all  Done!";
        System.Threading.Thread.Sleep(200);
        label2.Text += "\nTrigger mode Auto";
        CMD = scope.Acquisition.TriggerMode = "Auto";
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nShow measurements ";
        CMD = scope.Measure.ShowMeasure = true; // turn measure panel on
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nHorizontal scale up";
        CMD = scope.Acquisition.Horizontal.HorScaleUp.ActNow;
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nHorizontal scale up one more time..";
        CMD = scope.Acquisition.Horizontal.HorScaleUp.ActNow;
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nShow measurements statistics";
        scope.Measure.StatsOn = true;
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nTrigger mode Single";
        scope.Acquisition.TriggerMode = "Single";  //set single trigger mode 
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nclear all sweeps";
        CMD = scope.Measure.ClearSweeps;  //clear sweeps in messurements
        System.Threading.Thread.Sleep(2000);
        label2.Text += "\nTrigger mode Single...again";
        scope.Acquisition.TriggerMode = "Single";  //set single trigger mode 
        System.Threading.Thread.Sleep(2000);
        label2.Text = "\nall commands executed!";

        button1.Text = "click here to redo all...";

    }

    public void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        dynamic CMD = scope.SaveRecall.Setup.DoRecallDefaultPanel.ActNow();
    }
}

}

I get the error "the name scope doen't exist" in button2_click. CMD was defined in button1_click, so doesn't exist in button2_click and I can understand this, but why doesn't scope exist when defined in Form1_load and/or even in main? I do not know how to declare it and make it visible in all fucntions in Form1 without having to release and redefine it in each action button. Is there a way to do this? thanks

2 Answers 2

1

you can move it to a higher scope, ie right under your class declaration and leave it empty (null) then you can acess it with button2 but it will always be null until u assign it with button one first.

Sign up to request clarification or add additional context in comments.

1 Comment

I am no so sure I have understood what to do but will try... WIll let you knowhow it goes. Thanks
0

problem solved declaring variable in first form loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.