I have three buttons. Once I click on one it will change its border color to different color and show its active, but to set it default i have to click on the button again. All of these three buttons represent different action and they cant be running simutionasly for the user. Do you know what to do to set back to default border color of button 1 if button 2 is clicked?
Created my byttons in xamarin and this is the code behind.
I tried to create a separated method for that but i cant locate the buttons.
protected void EnglishToCzech_Clicked(object sender, EventArgs e)
{
Button englishtoczech = (Button)sender;
if (englishtoczech.BorderColor.Equals(Color.Default))
english.BorderColor = Color.FromHex("#da2c43");
else
englishtoczech.BorderColor = Color.Default;
}
protected void CzechToEnglish_Clicked(object sender, EventArgs e)
{
Button czechtoenglish = (Button)sender;
if (czechtoenglish.BorderColor.Equals(Color.Default))
english.BorderColor = Color.FromHex("#da2c43");
else
czechtoenglish.BorderColor = Color.Default;
}
protected void English_Clicked(object sender, EventArgs e)
{
Button english = (Button)sender;
if (english.BorderColor.Equals(Color.Default))
english.BorderColor = Color.FromHex("#da2c43");
else
english.BorderColor = Color.Default;
}
Ok if I understand you correctly, you can reset the color of all in each click. First you need to assign names to buttons. If you use XAML add x:Name
attribute and give them names. I suppose you name them englishtoczech
, czechtoenglish
and english
but you can name them anything. The convention is camel case. Then you can use them in your code behind like this:
protected void EnglishToCzech_Clicked(object sender, EventArgs e)
{
if (englishtoczech.BorderColor.Equals(Color.Default))
{
englishtoczech.BorderColor = Color.FromHex("#da2c43");
czechtoenglish.BorderColor = Color.Default;
english.BorderColor = Color.Default;
}
else
englishtoczech.BorderColor = Color.Default;
}
protected void CzechToEnglish_Clicked(object sender, EventArgs e)
{
if (czechtoenglish.BorderColor.Equals(Color.Default))
{
czechtoenglish.BorderColor = Color.FromHex("#da2c43");
englishtoczech.BorderColor = Color.Default;
english.BorderColor = Color.Default;
}
else
czechtoenglish.BorderColor = Color.Default;
}
protected void English_Clicked(object sender, EventArgs e)
{
if (english.BorderColor.Equals(Color.Default))
{
english.BorderColor = Color.FromHex("#da2c43");
englishtoczech.BorderColor = Color.Default;
czechtoenglish.BorderColor = Color.Default;
}
else
english.BorderColor = Color.Default;
}
This is a pretty good simple approach but, two things you should consider:
You shouldn't build your logic over button border colors and it is good to have some variable holding the state of system.
In more advanced scenarios if you are a professional I strongly recommend that you read MVVM and try that in a Xamarin project. That makes your life a lot easier down the path
Let me elaborate more on the first point.
You will set the border colors here but nothing happens. I think later on another event you will check the border color of buttons and do some logic with them.
This way you have built your logic over border colors. You can create an enum
public enum State
{
None,
English,
EnglishToCzech,
CzechToEnglish
}
And then in your code bihind add a variable of that enum
private State state = State.None;
And finally set the state in each button click accordingly.
This is just an example I hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.