I tried to change the background of the buttons when a button is clicked it doesn't color the background of the button.
My attempt:
private void Ans1_Click(object sender, RoutedEventArgs e)
{
//green the correct answer
Ans1.Background = bc.ConvertFromString("#FF3C9C27") as SolidColorBrush;
//rest all red
Ans2.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Ans3.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Ans4.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Thread.Sleep(1500);
}
private void Ans1_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button; <-- sender is the current button
button.Background = bc.ConvertFromString("#FF3C9C27") as SolidColorBrush;
}
It seems that your conversion is the problem.
bc.ConvertFromString("#FF3C9C27") most likely returns a System.Windows.Media.Color.
System.Windows.Media.Color as SolidColorBrush returns null however.
This should give you the desired result:
private void Ans1_Click(object sender, RoutedEventArgs e)
{
//green the correct answer
Ans1.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FF3C9C27"));
//rest all red
Ans2.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
Ans3.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
Ans4.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
}
About the Thread.Sleep "issue": you could use a Timer instead.
To change the background color of your button, inside your clicked function that's been auto-generated add the following code:
Ans1.BackColor = Color.Green;
Ans2.BackColor = Color.Red;
Ans3.BackColor = Color.Red;
Ans4.BackColor = Color.Red;
Hope this helps!
Related
So I have this button that launches another program. I disable it after launch to prevent double launch situations. Problem is the ForeColor changes from white to black which doesnt look good against my dark BackColor. How do I make the ForeColor not change when disabling or even change it after disable?
You need to use EnabledChanged and paint events of the current button. Suppose your button name is button1.
private void button1_EnabledChanged(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
button1.ForeColor = currentButton.Enabled== false ? Color.White : currentButton.ForeColor ;
button1.BackColor = currentButton.Enabled == false? Color.Black : currentButton.BackColor;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
}
private void button1_Paint(object sender, PaintEventArgs e)
{
Button btn = (Button)sender;
var solidBrush = new SolidBrush(btn.ForeColor);
var stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
e.Graphics.DrawString(button1.Text, btn.Font, solidBrush, e.ClipRectangle, stringFormat);
solidBrush.Dispose();
stringFormat.Dispose();
}
I have bunch of buttons on my form. And I would like to make it a bit nicer so button changing color and font to bold when mousei s over it seems like good idea. I would appreciate any help
button.BackColor = Color.Cyan;
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);
EDIT:
this is working for me:
private void button1_MouseEnter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.PaleTurquoise;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Bold);
}
private void button1_MouseLeave(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.WhiteSmoke;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Regular);
}
(there is button1_mousenter (or mouseleave) set as action for every button
Just select them all in your Form view and go to the mousehover event.
and write your code like this:
private void button_mousehover (object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Cyan;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, ((Button)sender).FontStyle.Bold;
}
You can add MouseEnter and MouseLeave events to your buttons that changes the buttons' colors.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseenter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseleave(v=vs.110).aspx
// bind handler to MouseEnter Event
this.yourButton1.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);
this.yourButton2.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);
// bind handler to MouseLeave Event
this.yourButton1.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);
this.yourButton2.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);
// enter handler
private void allButtons_MouseEnter(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = Color.Cyan;
btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Bold);
}
// leave handler
private void allButtons_MouseLeave(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = Color.DeepPink; // whatever your original color was
btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Regular);
}
in XAML i created button and when i click on the button then background will be changed to red color(because i set it) but if i close my application and then again start application so background is not red. What i need to do is keep background there like before(when i clicked on button) so have red background if i again start application. Your help will be for me very important. Thank you experts. Btw this is code for button now: :).
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
background.Background = new SolidColorBrush(Colors.Red);
}
you need to save the selected color; or save somwething some you can determine what color it has to be. In the constructor or your page read this value and set the right color.
you can use localsettings to save this.
ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red";
for example.
In the constructor:
if ( ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red")
{
background.Background = new SolidColorBrush(Colors.Red);
}
full sample:
public MainPage()
{
this.InitializeComponent();
if ((string)ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red")
LayoutRoot.Background = new SolidColorBrush(Colors.Red);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red";
LayoutRoot.Background = new SolidColorBrush(Colors.Red);
}
private void arrButton_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (turn == 0)
{
button.ForeColor = Color.Green; // Can't change color
button.Text = "X";
button.Enabled = false;
turn = 1;
}
else
{
button.Text = "O";
button.Enabled = false;
turn = 0;
}
}
I used button.ForeColor = new Color.Green but when I test X still can't change green color.
How to change color text in button ?
Disabled component doesn't effect any graphical changes. It must be enabled to reflect the Color change.
You should use any other condition to check disabled button if you want to keep the graphical changes.
For Example:
if(button.ForeColor == Color.Green)
//handle the click event
for wpf:
private void arrButton_Click(object sender, RoutedEventArgs e)
{
button.Foreground= Brushes.Blue;
}
for Winform:
private void arrButton_Click(object sender, EventArgs e)
{
button.BackColor = Color.Red;
}
I have a WPF project where i have to create a few ellipses on a canvas. I create two check Box and when i check on the first checkbox, the red ellipse will show on the canvas. If i uncheck the first check box, the ellipse will be disappear....the second check box will have the same function by creating a blue ellipse instead.
So heres my situation, when the two checkboxes are checked, a blue and a red ellipse will appear. To clear the ellipse on the canvas, i use the myCanvas.children.clear(). But when i uncheck one of the checkbox, both of the ellipse will be deleted.
private void redCB_Checked(object sender, RoutedEventArgs e)
{
drawRedCircle();
}
private void redCB_Unchecked(object sender, RoutedEventArgs e)
{
myCanvas.Children.Clear();
}
private void blueCB_Checked(object sender, RoutedEventArgs e)
{
drawBlueCircle();
}
private void blueCB_Unchecked(object sender, RoutedEventArgs e)
{
myCanvas.Children.Clear();
}
private void drawRedCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Red;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
Canvas.SetLeft(myCircle, 10);
Canvas.SetRight(myCircle, 10);
Canvas.SetBottom(myCircle, 10);
Canvas.SetTop(myCircle, 10);
myCanvas.Children.Add(myCircle);
}
private void drawBlueCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Blue;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
Canvas.SetLeft(myCircle, 20);
Canvas.SetRight(myCircle, 20);
Canvas.SetBottom(myCircle, 20);
Canvas.SetTop(myCircle, 20);
myCanvas.Children.Add(myCircle);
}
If you give the added circle a name, you can find it when the checkbox is unchecked and then remove it pretty easily.
private string redCircleName = "redCircle";
private string blueCircleName = "blueCircle";
private void redCB_Checked(object sender, RoutedEventArgs e)
{
drawRedCircle();
}
private void redCB_Unchecked(object sender, RoutedEventArgs e)
{
RemoveCircleByName(redCircleName);
}
private void blueCB_Checked(object sender, RoutedEventArgs e)
{
drawBlueCircle();
}
private void blueCB_Unchecked(object sender, RoutedEventArgs e)
{
RemoveCircleByName(blueCircleName);
}
private void RemoveCircleByName(string name)
{
var circle = (UIElement)LogicalTreeHelper.FindLogicalNode(myCanvas, name);
myCanvas.Children.Remove(circle);
}
private void drawRedCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Red;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
//Give it a name here so we can find it later
myCircle.Name = redCircleName;
Canvas.SetLeft(myCircle, 10);
Canvas.SetRight(myCircle, 10);
Canvas.SetBottom(myCircle, 10);
Canvas.SetTop(myCircle, 10);
myCanvas.Children.Add(myCircle);
}
private void drawBlueCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Blue;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
//Give it a name here so we can find it later
myCircle.Name = blueCircleName;
Canvas.SetLeft(myCircle, 20);
Canvas.SetRight(myCircle, 20);
Canvas.SetBottom(myCircle, 20);
Canvas.SetTop(myCircle, 20);
myCanvas.Children.Add(myCircle);
}
I think unchecked event calls later, which means that the circle created by checked event is cleared by the unchecked event.
One solution can be to move the logic of creating and clearing circles to a single method, and only registered checked events.
private void drawCircle()
{
myCanvas.Children.Clear();
if(redCB.Checked) drawRedCircle();
if(blueCB.Checked) drawBlueCircle();
}
private void redCB_Checked(object sender, RoutedEventArgs e)
{
drawCircle();
}
private void blueCB_Checked(object sender, RoutedEventArgs e)
{
drawCircle();
}
Rather then add/remove why not using the visibility ( IsVisible) of the ellipse ? Bind them to a notifying boolean property, and you bind also the checkBox to that property.
// ellipse1Visible is a notifying boolean
Binding ellipseBinding = new Binding("ellipse1Visible");
ellipseBinding .Source = ??? ; // set your binding source here (? this ?)
myEllipse.SetBinding(Ellipse.IsVisibleProperty, ellipseBinding );
myRelatedCheckBox.SetBinding(CheckBox.IsCheckedProperty, ellipseBinding)
if you want to use any number of ellipse, you have to use
an Array/List/ObservableColection of 'notifying booleans'
the syntax is :
// i is a valid index in the ellipseVisible collection of notifying boolean.
Binding ellipseBinding = new Binding("ellipseVisible[" & i & "]");
// the rest of the code is the same
you can add the checkbox in a near StackPanel/listBox/... while you add the related ellipse in the canvas.
No need to add/remove ellipse and no need to handle checked/unchecked with this solution.
Rq : if you intend to make no use of the visibility of each ellipse, you can even do it in a simpler way : no boolean, but rather you directly bind the visibility to the IsChecked property of the CheckBox :
// ... we just created myEllipse and myCheckBox
// ... and inserted them into canvas/stackPanel
Binding ellipseBinding = new Binding("IsChecked");
ellipseBinding.Source = myCheckBox;
myEllipse.SetBinding(Ellipse.IsVisibleProperty, ellipseBinding);
// and that's all