c# pictureBox changes size with font - c#

When I change the font of the whole Form with this code:
this.Font = new Font("Gravity", 12, FontStyle.Bold);
Then all my PictureBoxes (with a size of 16x16) are getting bigger, too.
Is there a way to stop this?

If you insist on changing the font of the form, then use
Control.OnFontChanged Method (EventArgs)
This method will be executed every time you change the font of the form, so when it is executed, you can specify that your PictureBox (All) will keep the font you have defined.
This is if all your PictureBox have the same font:
public class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
}
protected override OnFontChanged(EventArgs e)
{
foreach(Control control in this.Controls)
{
if(control is PictureBox)
{
control.Font = new Font(<Your constant defined font>);;
}
}
}
}
Now, assuming your pictureboxes have different fonts.
You can store the sources in a list (or also in an array), and each time the source of your form is altered, you simply call the previously stored sources to be reassigned to each PictureBox:
public class Form1 : Form
{
private List<Font> PBFonts = new List<Font>();
private void Form1_Load(object sender, EventArgs e)
{
foreach(Control control in this.Controls)
{
if(control is PictureBox)
{
this.PBFonts.Add(control.Font);
}
}
}
protected override OnFontChanged(EventArgs e)
{
int index = 0;
foreach(Control control in this.Controls)
{
if(control is PictureBox picture)
{
picture.Font = this.PBFonts[index];
}
index++;
}
}
}

Related

winforms: move a borderless form (without title bar)

I have a host class which inherits from Form. I use it to embed an WPF user control to use it in a winform application. This is customizable, I mean, you can choose to make form borderless or not.
If borderless, you cannot move the window, so I need to implement it manually. In this case I subscribe to some event which do the trick (I have removed the not necessary things from the class to focus only on the important parts):
public class MyHostDialog : Form
{
public MyDialogHost()
{
InitializeComponent();
}
public MyHostDialog(MyDialog myDialog) : this()
{
this.ElementHost.Child = new MyHostDialogView(myDialog);
if (this.Borderless)
{
this.ElementHost.Child.MouseDown += Child_MouseDown;
this.ElementHost.Child.MouseUp += Child_MouseUp;
this.ElementHost.Child.MouseMove += Child_MouseMove;
}
}
// BELOW CODE DOES NOT WORK, IT WOULD WORK IF Input.MouseButtonEventArgs and Input.MouseEventArgs WERE
// System.Windows.FORMS.MouseEventArgs INSTEAD.
private bool dragging = false;
private System.Drawing.Point startPoint = new System.Drawing.Point(0,0);
private void Child_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
dragging = true;
startPoint = new System.Drawing.Point(e.X, e.Y);
}
private void Child_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
dragging = false;
}
private void Child_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (dragging)
{
System.Drawing.Point p= PointToScreen(e.Location);
Location = new System.Drawing.Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);
}
}
}
Above code would work if instead of using Input.MouseButtonEventArgs and Input.MouseEventArgs, they were System.Windows.FORMS.MouseEventArgs. So, how to convert that code to work with MouseButtonEventArgs?

How to change color of very button in button arrays when i hover mouse to it in c# winform

I have a button list which is a button array. Please tell me how to change the color of every button array button when I hover the mouse. I had tried to use loops in the event button when I used loop, but when I hover the mouse on the other button, this button still changes color although I never set any event for it.
I hope this code sample will be helpful to you
public partial class Form1 : Form
{
private Button[] buttonsArray = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetButtonsArray();
if (buttonsArray != null)
{
foreach (Button button in buttonsArray)
{
button.MouseEnter += button_MouseEnter;
button.MouseLeave += button_MouseLeave;
}
}
}
private void GetButtonsArray()
{
// This is an example to fill array of buttons.
// You have to fill array in your way, according to your task.
foreach (Control control in this.Controls)
if (control is Button)
buttonsArray = buttonsArray.Append(control as Button);
}
private void button_MouseEnter(object sender, EventArgs e)
{
if (sender is Button)
(sender as Button).BackColor = Color.Yellow;
}
private void button_MouseLeave(object sender, EventArgs e)
{
if (sender is Button)
(sender as Button).BackColor = SystemColors.Control;
}
}
public static class Extensions
{
public static T[] Append<T>(this T[] array, T item)
{
if (array == null)
return new T[] { item };
T[] result = new T[array.Length + 1];
array.CopyTo(result, 0);
result[array.Length] = item;
return result;
}
}

Share a repetitive function between forms

This is my first question. I'm a complete newbie (I have some Arduino and Delphi knowledge, but nothing on C#) and there's something I've been attempting without success. In order to optimize some code I received I found some functions copypasted on each form and I would liek to reduce them to one (and better if in a separate .cs file), and access it from different forms.
For example: I have labels on Form1 and Form2, whose colour I want to be changed based on the .Checked property of a CheckBox on each Form. It's obvious cb_colorear finds controls in Form1. I tried to pass it values like (String formName) and then tried formName.Controls.Find but that didn't work. Any tips?
I'd also love to be pointed to some documentation on these matter. I've tried searching a bit, but without knowing C# and english not being fluent on English I'm struggling to find info I understand. Thank you all for your help.
//Form1 Code
namespace PruebaAccesoMetodos
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(); ;
}
public Color cb_colorear(String cbName, String label)
{
Control[] ctrl = Controls.Find(cbName, true);
CheckBox cb = ctrl[0] as CheckBox;
Control[] ctrl2 = Controls.Find(label, true);
Label lbl = ctrl2[0] as Label;
if (cb.Checked == false)
{
return Color.Red;
}
else
{
return Color.LawnGreen;
}
}
private void button1_Click(object sender, EventArgs e)
{
label1.BackColor = cb_colorear(checkBox1.Name, label1.Name);
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", #"c:\");
}
}
}
And Form2 code:
namespace PruebaAccesoMetodos
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1instance = new Form1();
label1.BackColor = frm1instance.cb_colorear(this.checkBox1.Name, this.label1.Name);
}
}
}
Update:
Ok, I made it work. Should've tried a bit more before asking haha.
Changed the cb_colorear function to receive Form type values:
public Color cb_colorear(String cbName, String label, Form formName)
{
Control[] ctrl = formName.Controls.Find(cbName, true);
CheckBox cb = ctrl[0] as CheckBox;
Control[] ctrl2 = formName.Controls.Find(label, true);
Label lbl = ctrl2[0] as Label;
if (cb.Checked == false)
{
return Color.Red;
}
else
{
return Color.LawnGreen;
}
}
Then added a this to each button calling the function:
private void button1_Click(object sender, EventArgs e)
{
label1.BackColor = cb_colorear(checkBox1.Name, label1.Name, this);
}
But, while it works, is this the propper solution?
You could use an extension method.
public static class Extensions
{
public static void cb_colorear(this Form form, string cbName, string label)
{
Control[] ctrl = form.Controls.Find(cbName, true);
CheckBox cb = ctrl[0] as CheckBox;
Control[] ctrl2 = form.Controls.Find(label, true);
Label lbl = ctrl2[0] as Label;
if (!cb.Checked)
{
lbl.BackColor = Color.Red;
}
else
{
lbl.BackColor = Color.LawnGreen;
}
}
}
So what have we done here:
We create a new static class (I chose to call it Extensions for the example).
We create our extension method. This method has to be static. Notice the first argument (this Form form instead of just Form form). This single keyword makes all the difference. It is what turns our normal method to an extension.
The rest is the same as your code. We use form as we would any other arguments. There is no need to return the Color, as we can set the label's BackColor property directly.
With the extension method declared, we can do the following in any form:
private void SomeFormMethod()
{
this.cb_colorear(checkBox.Name, label.Name)
}
The first argument is passed to the extension method implicitly, allowing us to use the this.method() syntax.

How to set repeater control back colour by item index?

I am using a DataRepeater control to show a popup. I am able to set BackColor of the current item by this code
private void dataRepeater1_CurrentItemIndexChanged(object sender, EventArgs e)
{
dataRepeater1.CurrentItem.BackColor = Color.Red;
}
but I am unable to add BackColor white for the previous item. Also I want to change the BackColor of the item form the list I am hovering mouse.
One way to solve this is to have one more property in your class, maybe called DataRepeater1_PreviousItem:
class YourClass
{
DataRepeaterItem DataRepeater1_PreviousItem { get; set; }
// ... some other code
private void dataRepeater1_CurrentItemIndexChanged(object sender, EventArgs e)
{
if (DataRepeater1_PreviousItem != null)
DataRepeater1_PreviousItem.BackColor = Color.White;
dataRepeater1.CurrentItem.BackColor = Color.Red;
DataRepeater1_PreviousItem = dataRepeater1.CurrentItem;
}
}

Show form as a ToolTip

I need to display a Form as a ToolTip of an UserControl. When the mouse is over the UserControl, the Form have to be show, and when the mouse leave that UserControl, the Form have to be hide.
I've overrided these events, in my UserControl class :
public partial class TreatedMetricsDisplay : UserControl
{
private TreatedMetricsWindow _treatedMetricsWindow;
public TreatedMetricsDisplay()
{
InitializeComponent();
_treatedMetricsWindow = new TreatedMetricsWindow ();
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover (e);
this._treatedMetricsWindow.Show();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave (e);
this._treatedMetricsWindow.Close ();
}
}
No exceptions, but events are not called.
Showing the Form will steal the Focus, It will get activated. So, it isn't a good idea to show Form as ToolTip. It will not behave as one would expect.
You need to use ToolStripDropDown combined with ToolStripControlHost. Which makes it possible to show any control as tooltip(not exactly).
public partial class MainForm : Form
{
private ToolStripDropDown dropDown = new ToolStripDropDown();
public MainForm()
{
InitializeComponent();
dropDown.Items.Add(new ToolStripControlHost(new ToolTipUserControl() { Size = new Size(200, 200) }));
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
dropDown.Show(MousePosition);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
dropDown.Hide();
}
}
ToolTipUserControl could be any Control which you want to show as tooltip.
Here is a code sample that might help:
private void ToolTipControl_Load(object sender, EventArgs e)
{
AttachHandlers(this);
}
private void AttachHandlers(Control currentControl)
{
foreach (Control control in currentControl.Controls)
{
control.MouseHover += GenericMouseHover;
control.MouseLeave += GenericMouseLeave;
if (control.Controls.Count != 0)
{
AttachHandlers(control);
}
}
}
void GenericMouseLeave(object sender, EventArgs e)
{
// no need to hide it if there was no form created in first place
if(_form != null && _form.Visible)
Form.Hide();
}
private void GenericMouseHover(object sender, EventArgs e)
{
Form.Location = this.PointToClient(Cursor.Position);
Form.Show();
}
ToolTipForm _form;
private ToolTipForm Form
{
get
{
if (_form == null)
{
_form = new ToolTipForm();
}
return _form;
}
}

Categories

Resources