How to change the color of a Button in the Windows Designer?

Started by PKirsch, May 27, 2013, 09:13:26 AM

Previous topic - Next topic

PKirsch

I'd like to change the color of a Button that I put in a form of the Windows
Designer in the Machines Simulator Editor to show the Status of a PLC-Output.
I know that I have to program it in the Logic Program of the installation
but I don't know the syntax.
If it's not possible to change the color of a Button is it possible with a
Label?

EasyPLC_Master

Hello PKirsch;

This code can help you:

GUI gui;
TextButton butt;
Label label;

public void Init()
{

gui = new GUI(MachSim);
Form form = new Form(new Vector2(100f, 100f), new Vector2(300f, 200f), "Test Form", Color.White, Color.Black, "tahoma", 0.8f, true, true, true, true,  Form.BorderStyle.FixedSingle);         
butt = new TextButton("buton", new Vector2(10, 70), "Button1", 120, Color.Gray);
label = new Label("label", new Vector2(10, 40), "PLC Output 0 Status:", Label.Alignment.Left, null, Color.Black);
form.Add(butt);
form.Add(label);
form.Show();
gui.AddForm(form);
MachSim.Components.Add(gui);
}

public void Main(GameTime gameTime)
{
   
   if(IO.GetOutput(0))
   {
        butt.Text = "ON";
        butt.color = Color.Red;
     }
     else
     {
        butt.Text = "OFF";
        butt.color = Color.Gray;
     }

}

public void Draw(GameTime gameTime)
{

}

public void Finish()
{
gui.RemoveAllForms();
MachSim.Components.Remove(gui);
}

PKirsch

Thank you for your help. Is it possible to do this if i don't want to create the buttons in the Init() but rather in the Windows Designer?
In the notes my code and the .frm Windows Designer-File.

GUI gui;
float pos = 0;
public void Init()
{
gui = new GUI(MachSim);
gui.Load(Editor.Path() + "\\MyMachines\\Bedienstationen_AFL4.frm");
Form form = gui.forms[0];
form.Position = new Vector2(ScreenW - form.size.X, ScreenH - form.size.Y);
Label lab1 = (Label)form.Controls[0];
Label lab2 = (Label)form.Controls[1];
Label lab3 = (Label)form.Controls[2];
Label lab4 = (Label)form.Controls[3];
TextButton but1 = (TextButton)form.Controls[4];
but1.OnPress += new EventHandler(but1_OnPress);
TextButton but2 = (TextButton)form.Controls[5];
but2.OnPress += new EventHandler(but2_OnPress);
TextButton but3 = (TextButton)form.Controls[6];
but3.OnPress += new EventHandler(but3_OnPress);
TextButton but4 = (TextButton)form.Controls[7];
but4.OnPress += new EventHandler(but4_OnPress);
MachSim.Components.Add(gui);
}

//Button Betriebsart Deckellinie - Aus
void but1_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, true);
    IO.SetInput(85, false);
    IO.SetInput(86, false);
    IO.SetInput(87, false);
}

//Button Betriebsart Deckellinie - Einrichten
void but2_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, false);
    IO.SetInput(85, true);
    IO.SetInput(86, false);
    IO.SetInput(87, false);
}

//Button Betriebsart Deckellinie - Hand
void but3_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, false);
    IO.SetInput(85, false);
    IO.SetInput(86, true);
    IO.SetInput(87, false); 
}

//Button Betriebsart Deckellinie - Automatik
void but4_OnPress(object sender, EventArgs e)
{
IO.SetInput(84, false);
    IO.SetInput(85, false);
    IO.SetInput(86, false);
    IO.SetInput(87, true);
}

public void Main(GameTime gameTime)
{
//Anzeige Betriebsart Deckellinie - Aus
if(IO.GetInput(84))
{
   // ??? but1.color = Color.Red;
}
else
{
  // )??? but1.color = Color.Grey;
}
}

public void Draw(GameTime gameTime)
{
}

public void Finish()
{
gui.RemoveAllForms();
MachSim.Components.Remove(gui);
}


[attachment deleted by admin]

EasyPLC_Master

Of course, the code is the same, you only have to declare the button object like 'global':


GUI gui;
float pos = 0;
TextButton but1;

public void Init()
{
gui = new GUI(MachSim);
gui.Load(Editor.Path() + "\\MyMachines\\Bedienstationen_AFL4.frm");
Form form = gui.forms[0];
form.Position = new Vector2(ScreenW - form.size.X, ScreenH - form.size.Y);
Label lab1 = (Label)form.Controls[0];
Label lab2 = (Label)form.Controls[1];
Label lab3 = (Label)form.Controls[2];
Label lab4 = (Label)form.Controls[3];

but1 = (TextButton)form.Controls[4];
but1.OnPress += new EventHandler(but1_OnPress);
TextButton but2 = (TextButton)form.Controls[5];
but2.OnPress += new EventHandler(but2_OnPress);
TextButton but3 = (TextButton)form.Controls[6];
but3.OnPress += new EventHandler(but3_OnPress);
TextButton but4 = (TextButton)form.Controls[7];
but4.OnPress += new EventHandler(but4_OnPress);
MachSim.Components.Add(gui);
}

//Button Betriebsart Deckellinie - Aus
void but1_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, true);
    IO.SetInput(85, false);
    IO.SetInput(86, false);
    IO.SetInput(87, false);
}

//Button Betriebsart Deckellinie - Einrichten
void but2_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, false);
    IO.SetInput(85, true);
    IO.SetInput(86, false);
    IO.SetInput(87, false);
}

//Button Betriebsart Deckellinie - Hand
void but3_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, false);
    IO.SetInput(85, false);
    IO.SetInput(86, true);
    IO.SetInput(87, false); 
}

//Button Betriebsart Deckellinie - Automatik
void but4_OnPress(object sender, EventArgs e)
{
IO.SetInput(84, false);
    IO.SetInput(85, false);
    IO.SetInput(86, false);
    IO.SetInput(87, true);
}

public void Main(GameTime gameTime)
{
//Anzeige Betriebsart Deckellinie - Aus
if(IO.GetInput(84))
{
   but1.color = Color.Red;
}
else
{
  but1.color = Color.Gray;
}
}

public void Draw(GameTime gameTime)
{
}

public void Finish()
{
gui.RemoveAllForms();
MachSim.Components.Remove(gui);
}

PKirsch

Ok that's easier than I thought :-) Thank you very much for that.
I now have one more question for this topic.
Is there a list of all usable Events as "onPress" for the buttons etc?
Specifically I want to set an output only as long as the button is pressed.
So I think I need one event for set the output when the button is pressed and another event for reset the output when the button is released.
I tried "OnReleased" or something like that but I'm stuck.

EasyPLC_Master

There's not an event for the button release!  :'(
However you can program it,  ;)
Here an example:


GUI gui;
float pos = 0;
TextButton but1;

bool Butt_1_pressed = false;
bool Butt_2_pressed = false;
bool Butt_3_pressed = false;
bool Butt_4_pressed = false;

public void Init()
{
gui = new GUI(MachSim);
gui.Load(Editor.Path() + "\\MyMachines\\Bedienstationen_AFL4.frm");
Form form = gui.forms[0];
form.Position = new Vector2(ScreenW - form.size.X, ScreenH - form.size.Y);
Label lab1 = (Label)form.Controls[0];
Label lab2 = (Label)form.Controls[1];
Label lab3 = (Label)form.Controls[2];
Label lab4 = (Label)form.Controls[3];

but1 = (TextButton)form.Controls[4];
but1.OnPress += new EventHandler(but1_OnPress);
TextButton but2 = (TextButton)form.Controls[5];
but2.OnPress += new EventHandler(but2_OnPress);
TextButton but3 = (TextButton)form.Controls[6];
but3.OnPress += new EventHandler(but3_OnPress);
TextButton but4 = (TextButton)form.Controls[7];
but4.OnPress += new EventHandler(but4_OnPress);
MachSim.Components.Add(gui);
}

//Button Betriebsart Deckellinie - Aus
void but1_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, true);
    IO.SetInput(85, false);
    IO.SetInput(86, false);
    IO.SetInput(87, false);
    Butt_1_pressed = true;   
}

//Button Betriebsart Deckellinie - Einrichten
void but2_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, false);
    IO.SetInput(85, true);
    IO.SetInput(86, false);
    IO.SetInput(87, false);
    Butt_2_pressed = true;   
}

//Button Betriebsart Deckellinie - Hand
void but3_OnPress(object sender, EventArgs e)
{
    IO.SetInput(84, false);
    IO.SetInput(85, false);
    IO.SetInput(86, true);
    IO.SetInput(87, false); 
    Butt_3_pressed = true;
}

//Button Betriebsart Deckellinie - Automatik
void but4_OnPress(object sender, EventArgs e)
{
IO.SetInput(84, false);
    IO.SetInput(85, false);
    IO.SetInput(86, false);
    IO.SetInput(87, true);
    Butt_4_pressed = true;
}

public void Main(GameTime gameTime)
{
//Anzeige Betriebsart Deckellinie - Aus
if(IO.GetOutput(84))
{
   but1.color = Color.Red;
}
else
{
  but1.color = Color.Gray;
}

//check for the buttons release event
if(input.MouseState.LeftButton == ButtonState.Released)
{
if(Butt_1_pressed)
{
Butt_1_pressed = false;
// write here the event for the button 1 release event
IO.SetInput(84, false); //for example...
}
else if(Butt_2_pressed)
{
Butt_2_pressed = false;
// write here the event for the button 2 release event
}
else if(Butt_3_pressed)
{
Butt_3_pressed = false;
// write here the event for the button 3 release event
}
else if(Butt_4_pressed)
{
Butt_4_pressed = false;
// write here the event for the button 4 release event
}
}

}

public void Draw(GameTime gameTime)
{
}

public void Finish()
{
gui.RemoveAllForms();
MachSim.Components.Remove(gui);
}