Cannot change object property from other window - c#

I have the main window, and another window. in the 2nd window i've created new canvas, and i want to change its properties from the main window, i've failed with this try:
this is the 2nd window's class:
public partial class window2 : Window
{
public Canvas painting = new Canvas();
public window2()
{
}
}
and here i try to change its property from the main window:
window2 paint = new window2();
private void button1_Click(object sender, RoutedEventArgs e)
{
paint.painting.Background = Brushes.Black;
}
when i click the button it does nothing.
Edit:
i think it would be better if i'll use the Application.current.properties and store the canvas object, but i don't know how to use it, i tried this:
Application.Current.Properties["p1"] = painting;
now how can i set properties from the main window with using the "p1" variable i've just created? i tried p1.background but i cant use p1 as variable, so how do i do it?

Your window2 constructor should contain this:
this.AddChild(painting);
Whenever you create a new control (like Canvas) you should set its parent container.
This is my code from window2:
public Canvas painting = new Canvas();
public window2()
{
this.AddChild(painting);
}
And mainwindow:
private void button1_Click(object sender, RoutedEventArgs e)
{
window2 w = new window2();
w.Show();
w.painting.Background = Brushes.Black;
}
What I believe you are saying is that you have an undetermined number of canvases and you want to access them all. I suggest you keeping them in a List of canvases or in a HashTable (you need to use System.Collections namespace). Also don't forget to set the parent container.

A Canvas is a little odd as far as WPF controls go. It likely has a zero size, so you're not seeing the change. Try hard-coding a size to check whether the code is working. In the window2 constructor do this:
painting.Width = 100;
painting.Height = 100;

Related

How to implement usercontrol to be visible on click inside one panel (DockPanel)?

I am making a basic application in WPF (C#) where I wanted to use UserControl. I already created 3 sample UserControl and a single page window which have buttons, labels, panel etc. I wanted to load each UserControl on click button. You can see my logic (code) below -
public partial class Dashboard : Window
{
public Dashboard()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
Dock_MainPanel.Controls.Clear();
Dock_MainPanel.Visible = true;
Sample1 usr1 = new Sample1();
usr1.Show();
Dock_MainPanel.Controls.Add(usr1);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
//SAMPLE CODE
}
}
Now my issue is this code is not working. It is stating some error. It is shown in image as below -
Error shown in image
Basically I wanted to load each UserControl on clicking of their respective button. If anyone having other solutions, it is welcomed.
As #Stefan W. suggests you should add an instance of the UserControl to the DockPanel's Children collection but if you intend to add several elements to the same DockPanel you probably also want to set the attached Dock property of the element using to specify where in the DockPanel the element will be located. You can do this using the DockPanel.SetDock method. Also, to make the DockPanel visible, you set its Visibility to Visibility.Visible. Try this:
private void btn1_Click(object sender, RoutedEventArgs e)
{
Dock_MainPanel.Children.Clear();
Dock_MainPanel.Visibility = Visibility.Visible;
Sample1 usr1 = new Sample1();
DockPanel.SetDock(usr1, Dock.Right);
Dock_MainPanel.Children.Add(usr1);
}
You should access the 'Children' collection in DockPanel
Dock_MainPanel.Children.Clear();
Dock_MainPanel.Children.Add(usr1);

C# usercontrol show

I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}

Edit Frame with button within Frame

I have a MainWindow, wich Contain a Frame.
My Frame switches between different xaml's using buttons on my MainWindow.
the problem I have now, is that I need to do this also from buttons within the xaml loaded within my Frame.
i tried te following:
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = new MainWindow();
Page myPage = new Page();
mw.editFramePage(myPage);
}
here is my edit editFramePage Method:
public void editFramePage(Page page)
{
myFrame.NavigationService.Navigate(page, UriKind.Relative);
}
This works, however it pops up a new MainWindow, I want this withing my current MainWindow.
Could use some help!
The problem is that you are creating a new MainWindow. You need to keep a reference to the original MainWindow and call editFramePage on that.
The default implementation skeleton of a WPF application saves the main window in App.MainWindow. To get to it, you need to use Application.Current.MainWindow.
So your function should look like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = (MainWindow) Application.Current.MainWindow;
Page myPage = new Page();
mw.editFramePage(myPage);
}
Note that the cast to MainWindow is required because Application.MainWindow returns the type Window instead of the more-specific type that you need.

Winforms Control

I have a button which I use all the time as a little pick button next to a combobox. When I click the button I open a larger full list. This side of things work well and I do not have a problem with this..
My problem lies when someone said to me can you change that ugly icon you picked to my nice icon.
I went crap, I have hundreds of these buttons on many forms. So I thought I will create a custom control called PickButton (which is a standard button and heap of default proeprties set) and drop these on the form everywhere instead. In the code of the PickButton custom control I set some properties and the image to the customers nice icon.
So I drop the PickButton from my toolbox onto the form, so far things are looking pretty good and I am feeling a bit clever. Now I think to myself I will change back to my nice icon not the crappy one the customer picked and change the code in the PickButton custom control. But I cannot get rid of that customers icon, because the code when the PickButton run happens before the code in the designer file which has the customers icon.
So my aim was to have a PickButton control and be able to change the icon and other properties in one place and all the properties would be set when an instance of the control is created and displayed on the form.
Was I not so clever and went about achieving the task the wrong way???
This is my PickButton custom control class
public class PickButton : Button
{
public PickButton()
{
InitialiseButton();
}
internal void InitialiseButton()
{
this.ImageAlign = ContentAlignment.MiddleCenter;
this.Image = WindowsFormsApplication1.Properties.Resources.Cancel.ToBitmap();
this.Size = new Size( 28, 28 );
this.Dock = DockStyle.Fill;
this.Margin = new Padding( 0, 2, 2, 0 );
this.Text = string.Empty;
}
}
Now I drop one onto my form and the code in the designer is as follows
//
// pickButton1
//
this.pickButton1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pickButton1.Image = ((System.Drawing.Image)(resources.GetObject("pickButton1.Image")));
this.pickButton1.Location = new System.Drawing.Point(0, 0);
this.pickButton1.Margin = new System.Windows.Forms.Padding(0, 2, 2, 0);
this.pickButton1.Name = "pickButton1";
this.pickButton1.Size = new System.Drawing.Size(284, 262);
this.pickButton1.TabIndex = 0;
this.pickButton1.Text = "pickButton1";
this.pickButton1.UseVisualStyleBackColor = true;
Now I want to change the image so I change my PickButton code to use a different icon
this.Image = WindowsFormsApplication1.Properties.Resources.Browse.ToBitmap();
Run the application andd the first icon is still the one being displayed because of this line of code in the designer file
this.pickButton1.Image = ((System.Drawing.Image)(resources.GetObject("pickButton1.Image")));
The concept of setting all the properties in one place was a good idea, it just wasn't implemented quite right. I would make this class inherit from UserControl instead of from Button. By making it a UserControl, you can use the designer to set all the properties you want, like the default Image for the button. Set that in the designer, then just drag and drop your UserControl from the toolbox onto your forms. If you are only using your "PickButton" control with comboboxes, I would put the combobox on the UserControl as well. If you ever want to change your button image in the future (or any other property for that matter), you will be able to change it in ctlPickButton and that will propogate the changes to all the instances used throughout your project(s).
ctlPickButton:
public partial class ctlPickButton : UserControl
{
public event EventHandler pickButtonClicked;
public ctlPickButton()
{
InitializeComponent();
}
//Allows buttons image to be set in code if necessary
public Image Image
{
get
{
return button1.Image;
}
set
{
if (Image != null)
{
button1.Image = value;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (pickButtonClicked != null)
{
pickButtonClicked(sender, e);
}
}
}
Demo Form:
public Form1()
{
InitializeComponent();
ctlPickButton1.pickButtonClicked += new EventHandler(ctlPickButton1_pickButtonClicked);
ctlPickButton2.pickButtonClicked += new EventHandler(ctlPickButton2_pickButtonClicked);
}
void ctlPickButton2_pickButtonClicked(object sender, EventArgs e)
{
if (comboBox2.SelectedItem != null)
{
MessageBox.Show(comboBox2.SelectedItem.ToString());
}
}
void ctlPickButton1_pickButtonClicked(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
MessageBox.Show(comboBox1.SelectedItem.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("French");
comboBox1.Items.Add("Spanish");
comboBox1.Items.Add("English");
comboBox1.Items.Add("German");
comboBox2.Items.Add("Pizza");
comboBox2.Items.Add("Hamburger");
comboBox2.Items.Add("Potato");
comboBox2.Items.Add("Chicken");
//Shows how the default image set in the designer can be overwritten for a
//specific instance using the "Image" property
ctlPickButton2.Image = Testbed.Properties.Resources.searchIcon2;
}
}
Image of ctlPickButton in designer
I think I've found a simple, clean solution:
In the CustomButton class (which inherits from System.Windows.Forms.Button), override the Refresh() method, and set the image of the button to the one you want to see:
public class CustomButton : Button
{
public override void Refresh()
{
Image = MyResources.HappyFace;
}
}
In the form that will hold an instance of your CustomButton, simply call customButton.Refresh() in the constructor, after InitializeComponent():
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
customButton.Refresh();
}
}
I've put a demo application up on Github.

How to effect change from child to parent window

This is probably a fairly simple question, but I have had no luck researching it thus far. I have a child window that has a yes and no button on it. When I click no I would like a check box to become unchecked in the parent window (which is the main window of my program).
Is there anyway that I could do something like?:
//No Button
private void No_Click(object sender, RoutedEventArgs e)
{
NameOfParent.checkBox.Checked = false;
}
I've seen this question but do not think that it exactly addresses my problem.
What is the correct way to go about this?
I've been using this to open my other windows:
Parent Window - Current code:
//Open new window
private void checkBox5_Checked(object sender, RoutedEventArgs e)
{
var newWindow = new ChildWindow();
newWindow.button_no.Click += buttonNo_click;
newWindow.Show();
}
//Unchecks Checkbox 5
private void buttonNo_click(object sender, RoutedEventArgs e)
{
checkBox5.IsChecked = false;
}
Opening the child window from the parent window:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.buttonNo.Click += buttonNo_Click;
window.Show();
}
In the code of the parent window, include this click event for the child window's no button:
private void buttonNo_Click(object sender, RoutedEventArgs e)
{
//code to uncheck the checkbox goes here
}
This turned out even simpler than the solution for Windows Forms that I've been using...and it appears to work in Windows Forms as well.
On construction of the child window, simply add a parameter that is of the type of the parent window.
Then when you are constructing your child window in the parent window code do the following (In The Parent Window Class):
ChildWindow child = new ChildWindow(this, other_param, other_param2.....);
ChildWindow.Show();
The keyword this will pass the parent window into the ChildWindow
Then you can store it into a class variable and access properties of it.
Child class constructor and parentWindow variable:
private ParentWindowType m_parentWindow = new ParentWindowType();
public ChildWindow(ParentWindowType parent, int other_param, string other_param2....)
{
m_parentWindow = parent;
}
Then in other child class methods, such as your button click handler you can access properties from the parent window:
public void ButtonClickHandler(....)
{
m_parentWindow.checkBox1.Enabled = true;
m_parentWindow.checkBox1.Checked = true;
}
This way you can store the parent window within the child window, and always have access to it.
Its nice and clean and simple this way. Ensure to make the parent window variable private in the child window class.
If made public, it would allow you to do funny things like
parentWindow.childWindow.m_parentWindow.childWindow etc......

Categories

Resources