Setting panel-objects properties - c#

I'm making a Windows Form Application called FMP.
I've got a class called Form1, a class called Panels.
Then I use inheritance to make different Panels with different properties.
The reason for doing this is because the teacher doesn't want us to initialize all the panels in the Form-class.
But I'm not sure how to do this. Found some things here #Stackoverflow, but they couldn't help me either.
The Size, The Location and the color are for all the Panels the same. (By clicking on a button, an other panel will appear ;) )
But the Name, Controls on the panel, and BackgroundImages are different. The controls are the most important aspect here.
The question is:
The Width and the Height should be equal to the Widht and the Height from the Form.
What is best in programming C#? To set the Width and the Height from Panels in the Form1 (but i made them protected) or declarate the form in the Panels class and use
Form1.Width?
The code I'm having right know:
The Form1
public Form1()
{
InitializeComponent();
buttonsProperties();
panelsProperties();
}
private void button1_Click(object sender, EventArgs e)
{
panelsChanged(1);
}
private void button2_Click(object sender, EventArgs e)
{
panelsChanged(2);
}
private void panelsChanged(int panelNr)
{
if (panelNr == 1)
{
panel1.Visible = true;
panel1.Enabled = true;
panel2.Visible = false;
panel2.Enabled = false;
}
else if (panelNr == 2)
{
panel1.Visible = false;
panel1.Enabled = false;
panel2.Visible = true;
panel2.Enabled = true;
}
}
The Panels
class Panels
{
Form1 f = new Form1();
//Color Property
protected Color color { get; set; }
//Size
protected Int32 Width { get; set; }
protected Int32 Height{ get; set; }
//Location
protected Point Location { get; set; }
public Panels()
{
initMembers();
}
private void initMembers()
{
this.Width = f.Width;
this.Height = f.Height;
this.Location = new Point(0, 0);
}
}
public class Panel1 : Panels
{
//Nothing yet.
}

Using the name Panels as a base class for each panel is confusing:
The name should not be in a plural form, as each instance of the class clearly resembles one "panel" (a panel has a Width, multiple panels do not have (one) width)
Since you're crating a WinForms application, the name looks too much like the System.Windows.Forms.Panel class
If I were you I'd let your base class derive from System.Windows.Forms.Panel:
abstract class MyPanelBase : Panel
{
public MyPanelBase()
{
Dock = DockStyle.Fill;
}
}
class MyPanel1 : MyPanelBase
{
}
This way you automatically get the behavior (and properties) of the Panel and it allows you to add it to a parent control (in your case, the form).
If all functionality you want is already supported by Panel you could even skip the MyPanelBase bit and let MyPanel1 derive directly from Panel.

Related

Windows Forms, accessing picturebox from class in main form

I'm making a game where I want to make things fall down and the player should try to catch it.
Currently, my items which are falling are drawn and made in a class separated from the main form, so when I try to access them from the main form where I control the gravitation.I can't seem to find the picturebox (picCoin).
Could you please take a look at my code and come with some solution?
This is the class were i create Coins
class Coin
{
private PictureBox picCoin = new PictureBox();
public Coin()
{
picCoin.BackColor = Color.Transparent;
picCoin.ImageLocation = #"C:\Users\sebfre1104\source\repos\SPEL\SPEL\Resources\hamburger.png";
picCoin.Width = 100;
picCoin.Height = 100;
picCoin.SizeMode = PictureBoxSizeMode.StretchImage;
}
public void DrawTo(Form f)
{
f.Controls.Add(picCoin);
}
public Rectangle getBounds()
{
return picCoin.Bounds;
}
public void setposition(int x, int y)
{
picCoin.Location = new Point(x, y);
}
}
i want to reach this picCoin in my main form so i can add gravity in private void TimerGravitation_Tick(object sender, EventArgs e)
public partial class Form1 : Form
{
bool IsJumping = false;
List<Coin> clist = new List<Coin>();
int poƤng = 0;
public Form1()
{
InitializeComponent();
}
private void TimerGravitation_Tick(object sender, EventArgs e)
{
if(!picSpelare.Bounds.IntersectsWith(picMark.Bounds) && IsJumping==false)
{
picSpelare.Top += 10;
}
}
I would be very grateful with any tips as this is my final project in class.
i dont know if i understanded question correctly but make picCoin public
make
private PictureBox picCoin = new PictureBox();
to
public PictureBox picCoin = new PictureBox();
At first, you should follow Umut Arpat's answer (turn picCoin from private to public).
Then you should initialize the Coin class in Form1 constructor like this:
private Coin _coin;
public Form1()
{
InitializeComponent();
_coin = new Coin();
}
And then you should add picture box from Coin class to list of Form1 controls like this:
this.Controls.Add(_coin.picCoin)
You can do that in Form1 constructor too.
Don't add PictureBox to Controls of the form from the Coin class (method DrawTo()).
class Coin
{
private PictureBox picCoin = new PictureBox();
public PictureBox Box {
get { return picCoin};
set { picCoin = value};
}
....
}
To use/access the box:
clist[i].Box // This is what you want

How load a form into another form's panel

I have a form called frmTest1 with a splitcontainer with two panels. Panel 2 should load many forms one at a time. It works fine for the first form, but the second form cant then load a third form into panel 2 of frmTest1.
Here is stripped frmTest 1 code:
namespace Project1
{
public partial class frmMain3 : Form
{
public frmMain3()
{
InitializeComponent();
}
private void btnShowTest1_Click(object sender, EventArgs e)
{
showScreen(new frmTest1());
}
public void showScreen(Control ctl)
{
while (splitContainer1.Panel2.Controls.Count > 0)
splitContainer1.Panel2.Controls[0].Dispose();
if (ctl is Form)
{
var frm = ctl as Form;
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Visible = true;
}
ctl.Dock = DockStyle.Fill;
splitContainer1.Panel2.Controls.Add(ctl);
}
}
}
The second form's code is below:
namespace Project1
{
public partial class frmTest1 : Form
{
public frmTest1()
{
InitializeComponent();
}
private void btnShowTest4_Click(object sender, EventArgs e)
{
frmMain3 main = new frmMain3();
main.showScreen(new frmTest4()); //Nothing shows
}
}
}
From the research I have done it seems the solution is to use a usercontrol but having never used it before, I am struggling. Can someone please show me how to resolve this?
Please try to use a UserControl. Just change the base class of frmMain3 to "UserControl" and delete the whole "if (ctl is Form)"-part from showScreen().

How to make an event span over multiple forms

The project I am working on needs a couple buttons on multiple forms, instead of doing the code shown below I was hoping it could be made global.
This is only one part of the project, all the code does is enlarge the button picture when the user hovers over it.
I've tried looking at classes, tags and attributes. I know classes can be made to use across multiple forms but I cant find out if they work with events.
private void btnEnter_MouseEnter(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Size = new Size(299, 102);
}
private void btnLeave_MouseLeave(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Size = new Size(289, 92);
}
You can create an inherited button. Add a new class then make sure you put : Button after the class name.
using System.Drawing;
using System.Windows.Forms;
namespace InheritedButton
{
public class ExpandButton : Button
{
public Size EnterSize { get; set; }
private Size _LeaveSize;
public Size LeaveSize
{
get
{
return (_LeaveSize);
}
set
{
_LeaveSize = value;
this.Size = LeaveSize;
}
}
public ExpandButton() : base()
{
}
protected override void OnMouseEnter(EventArgs e)
{
this.Size = EnterSize;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
this.Size = LeaveSize;
base.OnMouseLeave(e);
}
}
}
Build your project and the new button will appear in the toolbox. Drop it onto a form/control and make sure you set the EnterSize and LeaveSize. EnterSize determines the size of the button when you mouse over and LeaveSize sets the initial size and sets the size of the button when you mouse out. You don't need to set the Size property, just set LeaveSize.
Any time you want to use the expanding/contracting button just use the inherited one instead.

PropertyGrid does not update selected object

I want to implement a simple feature where user's can customize the Font of labels.
So I have FontEditor form with the following code:
public partial class FontEditor : Form
{
public Font myFont;
public FontEditor(Font myFont)
{
InitializeComponent();
this.myFont = myFont;
propertyGrid1.SelectedObject = this.myFont;
}
private void FontEditor_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult = DialogResult.OK;
}
}
I use it on a chart control like this:
using (FontEditor fe = new FontEditor(chart1.Titles[0].Font))
{
if (DialogResult.OK == fe.ShowDialog())
{
chart1.Titles[0].Font = fe.myFont;
}
}
When the font editor loads, i can see the following:
If I changed the Size from 18 to 10 and close the window, the SelectedObject (which is my font object from chart title) does not appear to be changing/updating:
Doesn't editing the property grid values should update the SelectedObject?
Okay, I realise the silly mistake. The edits you do on PropertyGrid are actually in propertyGrid1.SelectedObject.
This is how I fixed it:
FontEditor.cs
public partial class FontEditor : Form
{
public FontEditor(Font myFont)
{
InitializeComponent();
propertyGrid1.SelectedObject = myFont;
}
private void FontEditor_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult = DialogResult.OK;
}
public Font UpdatedFont
{
get { return propertyGrid1.SelectedObject as Font; }
}
}
Usage
using (FontEditor fe = new FontEditor(chart1.Titles[0].Font))
{
if (DialogResult.OK == fe.ShowDialog())
{
chart1.Titles[0].Font = fe.UpdatedFont;
}
}
This now works.

Trouble creating user control in C#

I'm creating a user control in C# but I can't figure out how to do the event stuff. I want to change the backcolor property of the panel on mouse hover but it's not working.
Code:
public partial class QuestionList : UserControl
{
public QuestionList()
{
InitializeComponent();
}
public struct QuestionListItem
{
public string Question { get; set; }
public string Answer { get; set; }
public QuestionListItem(string question, string answer)
{
Question = question;
Answer = answer;
}
}
public void Add(QuestionListItem questionlistItem)
{
Panel panel = new Panel();
panel.Dock = DockStyle.Top;
Label label = new Label();
label.MouseHover += Label_MouseHover;
label.Dock = DockStyle.Fill;
label.Text = questionlistItem.Question;
panel.Controls.Add(label);
Controls.Add(panel);
}
//Here (no idea what I just did..)
private void Label_MouseHover(Object sender, EventArgs e)
{
Label label = (Label)sender;
Panel panel = (Panel)label.Container;
panel.BackColor = Color.Red;
}
}
I think you have added your event handler right. The problem is with the line you put in your event handler:
Panel panel = (Panel)label.Container;
Should be
Panel panel = (Panel)label.Parent;
Change the Container into Parent.
Also, I think it is best to use VS designer to test what is the strongly-typed signature of the event handler. In your signature, you use EventArgs. I believe it should be MouseEventArgs instead.

Categories

Resources