Macros - Forms

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

Macros - Forms

Postby Robertspark » Thu Jun 29, 2017 11:45 pm

Here are a few more controls that can be adapted for creating a form (copy, paste + rename & relocate)

copy and save as an "Mxxx.txt" file and run from the MDI (or add a screen button as you require)

Code: Select all

// ADD Button

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);

// ADD Label

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 = "label1";

// ADD TextBox

TextBox textBox1 = new TextBox();
textBox1.Size = new System.Drawing.Size(82, 20);
textBox1.Location = new System.Drawing.Point(45, 123);
textBox1.Name = "textBox1";

// ADD PictureBox

PictureBox pictureBox1 = new PictureBox();
pictureBox1.Size = new System.Drawing.Size(112, 82);
pictureBox1.Location = new System.Drawing.Point(15, 12);
pictureBox1.ImageLocation = @"C:\UCCNC\Flashscreen\BMP\Defaultscreenset\aplus_down.png";
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;

// ADD RadioButton

RadioButton radioButton1 = new RadioButton();
radioButton1.Size = new System.Drawing.Size(85, 17);
radioButton1.Location = new System.Drawing.Point(6, 19);
radioButton1.AutoSize = true;
radioButton1.Checked = true;
radioButton1.Text = "radioButton1";
radioButton1.UseVisualStyleBackColor = true;

RadioButton radioButton2 = new RadioButton();
radioButton2.Size = new System.Drawing.Size(85, 17);
radioButton2.Location = new System.Drawing.Point(6, 46);
radioButton2.AutoSize = true;
radioButton2.Checked = false;
radioButton2.Text = "radioButton2";
radioButton2.UseVisualStyleBackColor = true;

// ADD GroupBox

GroupBox groupBox1 = new GroupBox();
groupBox1.Size = new System.Drawing.Size(122, 74);
groupBox1.Location = new System.Drawing.Point(155, 105);
groupBox1.Controls.Add(radioButton1);
groupBox1.Controls.Add(radioButton2);
groupBox1.Text = "groupBox1";

 // ADD CheckBox

CheckBox checkBox1 = new CheckBox();
checkBox1.Size = new System.Drawing.Size(80, 17);
checkBox1.Location = new System.Drawing.Point(45, 202);
checkBox1.AutoSize = true;
checkBox1.Text = "checkBox1";
checkBox1.UseVisualStyleBackColor = true;

 // ADD ComboBox

ComboBox comboBox1 = new ComboBox();
comboBox1.Size = new System.Drawing.Size(121, 21);
comboBox1.Location = new System.Drawing.Point(155, 202);
comboBox1.FormattingEnabled = true;
comboBox1.Items.AddRange(new object[] {
"Item1",
"Item2",
"Item3",
"Item4"});


// FORM

MyForm = new Form();
MyForm.Size = new System.Drawing.Size(300, 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.Controls.Add(textBox1);      // Add textBox1
MyForm.Controls.Add(pictureBox1);    // Add pictureBox1
MyForm.Controls.Add(groupBox1);     // ADD groupBox1
//MyForm.Controls.Add(radioButton1);  // ADD radioButton1 [not required if added to groupbox]
//MyForm.Controls.Add(radioButton2);  // ADD radioButton2 [not required if added to groupbox]
MyForm.Controls.Add(checkBox1);     // ADD checkBox1
MyForm.Controls.Add(comboBox1);     // ADD comboBox1
MyForm.ShowDialog();

 

MyFunction();

 

#Events

 

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

 

void button1_Click(object sender, EventArgs e)
{
 MessageBox.Show("button1 was clicked!");
 MyForm.Close();
}

 

void MyFunction()
{
 exec.Code("G0 X10");
}
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

Re: Macros - Forms

Postby Robertspark » Fri Jun 30, 2017 7:08 am

macro file attached, does tabs too....
Attachments
M500.txt
(5.25 KiB) Downloaded 816 times
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

Re: Macros - Forms

Postby shad » Fri Aug 18, 2017 9:24 pm

Hello Rob!
You tried to do reading or writing to the text box on this form from macros?
For example:
Code: Select all
void button1_Click(object sender, EventArgs e)
 {
   double res;
   string str = textBox1.Text;   // this operation do not allow to execute macros - error
   if (Double.TryParse(str, out res))
   {
      MessageBox.Show("Value = " + Convert.ToString(res));
   }
 }

Maybe I missed something?
-- Andrew
UC400ETH
UC300ETH-5LPT
NEURON Lite THC
http://neuroncnc.com/
shad
 
Posts: 331
Joined: Thu Sep 15, 2016 5:23 pm

Re: Macros - Forms

Postby dezsoe » Fri Aug 18, 2017 10:00 pm

Move this line:
Code: Select all
TextBox textBox1 = new TextBox();
after this line:
Code: Select all
Form MyForm; //This is a global variable, a Windows Form

The variables declared before #Events are local variables and cannot be seen from the event handlers. The variables after #Events are global variables in this code, like MyForm.
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macros - Forms

Postby shad » Sat Aug 19, 2017 4:47 am

Thank you Dezsoe!
It's works. Just I am was very sure that we need to declare all controls before ShowDialog() function.
Need to learn more C# realization in the macros.
-- Andrew
UC400ETH
UC300ETH-5LPT
NEURON Lite THC
http://neuroncnc.com/
shad
 
Posts: 331
Joined: Thu Sep 15, 2016 5:23 pm

Re: Macros - Forms

Postby beefy » Thu Aug 24, 2017 8:00 am

I'm pulling my hair out trying to figure out how to read textBox1 and get the contents into a DRO.

Is this possible.

I can't even declare a variable after #Events without an error being thrown when I try to run the macro.

For example if I write:
Code: Select all
static string boxData = textBox1.Text;


after #Events, an error is thrown.

I'm trying to do something like:
Code: Select all
static string boxData = textBox1.Text;

void button1_Click(object sender, EventArgs e)
{
       exec.Setfield(boxData, 3006);
}


Keith
beefy
 
Posts: 449
Joined: Mon Sep 05, 2016 10:34 am

Re: Macros - Forms

Postby dezsoe » Thu Aug 24, 2017 8:15 am

textBox1 must be a global, so declare it after #Events as
Code: Select all
TextBox textBox1;

Before #Events remove the declaratiion, just give a new value to textBox1:
Code: Select all
textBox1 = new TextBox();

Then you can use its .Text in the event handler:
Code: Select all
void button1_Click(object sender, EventArgs e)
{
       exec.Setfield(textBox1.Text, 3006);
}

You don't need a static variable for this job.
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macros - Forms

Postby beefy » Thu Aug 24, 2017 7:44 pm

Hi Dezsoe,

thank you very much for the help. If I can figure this one out it will make things a lot easier for me and save a lot of work with screenset building.

I put a "Textfield" type DRO on my main screen with a number of 3006

I THINK I've done exactly what you told me but it still throws an error. I stripped Roberts code down to leave just a button and a text box.

Code: Select all
// ADD Button

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);


// ==============================================================================
// ADD TextBox

// TextBox textBox1 = new TextBox();
textBox1 = new TextBox();
textBox1.Size = new System.Drawing.Size(82, 20);
textBox1.Location = new System.Drawing.Point(45, 123);
textBox1.Name = "textBox1";

   
// ==============================================================================   
// FORM

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(textBox1);                  // Add textBox1

MyForm.ResumeLayout(false);
MyForm.PerformLayout();

MyForm.ShowDialog();

MyFunction();

 
// ==============================================================================
#Events

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

TextBox textBox1;


void button1_Click(object sender, EventArgs e)
{
   exec.Setfieldtext(textBox1.Text, 3006);
   MyForm.Close();
}


void MyFunction()
{
   exec.Code("G0 X10");
}


If I comment out:
Code: Select all
exec.Setfieldtext(textBox1.Text, 3006);

out of the buttonpress function, the macro compiles OK.

But as soon as I un-comment that line, it will not compile and throws an error.

Can you see if I've done something wrong in that code.

Cheers,

Keith
beefy
 
Posts: 449
Joined: Mon Sep 05, 2016 10:34 am

Re: Macros - Forms

Postby dezsoe » Thu Aug 24, 2017 8:52 pm

Hi Keith,

Sorry, it was my mistake: exec doesn't have Setfieldtext, it must be AS3. That line is:
Code: Select all
   AS3.Setfieldtext(textBox1.Text, 3006);

Sorry again!
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macros - Forms

Postby beefy » Fri Aug 25, 2017 8:21 am

Hi Dezsoe,

absolutely no need for any apology. On the contrary, thank you, thank you.

I would not have had a clue how to do that if you had not shown me. I'm now quite excited that I know how to get data from a form to UCcnc.

Much appreciated, and thank you again.

Keith.
beefy
 
Posts: 449
Joined: Mon Sep 05, 2016 10:34 am

Next

Return to Macros

Who is online

Users browsing this forum: No registered users and 15 guests