I have a rather simple WCF program in which a user can click two buttons. 'ChangeBackGroundColour' & 'ChangeButtonColour'.
The WCF service is returning a random colour from a list of colours, and that string is dictating the colour of the two objects via switch case statement.
However, the colour of the objects cannot be the same. How would one stop a duplicate?
Heres my code:
Front_End
public partial class Front_End : Form
{
RandomColourServiceReference.RandomColoursServiceClient ws = null;
string BackGroundColour { get; set; }
string TextColour { get; set; }
public Front_End()
{
InitializeComponent();
}
private void Front_End_Load(object sender, EventArgs e)
{
ws = new RandomColourServiceReference.RandomColoursServiceClient();
}
private void BtnChangeBack_Click(object sender, EventArgs e)
{
BackGroundColour = ws.GenerateRandomColour();
switch (BackGroundColour)
{
case "Red":
this.BackColor = Color.Red;
break;
case "Blue":
this.BackColor = Color.Blue;
break;
case "Black":
this.BackColor = Color.Black;
break;
case "Purple":
this.BackColor = Color.Purple;
break;
case "Green":
this.BackColor = Color.Green;
break;
}
}
private void BtnChangeButton_Click(object sender, EventArgs e)
{
TextColour = ws.GenerateRandomColour();
switch (TextColour)
{
case "Red":
btnChangeButton.BackColor = Color.Red;
btnChangeBack.BackColor = Color.Red;
break;
case "Blue":
btnChangeButton.BackColor = Color.Blue;
btnChangeBack.BackColor = Color.Blue;
break;
case "Black":
btnChangeButton.BackColor = Color.Black;
btnChangeBack.BackColor = Color.Black;
break;
case "Purple":
btnChangeButton.BackColor = Color.Purple;
btnChangeBack.BackColor = Color.Purple;
break;
case "Green":
btnChangeButton.BackColor = Color.Green;
btnChangeBack.BackColor = Color.Green;
break;
}
}
}
}
RandomColourService
public class RandomColoursService : IRandomColoursService
{
public string GenerateRandomColour()
{
//Declare and initialize a list of string colours
List<String> colours = new List<String>();
colours.AddRange(new String[]{ "Red", "Blue", "Green", "Pink", "Purple", "Black"});
//create new instance of random
Random rand = new Random();
//return a random colour in array
return colours[rand.Next(0, colours.Count)];
}
}
IRandomColourService
[ServiceContract]
public interface IRandomColoursService
{
[OperationContract]
string GenerateRandomColour();
}
I have a feeling a while loop should be involved but I'm not quite sure.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire