Evar variable in Forms

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Evar variable in Forms

Postby sebba » Wed Dec 23, 2020 10:23 am

Hello,

I'm new here, also I am new with C# and UCCNC... I have some Mach3 and VB scripting experience but this is another story.

I am trying to make a macro which have to read the Evar variable and use it in some other functions.
I made a form, I can initially read the Evar value but finally this variable is lost.
I think is something related with public/private variables but it seems is too "early" for me to figure out how to solve this.
So, the macro is the next one:

I want it to work like this:
Entering MDI: M20000 E125
Windows popup appear (and ask for some other info, not implemented for now)
And finally do G0 X125 (Evar's value)

Code: Select all
exec.AddStatusmessage("Evar = " + Evar);

if (Evar != null)
{
AS3.Setfield (Convert.ToInt32(Evar),21111);
AS3.Validatefield(21111);
}

Button button1 = new Button();
button1.Size = new System.Drawing.Size(110, 47);
button1.Location = new System.Drawing.Point(28, 149);
button1.Text = "Press me";
button1.Click += new EventHandler(button1_Click);

Label label1 = new Label();
label1.Size = new System.Drawing.Size(57, 22);
label1.Location = new System.Drawing.Point(56, 93);
label1.AutoSize = true;
label1.Font = new System.Drawing.Font("Trebuchet MS", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label1.Text = Convert.ToString(Evar);

MyForm = new Form();
MyForm.Size = new System.Drawing.Size(630, 300);   // Enter width & height of Form
MyForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
MyForm.Controls.Add(button1);        // Add Button1
MyForm.Controls.Add(label1);       // Add label1
MyForm.ShowDialog();

MyFunction();


#Events

Form MyForm; //This is a global variable, a Windows Form

 

void button1_Click(object sender, EventArgs e)
{
 MessageBox.Show("Evar " + Evar);  // CS0103 | in line: 57 | error text: The name 'Evar' does not exist in the current context
 MyForm.Close();
}

void MyFunction()
{
 exec.Code("G0 X" + Evar); // CS0103 | in line: 57 | error text: The name 'Evar' does not exist in the current context
}
 


It seems the Evar is "lost" after #Events and lines like "MessageBox.Show("Evar " + Evar);" or "exec.Code("G0 X" + Evar); ", are generating the CS0103 error.
How can I make this working?

Thank you very much,
Seb
- YQWQ
User avatar
sebba
 
Posts: 53
Joined: Mon Dec 21, 2020 9:56 am
Location: Bucharest, ROMANIA

Re: Evar variable in Forms

Postby ger21 » Wed Dec 23, 2020 1:12 pm

Would this work?


Code: Select all
Double XPosition = Evar;

....
....
....

exec.Code("G0 X" + Evar);
while(exec.IsMoving()){}
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Evar variable in Forms

Postby dezsoe » Wed Dec 23, 2020 1:17 pm

Define a variable after the #Events, e.g. double? myevar;. Before the #Events write myevar = Evar;. The variables after the #Events are public in the macro, so you'll see them in every subroutine.
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Evar variable in Forms

Postby sebba » Wed Dec 23, 2020 2:16 pm

dezsoe wrote:Define a variable after the #Events, e.g. double? myevar;. Before the #Events write myevar = Evar;. The variables after the #Events are public in the macro, so you'll see them in every subroutine.



Hello, and thank you all for replies.
I tried to do this, for now no more errors appear but the new double myEvar variable seems to be null
my actual code (the last part) looks like this:

Code: Select all
myEvar = Evar;

#Events
double? myEvar;

Form MyForm; //This is a global variable, a Windows Form

void button1_Click(object sender, EventArgs e)
{
 MessageBox.Show("myEvar " + myEvar);   
 MyForm.Close();
}


the output of "MessageBox.Show("myEvar " + myEvar);" looks like this:
E125.PNG
E125.PNG (9.45 KiB) Viewed 6629 times


Also, thank you @ger21, for your input. Same issues like before.

Thank you,
- YQWQ
User avatar
sebba
 
Posts: 53
Joined: Mon Dec 21, 2020 9:56 am
Location: Bucharest, ROMANIA

Re: Evar variable in Forms

Postby dezsoe » Wed Dec 23, 2020 2:42 pm

See this post and search the web for "C# double? type". In the manual you'll find how to check if the Evar has value.
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Evar variable in Forms

Postby dezsoe » Wed Dec 23, 2020 3:50 pm

It work by me (I wrote M1063 E1, X moved to 1):

Code: Select all
exec.AddStatusmessage("Evar = " + Evar);

myEvar = Evar;

Button button1 = new Button();
button1.Size = new System.Drawing.Size(110, 47);
button1.Location = new System.Drawing.Point(28, 149);
button1.Text = "Press me";
button1.Click += new EventHandler(button1_Click);

Label label1 = new Label();
label1.Size = new System.Drawing.Size(57, 22);
label1.Location = new System.Drawing.Point(56, 93);
label1.AutoSize = true;
label1.Font = new System.Drawing.Font("Trebuchet MS", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label1.Text = Convert.ToString(Evar);

MyForm = new Form();
MyForm.Size = new System.Drawing.Size(630, 300);   // Enter width & height of Form
MyForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
MyForm.Controls.Add(button1);        // Add Button1
MyForm.Controls.Add(label1);       // Add label1
MyForm.ShowDialog();

MyFunction();


#Events

Form MyForm; //This is a global variable, a Windows Form

double? myEvar;


void button1_Click(object sender, EventArgs e)
{
  MessageBox.Show("Evar " + myEvar);  // CS0103 | in line: 57 | error text: The name 'Evar' does not exist in the current context
  MyForm.Close();
}

void MyFunction()
{
  exec.Code("G0 X" + myEvar); // CS0103 | in line: 57 | error text: The name 'Evar' does not exist in the current context
}

evar1.png
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Evar variable in Forms

Postby sebba » Wed Dec 23, 2020 4:18 pm

Great, thank you very much!
It is working now, even mine code, after I moved "myEvar = Evar;" in the first lines of code...

Best regards,
Seb
- YQWQ
User avatar
sebba
 
Posts: 53
Joined: Mon Dec 21, 2020 9:56 am
Location: Bucharest, ROMANIA


Return to Macros

Who is online

Users browsing this forum: No registered users and 2 guests