change form background image through a button click - c#

I want to change the form's back ground image when I click the button. I'm stuck at this error. It says:
An object reference is required for
the non-static field, method, or
property
'System.Windows.Forms.Control.BackgroundImage.get'
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Location = new Point(25, 9);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Location = new Point(18, 9);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Form1.BackgroundImage =
}
On the last part of the code, you can see that I am attempting to change the background image of the form. but it does not allow me and I don't know how to do it properly.

Use this instead of Form1:
this.BackgroundImage = ...

Form1 is a Type, not an Instance of an Object, you're looking for this.

if you want to use Form1 use this:
Form1 f1 = new Form1();
f1.BackgroundImage = ...

Related

How to pass variable value between identical instances of WindowsForm

I have a base form From1. In the form when button is clicked, new form From2 is created.
private void Button_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
User can create several instances of From2. In From2 user can set a value in textBox and click a button. Once it is clicked, value from textBox has to be somehow transferred to all other created instances of From2. How can I do that?
Step 1: remember all forms you created.
private void Button_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
allForms.Add(f2); // remember it
f2.Show();
}
Step 2: when the value changes, update all remembered forms
private void textbox1_TextChanged(object sender, EventArgs e)
{
foreach (Form2 form in allForms)
{
form.MyValue = textbox1.Text;
}
}
Just write the code like that, then let the IDE help you creating the properties and adjust the visibility accordingly, e.g. have the IDE help you implement a property in Form2 that sets the text
string MyValue
{
set
{
anotherTextbox.Text = value;
}
}
You'll then notice that you need some more stuff, probably.
Step 3: remove the form from the list when it is closed.
private void Button_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
allForms.Add(f2);
f2.Closed += OnClose; // Method to be called when form is closed
f2.Show();
}
private void OnClose(object sender, EventArgs e)
{
Form2 form = (Form2) sender;
form.Closed -= OnClose; // Unregister event handler
allForms.Remove(form); // remove it
}

Adding picturebox at runtime and changing their properties (from another method)

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).

Drawing from one Form on another while the one is closing/closed

I am programming something in c# for school. I have 2 Forms and while I close the child form I want to draw something on the parent Form and I don't want to use any button I would have to click afterwards. This is what I have tried but it doesn't work.
private void button1_Click(object sender, EventArgs e)
{
this.Close(); //Closes Form2(Child)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
Graphics g;
g = f1.CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
}
I also have tried using the FormClosed event.
On your parent form, when you initialize the child form, set it's Owner property to this:
var form2 = new Form2();
form2.Owner = this;
And then from the child form, you can access the parent by doing this:
Graphics g;
g = ((Form1)this.Owner).CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
Taking in consideration your Form1 is the parent form and Form2 is your child form, that is Form2 is being called from Form1 originally. Then you should first send your copy of Form1 to the new Form2 you are creating, stored it on a private field and on closing use it to draw on it. Something similar to:
private Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close(); //Closes Form2(Child)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Graphics g = _parent.CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
}
Also on parent, you should have something similar to:
private void button1_Click(object sender, FormClosingEventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
The problem with the code you posted is that you are creating a new Form1, which is not the same object/instance as the original Form1 you have already when opening a new instance Form2.

On c# the mousehover on a picturebox appear to do nothing.Do i need some kind of eventhandler?

I am trying to create a MouseHover event for a pictureBox, but I have had no luck so far:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Image = ArgyroCinema.Properties.Resources.ktz00h07;
label1.Text = "hover";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = ArgyroCinema.Properties.Resources.ktz00h07;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = ArgyroCinema.Properties.Resources.ktz00h07;
}
What is going on here? MouseClick works correctly, maybe I have to add something on Form1.Designer.cs ?
ok i had to add this line on the constructor:
this.pictureBox1.MouseHover += new System.EventHandler(this.pictureBox1_MouseHover);
although i will use mouseenter, its faster

Set control visible from other form

I set the button visible property to false of Form2. How I will make the button(Form2) visible when I click a button(a button that also opens Form2) from Form1.
I tried this :
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.button1.Visible = true;
f2.button1.Location = new Point(200, 200);
}
Create a method in Form2
public void setButton1Visible(boolean flag){
this.button1.Visible = flag;
}
You cannot access the button directly from Form1. (Actually you can,but it is not right way to solve it.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.setButton1Visible(true);
}
I think button1 is declared as private. Your code will work if you declare button1 as public.
public System.Windows.Forms.Button button1;
Imagine you control is in form 1. Set corresponding control "modifiers = public" from control property window
Form 1
private void ShowForm2_Click(object sender, EventArgs e)
{
Form2 NewForm = new Form2();
NewForm.Owner = this;
NewForm.Show();
}
In Form 2
private void ChangeProperty_Click(object sender, EventArgs e)
{
(this.Owner as Form1).MyButton.Visible = false;
}
//while doing this Control In Form1 will be hidden :)

Categories