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