Changing color to status bar - c#

Ok I am still learning this...
I have Googled and done some research but did not find what I am looking for.
I'm trying to Add a status bar, which displays the current color in the background. This should update regardless of how the user changed the background color (Context menu, Menu bar, Buttons).
I already added the status bar, but I am not too sure on how to get it to display when you click on the button with the color or when you use the context menu. So I have to go in each one of those code to change or make a public class with the status bar.
Below you can find an image of what I intend to do:
If you look on the bottom it say red background and it is also highlighted to red. When I click on the blue or the green I need it to change over also.
What would be the best way to accomplish this?
This is my code from the toolstrip:
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Red;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Green;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Blue;
}
This code is from my context menu:
private void redToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Red;
}
private void greenToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Green;
}
private void blueToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Blue;
}
I am trying to figure out the status bar right now.
Edit I figure out what I had to do. I needed to go back on my form page and edit the toolstrip. Thanks for the help guys and girls.

Make all the buttons use the same event. And then check the sender. If you assign greenButton, blueButton etc tags to the buttons then you can use this:
private void ColorButtons_Click(object sender, EventArgs e)
{
Control item = (Control)sender;
if(item.tag == "greenColor")
this.BackColor = Color.Green;
else if(item.tag == "blueColor")
this.BackColor = Color.Blue;
// and so on
}

The best solution is to react to the background color change of your main form. You can do this either by handling BackColorChanged event or overriding OnBackColorChanged protected method in your main form class.

You can use a method to change the color, and in this method, set the text and/or color of your status bar.
So instead of setting (for instance)
this.BackColor = System.Drawing.Color.Blue;
you'd call
this.SetColor(System.Drawing.Color.Blue);
and in SetColor(Color), you handle the text change.
For instance, you could use this method :
private void SetColor(System.Drawing.Color c)
{
this.BackColor = c;
this.toolStripStatusLabel1.Text = c.Name;
}

Altough you are not using a good method rigth now (other answers have better methods), you can easily fix it. Since you want to change the background color of a control inside the status bar, you can do it in several places:
You can have a function that change that receive a color and change the control according to that. In your example, the label on the strip bar is red, and you already change it on designer. Well, at runtime you can do the same, using code ;). the label has a name. just add a function something like this:
private void ChangeColor(mycolor as System.Drawing.Color)
{
yourlabelcontrol.BackColor = mycolor;
yourlabelcontrol.Text = mycolor.Name;
}
You can also execute this on every action that change a color.
You can have one function that change the color of everything (like this one, but change the color for everthing)

Related

How to correctly highlight buttons on click?

So I have built an application in C# using Winforms and my application uses a few different buttons. I'd like to have a highlight on the button that has been clicked to show what 'tab' you're in.
I've tried doing the following;
// BUTTONS //
private void dashboard_btn_Click(object sender, EventArgs e)
{
// Load Form
OpenChildForm(new FormDashboard());
dashboard_btn.FlatAppearance.BorderColor = Color.Red;
dashboard_btn.FlatAppearance.BorderSize = 1;
}
However, this of course doesn't work nicely since it adds a border around the button but when I click another button the border also stays around the previous button.
How would you implement a feature to add a border around the button that get's clicked but have the border disappear after you click another button?
Thank you for any feedback!
EDIT:
I've implemented Jimi's advice and used the Leave event to change the border around the button back to 0. However I'm not sure how to implement this in a global way so all my buttons are subscribed to this event.
My code now looks like this;
// BUTTONS //
private void dashboard_btn_Click(object sender, EventArgs e)
{
// Load Form
OpenChildForm(new FormDashboard());
// Button Highlight
dashboard_btn.FlatAppearance.BorderColor = Color.Red;
dashboard_btn.FlatAppearance.BorderSize = 1;
}
// BUTTON REMOVE HIGHLIGHT //
private void dashboard_btn_Leave(object sender, EventArgs e)
{
dashboard_btn.FlatAppearance.BorderSize = 0;
}
EDIT 2:
I ended up using Jimi's example and this worked for me :)
This might lend itself to a RadioButton style functionality because clicking a different radio button in the same container will uncheck the others. So, to implement the "generalized approach" that you mention in your comment, you could make a simple custom RadioButtonEx class where the Appearance property is set to Button then change your border style when the Checked property changes. In this example, the Click event has been changed to static so that clicking on any button directs the event to the common onAnyClick method.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
RadioButtonEx.Click += onAnyClick;
}
private void onAnyClick(object sender, EventArgs e)
{
label1.Text = ((RadioButtonEx)sender).Text;
}
}
public class RadioButtonEx : RadioButton
{
public static new event EventHandler Click;
public RadioButtonEx()
{
FlatAppearance.BorderColor = Color.Red;
FlatAppearance.BorderSize = 1;
Appearance = Appearance.Button;
}
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if(Checked)
{
FlatStyle = FlatStyle.Flat;
Click?.Invoke(this, EventArgs.Empty);
}
else
{
FlatStyle = FlatStyle.Standard;
}
}
}

C# Winforms Application Button Event for Method in Another Class

My problem is that I have a button in a winforms application that I would like to create a hover effect for. I have already achieved this effect by selecting this button, moving to the properties pane, selecting the Events, and double clicking the MouseEnter Event. This automatically creates a method in the c# code, and I can go into that method change the background color, no problem. I use a similar process for the MouseLeave Event and change the color back. This I know how to do.
Since each user control also has many buttons on it, I've been able to create generic methods (like below), and reuse these methods for each button within the user control. This is easy to select a method with the appropriate signature from the properties under events. I can find it in the dropdown next to the method. However, I have many user controls which have buttons on them, so these 2 methods are repeated in every single class.
What I've done to attempt to clean this up, is create a little static class. Then I can go into the designer (after building) and this works 100%. This not only reduces redundant code but also allows for a style change if ever the hover color needed to change, without modifying each code behind. However, if I were ever to make a modification the the user control, my reference to the method in the designer gets wiped out by the designer. The line of code just disappears.
Everything I read says "don't change the designer" for this exact reason. However, the visual studio user interface does not allow me a way to reference a method in an outside class. Any advice would be much appreciated.
private void Mouse_Enter(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Enabled == true) btn.BackColor = Color.LightGray;
}
private void Mouse_Leave(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Enabled == true) btn.BackColor = Color.White;
}
public static class ButtonHelper
{
public static void Mouse_Enter(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Enabled == true) btn.BackColor = Color.LightGray;
}
public static void Mouse_Leave(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Enabled == true) btn.BackColor = Color.White;
}
designer
this.btnEdit.UseVisualStyleBackColor = false;
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
this.btnEdit.MouseEnter += new System.EventHandler(ButtonHelper.Mouse_Enter);
this.btnEdit.MouseLeave += new System.EventHandler(this.Mouse_Leave);
Here is the updated code based on the advice by 41686d6564
public class HoverButton:Button
{
protected override void OnMouseEnter(EventArgs e)
{
if (Enabled == true) BackColor = Color.LightGray;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (Enabled == true) BackColor = Color.White;
base.OnMouseLeave(e);
}
protected override void OnEnabledChanged(EventArgs e)
{
if (Enabled)
{
ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(22)))), ((int)(((byte)(137)))));
BackColor = Color.White;
}
else
{
ForeColor = Color.Gray;
BackColor = Color.LightGray;
}
base.OnEnabledChanged(e);
}
}

Dynamically change control color when hovered without changing back when hovering childs

I have a application that talks to the Database-API of a game and displays information about certain items. One part of the UI looks like this:
For the grey boxes I have created a control called 'ItemPreviewControl', which each show information about one specific found Item. I want the control to become red when hovered and return to gray when the mouse leaves the control. For this I have used the MouseEnter- and MouseLeave-Events of the ItemPreviewControl, and as you can see here it also works:
Here's the code I used:
private Color mainColor_MouseEnterLeave;
private void ItemPreviewControl_MouseEnter(object sender, EventArgs e)
{
mainColor_MouseEnterLeave = this.BackColor;
this.BackColor = Color.FromArgb(192, 0, 0);
}
private void ItemPreviewControl_MouseLeave(object sender, EventArgs e)
{
this.BackColor = mainColor_MouseEnterLeave;
}
Now the problem I have is the following: My 'ItemPreviewControl' also changes back to gray when I hovor above one of its child elemenets, so e.g. when I hover above the textboxes inside the control. But I want the control to stay red until I leave the actual control. Do you have any ideas of how to implement this?
The UI is a Windows Forms App. The UI as well as all the Backend-Libraries are in .NET Core 3.1.
Thank you for reading.
Edit: Solution for my problem:
It turns out I had to check whether the Mouse is still inside the UserControl's bounds. If yes, cancel the MouseLeave-Method with a return, and otherwise continue the method. Here's my updated code:
private void ItemPreviewControl_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(192, 0, 0);
}
private void ItemPreviewControl_MouseLeave(object sender, EventArgs e)
{
if (this.ClientRectangle.Contains(this.PointToClient(MousePosition)))
return;
this.BackColor = Color.FromName("Gray");
}

how to can Change the richtextbox default blue highlight color?

I've been looking for the way to change the richtextbox highlight color when a user select text or event mouse move. Windows uses blue as default color.But I want it to be green And blue by default no longer exists.
private void richIndicateText_MouseMove(object sender, MouseEventArgs e)
{
richIndicateText.Select(0, 50);
richIndicateText.SelectionBackColor = Color.Green;
}
this.richIndicateText.MouseClick += new MouseEventHandler(richIndicateText_MouseMove); //hook
this.richIndicateText.MouseClick -= richIndicateText_MouseMove; //unhook
private void richIndicateText_MouseMove(object sender, MouseEventArgs e)
{
richIndicateText.Select(0, 50);
richIndicateText.SelectionBackColor = Color.Green;
}
I will try my best, maybe something like this ?
Unfortunately, the desired behavior is not possible in Windows Forms (details here). The workaround would be to use a WPF RichTextBox in the Windows Form through ElementHost.

Buttons in a Visual Studios form application

Sorry if this is a dumb question, I'm taking an intro to programming class and need a bit of help with this project I'm working on.
I'm trying to write an application that has about 30 buttons. One common thing I want is for all the buttons to turn yellow when clicked. If they're clicked a second time, they change back to the default color. right now I use the code:
private void btn_1_Click(object sender, EventArgs e)
{
btn_1.BackColor = Color.Yellow;
}
But that only turns the buttons yellow, I can't turn them "off" by clicking it a second time.
Also, when I'm creating these button events in VS2010, I end up with 30 different event handlers for each button..Is there a way to get them all to do the same thing without having to write all the repetitive code?
I'm guessing that I would have to write my own buttons class? How would I go about doing that? Do i need to create a class library which inherits Buttons?
Sorry for the noob questions. THanks
If every button has a specific action that needs to be performed, then yes, you need to have a click handler for each; however, you can encapsulate the common behavior in a single method.
For example:
private void btn_1_Click(object sender, EventArgs e)
{
ToggleColor((Button)sender);
//rest of the code specific to this button
}
private void ToggleColor (Button button)
{
if(button.Color==Color.Yellow;
button.Color=Color.Black;
else
button.Color=Color.Yellow;
}
Note that above code is not tested.
Now, if all the buttons do the same thing, you can just set the on click handlers for all of them to be btn_1_Click; for example.
private void btn_1_Click(object sender, EventArgs e)
{
if (btn_1.BackColor != Color.Yellow)
{
btn_1.BackColor = Color.Yellow
}
else
{
btn_1.BackColor = Color.Control;
}
}
this is switching default and yellow
If all buttons do the exact same thing you can assign the same event handler to all buttons (instead of btn_1_Click, btn_2_Click etc... you'd have btton_click) - you can select this handler in the properties of each button.
You don't have to write your own class. You can simply assign all your buttons to the same event handler, like this:
button1.Click += new EventHandler(myEventHandler);
button2.Click += new EventHandler(myEventHandler);
button3.Click += new EventHandler(myEventHandler);
button4.Click += new EventHandler(myEventHandler);
Just keep in mind that your event handler has this signature:
private void myEventHandler(object sender, EventArgs e)
By doing that, all your buttons, when clicked, will trigger the same method.
Now to control the color, what you can do is create a simple property on your form which would hold the last color applied. It could be an enum, then you could simply check its value and apply the other one to the buttons, like this:
// Declare your enum:
private enum Colors { Yellow, Default }
private Colors ActualColor = Colors.Default;
// Write your custom event handler:
private void myEventHandler(object sender, EventArgs e)
{
if (ActualColor == Colors.Default)
{
// Apply yellow to buttons
ActualColor = Colors.Yellow;
}
else
{
// Apply default
ActualColor = Colors.Default;
}
}
In order to keep track whether it is the 'second time' you press the button, you should declare a variable OUTSIDE the method, which indicates whether you already pressed the button or not.
For example:
public bool IsButtonYellow;
private void btn_1_Click(object sender, EventArgs e) {
if(!IsButtonYellow) {
btn.BackColor = Color.Yellow;
IsButtonYellow = true;
}
else {
btn.BackColor = Control.DefaultBackColor;
IsButtonYellow = false;
}
}
Yes:
Create your own button class
Inherit from Button
Implement the handler in your button class and you're done
You can do something simple like this:
public class MyButton : Button
{
private bool _buttonState;
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if (_buttonState)
{
BackColor = Color.Yellow;
}
else
{
BackColor = Color.White;
}
}
}
Then in your code you can just create as many of these "MyButton" objects as you need, with no code repetition.
To make all buttons use the same event handler in VS2010:
Click once on a button to select it.
In the “properties” window: click on the “lightning” (=events).
Paste the first button’s event name (btn_1_Click) next to “Click”.
Do the same for every button.
As for changing the color:
See answer by killie01.
Good luck.

Categories

Resources