Change BackgroundImage of form from Custom Control running in Panel - c#

My project consists of a form with a panel that contains a custom control.
In this custom control I have buttons that change the background image.
My issue is; these buttons only change the background image of the custom control that they are placed in, and I need them to change the background image of the main form containing the panel with the custom control.
My current code:
this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
I need something that will in effect accomplish this:
MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
ie: Change background image of MainForm.cs from CustomControl.cs

You can use Control.FindForm method for that, like this
this.FindForm().BackgroundImage = ...

I ended up using something different:
Form MainForm = Application.OpenForms["(The name of the form in which I wanted to change the background)"];
//...
MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
It ended up being a lot simpler than I thought it was.

Find the control's parents and change the BackgroundImage:
if(this.Parent!=null && this.Parent.Parent!=null)
this.Parent.parent.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));

Related

Need to use a color dialog result in a separate Windows Form

Please go easy on me I am very new to coding and stuck on something that I'm sure is very simple.
Currently I have an Options form in which the user can select from the standard colordialog.showdialog() and the result is displayed as a sample on the form as a label
If(backColorDialog.Showdialog() == DialogResult.OK);
backColorLabel.BackColor = backColorDialog.Color; // set to label to show option selected
I need to take that selected color and apply it to the background color of a "game board" in another Windows form. I have already added a reference from the "game board" form to the options form.
The "Game Board" is laid over a TableLayoutPanel so I need to be able to change the BackColor of the panel
TableLayoutPanel1.BackColor = (colordialog result from options form)
Like I said I am new to this and appreciate any help you can provide
Create a property on your configuration form:
Color SelectedColor { get; private set; }
And assign users choice to it:
if (backColorDialog.Showdialog() == DialogResult.OK)
{
backColorLabel.BackColor = backColorDialog.Color;
SelectedColor = backColorDialog.Color;
}
(note there was a mistake in your code - the ; after if line)
After this, in your main form you can read it from your configuration form:
TableLayoutPanel1.BackColor = optionsForm.SelectedColor;
As noted by justin.chmura in the comments below, you can also consider a different approach using events. Your options form would raise an event when user selects a different color, and main form's event handler would apply this color instantly on the game panel. The difference is that it would update the color immediately. When it's not needed the above solution with a property is enough.

Windows form C# change user control by code

I have a windows form and i dont want to make any other windows forms just one windows form and different user controls how can i change between user controls for example hide one and show the other user control programmatically ?
private void Btt_info_Click(object sender, EventArgs e)
{
Frm_Main frm_main = new Frm_Main();
frm_main.Controls["panel1"].Controls.Clear();
UC_Info uc_info = new UC_Info();
frm_main.Controls["panel1"].Controls.Add(uc_info);
}
i added this but it doesnt work
Add a container control (if I remember correctly, there's a containers section in the toolbox?), like a panel. Create usercontrols for what you want to dynamically switch around. So make like a 'HomePage' usercontrol and a 'LoginPage' usercontrol. Dynamically add the usercontrol that you want to display to the container. WHen you want, remove it from the container and add a different usercontrol:
Panel myPanel = new Panel();
LoginPage ctlLoginPage = new LoginPage();
HomePage ctlHomePage = new HomePage();
//add the loginpage to the panel first
myPanel.Controls.Add(ctlLoginPage);
...do stuff...
//remove whatever control is currently in the Panel
myPanel.Controls.Clear();
//add the other control, the HomePage control instead now
myPanel.Controls.Add(ctlHomePage);
..do other stuff...
I usually do it this way so you leave your form itself open to add common controls and stuff that might be shared between your different 'pages'.
EDIT: Note that I normally would add the panel in the designer and not create it dynamically in the code. This was just an example.
EDIT: The interaction between your mainform and usercontrols can be handled in a few different ways, and I am not saying that any of these is the correct method.
You create a static property for your Panel on the Mainform, so that
you can always access it to swap your controls around.
In this example I'll also add a static method for it
enum PanelControlsEnum {HomePage, LoginPage};
public static Panel MyContainerPanel {get;set;}
public static void SwitchPanelControls(PanelControlsEnum selControl){
..put your switch panels code here..
}
Then inside your usercontrol you call a predefined method, something like:
MainForm.SwitchPanelControls(PanelControlsEnum.HomePage);
Another method is to bind the button click event on your mainform
instead of inside the form.
Like This:
HomePage ctlHomePage = new HomePage();
ctlHomePage.Click += MyClickEvent;
myPanel.Controls.Add(ctlHomePage)
...
private void MyClickEvent(object sender, RoutedEventArgs e)
{
..switch user control code here...
}
Create a method that returns a UserControl object. Then put conditions in that method as to which control you want to load at a specific condition and then in your main form code.
UserControl control = GetControlFromMyMethod();
form1.Controls.Add(control);
where 'control' is the returned control from your method.
To remove the existing one you have to loop through the form1.Controls and find out the control and call 'Remove'.
Update:
Mike C has a better idea of adding a panel and loading your desired control on the panel as then it's easy to remove your control and you then don't have to loop through the forms controls to find it and then remove it.
Try this:
this.Controls.Clear();
usercontrol load = new usercontrol ();
this.Controls.Add(load);
load.Show();
you could try this it will definitely help you as it did helped me a lot it short and straight to the point hope that will help

How to prevent usercontrol fill option from extending too far

I have a usercontrol that I'm adding as a control of a main form dynamically. The Mainform is basically empty, except it has a large status bar on bottom.
Problem is, when I set the Dockstyle.Fill option on my usercontrol, the size of the loaded usercontrol extends beyond the statusbar (It fills the entire main form as if the status bar wasn't there).
How do I prevent this behavior? This is an example of how I dynamically load my form
logicForm = new LogicForm();
this.Controls.Add(logicForm);
logicForm.Dock = DockStyle.Fill;
I think you need to set the DockStyle to None and use the Anchor property instead.
Set the anchor to Top,Bottom,Left,Right and size your control to fill all the space up to the status bar.
You should find when you run that the user control will then resize with the form.
I just found the solution
I need to bring the form to front in order to properly dock it if I already have some other controls on the main form:
logicForm.BringToFront();
Found here: http://dotnetref.blogspot.kr/2008/08/using-dock-fill-on-control-when-you.html
-________________________-

c# Close a form opened from and instance class from the main form that created the class

I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?

Is their something like iframe in C#?

Is their something like iframe in c#?
I want to build an application with one form that its content changes according to the actions I do, like messenger live. The part when I log in and log out its too different "windows" but it happens in the same form.
There is no iframe equivalent in winforms or wpf, but there are ways to deal with it.
For either winforms or wpf, what you want to do is have a Panel which you change the content of.
A panel is a container which holds/encapsulates other controls.
If you have two different views you want to toggle between, create two panels at the same position with the content you need. Then you will show one and hide the other. When the user executes some action which requires you to change the view, then simply hide the displaying panel, and unhide/show the other one.
Think of it as layers, where you will only show one at a time.
You can also dynamically load user controls into a panel, much like an iframe, but I find it easier to have the content in the form, and hide/show as needed.
Assuming WinForms, how about using UserControls? Place as many as you want in a single/multiple forms, and interact as you do in forms. See, UserControl class.
You can load a form into a Panel or tabPage from a TabControl:
Form f = new Form();
f.TopLevel = false;
panel1.Controls.Add(f);
f.Show();
f.Dock = DockStyle.Fill;

Categories