Why are my disabled buttons changing color? - c#

My problem is: When my application is opened, I disable all buttons (in form_Load) and their color changes to the color of the background. But then I do some action (like clicking on a button), and in this action I disable the buttons again.
Now some of these buttons become GRAY and others become as the background.
Why is this? I don't want the the gray effect. Normally when I disable the button at the beginning of the application, it becomes the color I expect, but when I try to disable them again this strange behavior appeared. What to do?
My code is like:
private void _btnDownload2PC_Click(object sender, EventArgs e)
{
// do action
_btnDownloadToPC.Enabled=false; // its color became gray
_btnDownloadToPhone.Enabled=false; // its color became like the
// background color and it can't
// be pressed
}
I figured out that the problem is when I use the button_MouseLeave() or button_MouseMove() functions. For example:
private void _btnOneToCort_MouseLeave(object sender, EventArgs e)
{
this._btnOneToCart.Image=global::MyProject.Properties.Resources.button3over;
}
but this doesn't make sense. Why does this function change my button settings (I don't know what these are) When I use these function this strange behavior appears, but when I don't, everything goes right?

You can programmatically access the color of a button. Set a breakpoint and do that to see if they really are changing.
My guess: the image that you are setting has a gray background that is different than the standard disabled color. You will need to edit the image and remove the background.

Why don't disable buttons in designer? If not acceptable, do that in form constructor not Form_Load.
Also, how can you click on button if it's disabled?
Are form color settings default? Have you changed windows theme color preferences?

Related

How to detect a click on a component that is not visible

Is it possible to detect a click on a picturebox that is not visible
I have tried this :
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("teste");
}
But even if the picture box has no image and that background color is transparent, it hides the elements behind it.
I explain myself a little better;
I do a tic-tac-toe and I have the image of the cross that is not visible when the game is started; as soon as I press on a delimited area (where I want to place the cross), I want it to become visible
How can I do that?
Thank you.
I would use the PictureBox with an ImageList containing 2 images: the first is the background of your game field and the second is the cross.
In your Click Event I would toggle the pictures on click then.

Undo the border i created with ControlPaint.DrawBorder()

I am working on WindowsForm C#.
The border i create becomes problem when maximized!
private void App_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Crimson, ButtonBorderStyle.Solid);
}
Normal:
Maximized:
Am i doing something wrong?
Is there a better way of doing this?
DrawBorder() has a parameter that can be used to remove a previously drawn border, just set ButtonBorderStyle to None
Example:
ControlPaint.DrawBorder(e.Graphics, (sender as Control).ClientRectangle, Color.Red, ButtonBorderStyle.None);
The only thing I don't like as much of my approach, is that it requires calls to the Paint event, which can get slow.
See this to refresh the form at runtime
Based on the information provided, I see it twice, meaning the painted information is being retained (relative to the position of the form) from the previous instance.
I'd strongly recommend staying away from trying to paint the non-client area.
If you have a custom request such as this, create a form with FormBorderStyle.None. Then, have a custom event to paint the Split Container's border. The top panel will have the custom min/max buttons. The rest of the content should be in the lower panel that fills below the min/max button.
Stay away from non-client painting. It's more trouble than it's worth.
I did it my self :D
1) Create a button, turn its FlatStyle to flat.
2) Turn Enable property to false, also remove all text
3) Dock the button to fill.
4) If previous button,labels(control) got hidden because of button, right click the button , send it to back!

How to create fade button effect with pictureboxes

I'm trying to create an effect so when the user hovers the mouse over a picturebox, the button fades to a hoverimage, and when they leave, it fades back to the original. I'm using pictureboxes as buttons in a program. I'm doing this because all the buttons will be pictures with no button textures, so I didn't see the point in using a button. Just so you can visualize it, here is the original image:
And the image to fade to:
I could still change these images a bit, but thats the general idea.
How would I go about creating this fading effect? I'm picturing something using timers and opacity settings, but I don't know how any of that stuff could help me solve this.
E: Heres a bit of code I have. It changes from image to image when I hover, but its not a fade, and it looks very choppy.
private void pictureBox3_MouseEnter(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox37.Image;
}
private void pictureBox3_MouseLeave(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox38.Image;
}
pictureBox37 and pictureBox38 are invisible reference pictureboxes with the images I need.
I don't think there's any WinForms support for this kind of animation, so you'd have to do this manually. If you have more than one of these buttons, I recommend creating your own UserControl with this functionality. I can give you some pointers how to do this:
Your UserControl will have two Image properties, one for the normal and one for the hover image
You'll have to override the OnPaint method to do your own custom painting, blending these two images onto your Graphics according to a position property. This is a float where 0 indicates showing the normal image and 1 the hover image. Any value in between means a blend of these two images. I found some good blending code here.
Then you'll need a Timer to update this position dynamically and redraw the button (by calling Invalidate). I recommend using a Windows.Forms.Timer, which has the advantage that its Tick event is always executed on the main Thread, so you don't have to use Invoke to modify your control.
And last but not least you override the OnMouseEnter and OnMouseLeave methods, to set this all into motion. These methods could set a positionChange property that indicates in which direction position is changing.
Hope this helps ...

in C# windows apps, the Transparency in my textbox made it as if it wasn't there

hey guys I used this code to enable Transparency to my form:
private void TypingFreak_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.TransparencyKey = Color.FromKnownColor(KnownColor.Control);
this.Update();
}
BUT, it became as if wasn't there, so if I'm running it on my desktop, I could see the icons and react with them (I can't write to the textbox)
in my form, I have 2 rich textBoxes that I want them to be transparent to the backcolor of my form background ..
any idea how to fix this ?
TransparencyKey makes the form behave as if it wasn't there in places with that colour. In your case it's the background colour of all content controls (such as TextBox, ListBox, etc.) and thus almost the whole form is invisible. Or probably if your form has a different color then you get holes in places where you put such a control.
You would need to make the control itself transparent by setting a transparent background colour, but not all controls support this. In fact, RichTextBox (and TextBox) does not:
    
leads to
    

WPF control hosted in an ElementHost control changes colour

I'm creating a WPF 'Button' control and making it the child of my ElementHost control.
The background of the button is set to Red.
When I run my project, the button changes colour and it seems to loop through every couple of seconds going from Red to a light blue and back again.. until the Form loses focus.
If I hover over the button, it turns blue, then when I move off the button it starts this colour looping again.. my code is as simple as..
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Controls.Button but = new System.Windows.Controls.Button();
but.Background = System.Windows.Media.Brushes.Red;
elementHost1.Child = but;
}
}
Is this normal ? can I turn it off?
Yes, it's normal. WPF buttons always do a subtle color cycling effect when they have input focus, and that's the case for your button since it's the only control on the form. It's not very subtle in your example because Red is a long way from the other blueish color the button cycles through -- delete that Background assignment to see the effect as intended. Try adding another ElementHost with another WPF Button to the same form, and you'll see that only the focused button does the color cycling.
As for how to turn it off... I don't know but I'm afraid it's not easy. I don't see any simple property on Button that would change this effect. Such effects are generally achieved by WPF style templates which is a subject that makes grown men cry. You can find the MSDN overview below, but note that this assumes you're working within WPF and XAML, not within Windows Forms:
http://msdn.microsoft.com/en-us/library/bb613570.aspx
My guess is that you would have to associate a changed Focused style with your button that won't do the color cycling, or else discover the resource name of the second brush (other than Background) that the Focused style cycles to, and set that resource to the same color as Background.

Categories