Description - I have main window having status label which shows status in the form of connected and disconnected. When i create a object(parent window) in child window and set label status it wont be reflected in parent window. .I also tried, took a one Boolean variable and set to value 1 in child window code,but when i access that variable in parent window ,i got by default value that is zero.
/// My parent window code
public partial class MainWindow : Window
{
public CompSetting obj = new CompSetting();
public MainWindow()
{
InitializeComponent();
}
private void click(object sender, MouseButtonEventArgs e)
{
CompSetting compPortseting = new CompSetting();
compPortseting.ShowDialog();
}
private void button_Click(object sender, RoutedEventArgs e)
{
if (obj.flag == 1) // Here i get by defult value 0 . but already set to 1 on child window. In short cant access flag variable value.
{
LblPortStatus_lable.Content = "chetas";
}
else
{
LblPortStatus_lable.Content = "rahul";
}
}
}
// My child window code
public partial class CompSetting : Window
{
public int flag ;
public CompSetting()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
flag = 1;
}
}
private void click(object sender, MouseButtonEventArgs e)
{
if(obj != null)
{
obj.ShowDialog();
}
}
Do not create a new object, just use the existing one.
Extra
Instead of creating an instance of the child window consider creating an instance of a settings object. The child window would then get a reference to the settings object and modify it:
public partial class MainWindow : Window
{
public CompSetting settings = new CompSetting();
public MainWindow()
{
InitializeComponent();
}
private void click(object sender, MouseButtonEventArgs e)
{
var settingsDialog = new SettingDialog(settings);
settingsDialog.ShowDialog();
}
private void button_Click(object sender, RoutedEventArgs e)
{
if (settings.Flag == 1)
{
LblPortStatus_lable.Content = "chetas";
}
else
{
LblPortStatus_lable.Content = "rahul";
}
}
}
// My child window code
public partial class SettingDialog : Window
{
private CompSettings settings
public SettingsDialog(CompSettings settings)
{
this.settings = settings;
}
private void button_Click(object sender, RoutedEventArgs e)
{
this.settings.Flag = 1;
}
}
public class CompSettings
{
public int Flag;
}
Related
I have a "MainForm" and a "GraphicsForm". Clicking "New" on the main form, a "GraphicsForm" will be created.
The problem is that when I create multiple "GraphicsForm", and when I want to save the content of one of the "GraphicsForm", I need to clicking "Save" on the "MainForm" and the program will save the content of the active "GraphicsForm" to a file, I don't know how to pass the content of this "GraphicsForm" to "MainForm" for storage.
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private ToolStripMenuItem _winMenuItem = new ToolStripMenuItem();
private GraphicsForm _graphicsForm;
private int _counter = 1;
private void New_Click(objec sender, EventArgs e)
{
_winMenuItem.Name = "Win";
_winMenuItem.Text = "Windows";
int item = MainMenuStrip.Items.IndexOf(_winMenuItem);
if (item == -1)
{
MainMenuStrip.Items.Add(_winMenuItem);
MainMenuStrip.MdiWindowListItem = _winMenuItem;
}
_graphicsForm = new GraphicsForm();
_graphicsForm.Name = string.Concat("Win_", _counter.ToString());
_graphicsForm.Text = _graphicsForm.Name;
_graphicsForm.MdiParent = this;
_graphicsForm.Show();
_graphicsForm.WindowState = FormWindowState.Maximized;
_counter++;
}
private void Save_Click(object sender, EventArgs e)
{
... // Problem here
}
private void Open_Click(object sender, EventArgs e)
{
... // Problem here
}
}
GraphicsForm.cs
public partial class GraphicsForm : Form
{
//StorageDoc is a class to manage all the graphics drawn by the user in the form.
private StorageDoc _storageDoc = new StotageDoc();
public GraphicsForm()
{
InitializeComponent();
}
private Canvas_MouseDown()
{
}
private Canvas_Paint()
{
}
...
Because MainForm is a MDI form, it is easy to use ActiveMdiChild to get the active child form.
class MainForm : Form
{
public void OnSaveButtonClick(object sender, EventArgs e)
{
if(ActiveMdiChild is GraphicsForm g)
Save(g);
}
}
I'm sure this has been answered before but basically, you pass in an instance of the 'data storage' to the new form.
interface ISaveForm
{
void Save();
}
class MainForm
{
private DataStorage _dataStorage;
private ICollection<ISaveForm> _forms = new List<ISaveForm>();
public void OnNew()
{
var subForm = new GraphicsForm(_dataStorage);
subForm.Show();
_forms.Add(subForm);
}
public void OnSave()
{
foreach(var form in _forms)
{
form.Save();
}
}
}
class GraphicsForm : Form,ISaveForm
{
private DataStorage _dataStorage;
public GraphicsForm(DataStorage dataStorage)
{
_dataStorage = dataStorage;
}
public void Save()
{
}
}
So I'm making this small program for my assignment at university and I'm finding it hard to add to my list in my form. Here is my code:
public partial class WorkOutBeam : Form
{
Check checkLib;
public BindingList<ListBox> list;
public WorkOutBeam()
{
InitializeComponent();
}
public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName();
forceItem.Show();
}
public void AddToForceList(string name)
{
list.Items.Add(name);
}
}
NewForceName class below:
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName()
{
InitializeComponent();
}
private void OkButton_Click(object sender, EventArgs e)
{
if (NewForceNames.Text != "")
{
ReferToLibs();
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}
private void NewForceName_Load(object sender, EventArgs e)
{
}
}
So I say to my program, "give me a new force." When it does, it initializes a new form of "NewForceName." I type into a text box and click 'Ok', this starts a public method shown below:
The list is a binding list which refers to the listBox as a data source. However the program tells me that the Items part is inaccessible due to its protection but I don't know how to add it as public. I tried looking in the properties of my listBox but to no avail.
Give this a shot:
public partial class WorkOutBeam : Form
{
Check checkLib;
// public BindingList<ListBox> list; // get rid of this for now
public WorkOutBeam()
{
InitializeComponent();
}
/*public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}*/
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName(this); // pass a reference to this
// instance of WorkoutBeam
forceItem.Show();
}
public void AddToForceList(string name)
{
// we should do some more things here, but let's keep it simple for now
listBox1.Items.Add(name);
}
}
And
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName( WorkoutBeam beam ) // we take a WorkoutBeam instance as CTOR param!
{
InitializeComponent();
workoutBeam = beam;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NewForceNames.Text))
{
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
// DO NOT create new WorkoutBeams every time. Use the original.
/*private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}*/
}
Disclaimer: I did not address each and every problem in this code. This is just enough so that it should "work" as intended.
So I have a Page(Homepage.xaml), when I click a button on this page it opens a prompt(Prompt.xaml).
This prompt is just a Window that I've made and executed using a window.ShowDialog(); method in the Homepage.cs. I've been able to add a little code and when the NO button is clicked the prompt window is closed, now where I'm finding trouble is the YES button.
What I want is for the YES button to take me back to MainWindow, which I've been able to achieve so far, but when it opens the previous Homepage.xaml
is still there and I don't know how to close the Page from the prompt window, if that's even possible?
Another thing is, when the MainWindow opens it kinda pops up, can I make it so that it just lands on the page instead of opening/popping up like a new program?
Heres the Code.
Homepage.cs
public partial class User_Homepage : Page
{
public static Page pager { get; set; }
public User_Homepage()
{
InitializeComponent();
}
public void UserLogoutBtn_Click(object sender, RoutedEventArgs e)
{
var lovmsgb = new Custom_MessageBoxes.LogoutVerification();
lovmsgb.ShowDialog();
}
}
Prompt.cs
public partial class LogoutVerification : Window
{
public LogoutVerification()
{
InitializeComponent();
}
private void YesLogoutBtn_Click(object sender, RoutedEventArgs e)
{
this.Close();
MainWindow window = new MainWindow() { WindowStartupLocation = WindowStartupLocation.CenterScreen};
window.Show();
}
private void NoLogoutBtn_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
And MainWindow.cs just in case
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BtnClickP1(object sender, RoutedEventArgs e)
{
mainFrame.Content = new Page1();
}
private void AdminBtn_Click(object sender, RoutedEventArgs e)
{
mainFrame.Content = new Page3();
}
}
To check what button was clicked you should assign DialogResult in your DialogWindow:
private void YesLogoutBtn_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
this.Close();
}
private void NoLogoutBtn_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}
Now make method to call dialog in your MainWindow and pass it as argument to Page1:
private void AskDialog()
{
Dialog dialog = new Dialog();
if (dialog.ShowDialog() == true)
{
mainFrame.Content = null;
}
else
{
// False action
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
frame.Content = new Page1(AskDialog);
}
In Page1 just call this action after button is pressed:
public User_Homepage(Action askDialog)
{
InitializeComponent();
AskDialog = askDialog;
}
private readonly Action AskDialog;
private void UserLogoutBtn_Click(object sender, RoutedEventArgs e)
{
AskDialog();
}
I've created a new form, in which I have a toolbox. When I press a button in that form, it should relay that information that has been entered by the user(toolboxbox value) to the main form, in which it should say that piece of information in a label.
Since the method to create that username from the toolbox is private, I cannot access it from any other way. Making it public does not seem to make a difference, neither does get,set (from the way I've been trying to atleast).
Picture that may help explaining it:
Code (in which to create user):
namespace WindowsFormsApplication3
{
public partial class Newuserform : Form
{
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
string uname = textboxUsername.ToString();
}
public void Unamecreate()
{
}
}
}
Form1 Code (To receive created user):
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Aboutform form2 = new Aboutform();
form2.Show();
}
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.Show();
}
}
}
you have a lot of options.
One way is to create an event and handle it in the main form.
public partial class Newuserform : Form
{
//the public property
public event EventHandler<string> UnameChanged;
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
if (UnameChanged != null)
UnameChanged(textboxUsername.ToString()); //fire the event
}
}
Now, to "handle" the event, do the following in your main form:
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.UnameChanged += Handler;
formnewuser.Show();
}
private void Handler (object sender, string Uname)
{
// do something wit the new Uname.
}
note: recreating the Newuserform will require to cleanup previous attached resources.
I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.
Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.
Here is code for my windows:
First Window
public partial class Presenation_1 : Window
{
Presentation_2 p2 = new Presentation_2();
MainWindow M = new MainWindow();
public Presenation_1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
}
Second Window
public partial class Presentation_2 : Window
{
Presentation_3 p3 = new Presentation_3();
Presenation_1 p1 = new Presenation_1();
public Presentation_2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p1.Show();
}
}
Third Window
public partial class Presentation_3 : Window
{
Presentation_4 p4 = new Presentation_4();
Presentation_2 p2 = new Presentation_2();
public Presentation_3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p4.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
}
Fourth Window
public partial class Presentation_4 : Window
{
Presentation_3 p3 = new Presentation_3();
MainWindow M = new MainWindow();
public Presentation_4()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
}
Thanks in advance
Don't create your Windows before the button is clicked, you can create them in the event handler:
private void btnForward_Click(object sender, RoutedEventArgs e)
{
var p2 = new Presentation_2();
this.Close();
p2.Show();
}
When you create a windows, you create 2 other windows with
new Presentation_X()
This new windows is automaticaly show and itself open 2 other windows.
You can create this windows once in the Mainwindow (auto hide this one), pass the reference in constructor and not close these windows. Quick example (not tested) :
public partial class Presenation_X : Window
{
private Window preview;
private Window next;
public Presenation_X(Window w1, Window w2)
{
this.preview = w1;
this.next = w2;
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.next.Show();
this.Hide();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.preview.Show();
this.Hide();
}
}