I created a simple code here just to play around with C#. It has 3 buttons and 1 panel. If you click on the 2nd & 3rd button the panel height changes. Is that also possible to change the color?
For example, if I click on the 2nd button, I would like to have it as yellow and at the same time the height changes as well, and the same with 3rd button.
public partial class Form1 : Form
{
public int heightPanel;
public Form1()
{
InitializeComponent();
heightPanel = panel1.Height;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Height = heightPanel;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
}
}
I have an idea but I don't know where to put this. I think it would be something like this:
panel1.Height=this.BackColor.ToString();
Any inputs?
Response to reply
Yes, I would like to retain the 3 colors if I click in any of the button. I'm not sure if it's possible. For ex:
my button1 =pink
button2=yellow
button3=green
If I click on the button 1 I'll see the pink color, and if I click on the button2 I'll see the pink and yellow.
Is that possible?
All you'd need to do would be to set the BackColor on a new line. For instance...
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
panel1.BackColor = Color.Yellow;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
panel1.BackColor = Color.Yellow;
}
Simple as:
panel1.BackColor = Color.Red;
In button2_Click, just add another line like this:
panel1.BackColor = Color.Yellow;
Related
private void Form1_Load(object sender, EventArgs e)
{
if (MouseMove == button1;)
{
button1.Size= 100;70;
}
}
}
}
I couldn't find how to write code
Use the MouseEnter event to capture the mouse cursor hovering over the button borders,
and the MouseLeave event to detect when the cursor leaves the button borders, in order to return it to its original size.
Here is one way to implement such functionality:
private Size OriginalButtonSize;
private Size HoveredSize;
private void Form1_Load(object sender, EventArgs e)
{
OriginalButtonSize = this.button1.Size;
HoveredSize = new Size(OriginalButtonSize.Width + 30, OriginalButtonSize.Height + 30);
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Size = HoveredSize;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Size = OriginalButtonSize;
}
Output:
You can use this code:
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Width = 100;
button1.Height = 70;
}
if you want to set size to the previous size after leaving the mouse, you can use this (for example it was 60 and 30 at first place):
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Width = 60;
button1.Height = 30;
}
I'm adding a PictureBox to a Form at runtime in C# and it does work (I think). Only problem is I can't change the properties of the PictureBox from outside nor from inside the method, eg. I can't even change the BackColor.
This is my attempt:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox canvas = new PictureBox();
canvas.Dock = DockStyle.Fill;
canvas.BackColor = Color.Red;
}
The PictureBox should now fill the entire Form and have a red background but it doesn't work.
Also, how do I add the PictureBox "publicly" so I can change the properties from another method?
I tried it like this:
PictureBox canvas = new PictureBox
{
Dock = DockStyle.Fill,
BackColor = Color.Red
};
private void button1_Click(object sender, EventArgs e)
{
canvas.BackColor = Color.Red;
} // Now I can use "canvas" in other methods without any errors but still nothing happens
That's good start:
PictureBox canvas = new PictureBox
{
Dock = DockStyle.Fill,
BackColor = Color.Red
};
private void button1_Click(object sender, EventArgs e)
{
canvas.BackColor = Color.Red;
}
But you have to add that box to your form:
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(canvas);
}
Once it's working as expected, you should call canvas field like that: _canvas as this is private field of this class (form).
When we pass the mouse over a button we can change the button back color using MouseOverBackColor and MouseDownBackColor using FlatApearance property box.
How can I change a button text color in the same mode when the mouse pass over it?
This should work for all sorts of Buttons, with or without FlatAppearance:
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.ForeColor = SystemColors.ControlText;
}
By using RGB values, this can look like this:
//Hover Text Color changing
private void btnHome_MouseHover(object sender, EventArgs e)
{
btnHome.ForeColor = System.Drawing.Color.FromArgb(1, 102, 207);
}
private void btnHome_MouseLeave(object sender, EventArgs e)
{
btnHome.ForeColor = System.Drawing.Color.LightGray;
}
donĀ“t forget to do everytime a new method call for each button!
Just follow this figure to come to a nice solution:
If you only want to change button text color when mouse is over the button:
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = System.Drawing.Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.ForeColor = System.Drawing.Color.Black;
}
I have an PictureBox that I want to move up on the y axis after a button click. The problem is that the PictureBox simply appears there after the button click. I want it to move to the new position, not teleport. What do I do?
public partial class Form1 : Form
{
Point stageplus1 = new Point(164, 325);
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox5.Location = stageplus1;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
}
}
Expanding on BJ Myers comment this is how you can implement that:
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
// calculate new position
this.pictureBox1.Top++;
this.pictureBox1.Left++;
// when to stop
if (this.pictureBox1.Top > (this.Height - (2 * this.pictureBox1.Height)))
{
this.timer1.Enabled = false;
}
}
I used the default Timer control that raises a Tick at an interval. The Tick event gets executed on the UI thread so you don't have to be bothered by cross thread errors.
If added to your form this is what you'll get:
If you need to animate more stuff you might want to look into using a Background worker and helper classes like I show in this answer of mine
I am trying to apply a color to a hovered button like this:
private void button_MouseEnter(object sender, EventArgs e)
{
button.UseVisualStyleBackColor = false;
button.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
}
private void button_MouseLeave(object sender, EventArgs e)
{
button.UseVisualStyleBackColor = true;
button.BackColor = System.Drawing.SystemColors.ControlLight;
}
My Problem is there seems to be some kind of a default hover effect. The color is much darker than it should be. At the moment I do not know where to start to get rid of it.