C# .xaml transfer an Object from one window another - c#

I have a form "SprocketOrderForm.xaml" window. I push a button to open a new window, "SprocketForm.xaml". In that window when I click submit it will create a new "Aluminum" object populated by the textboxes in "SprocketForm". Now I need to get that new object back into a listbox in "SprocketOrderForm". I've been stuck on this for a while now.
"SprocketOrderForm.xaml"
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (rbAluminum.IsChecked==true)
{
SprocketForm sf = new SprocketForm("Aluminum");
sf.Show();
lisbOrderList.Items.Add(sf.objAlum);
}
}
"SprocketForm.xaml"
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (sproc=="Aluminum")
{
AluminumSprocket Steel = new AluminumSprocket(int.Parse(txtbItemID.Text), int.Parse(txtbNumberOfItems.Text), int.Parse(txtbNumberOfTeeth.Text));
objAlum = Steel;
this.Close();
}
}

Related

Closing multiple forms C#

I'm currently working with C# windows forms.
I got like 40 cs files, and when the applications works like :
Pressing a button -> Opens new form upon the first one and pressing another button opens another form upon the one before.
Now, whenever I click multiple forms none of the others are closing itself when I'm pressing different buttons, they all stay on the background.
Now, if I use this.Close(); its working with 1, but i got like 40 cs files and it's hard to compile them all..
looking for any suggestion ?
Thank you guys for any sort of help !
public partial class Costumers_Orders : Form
{
public Costumers_Orders()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
NewCostumer mm = new NewCostumer();
mm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Remove_Customer mm = new Remove_Customer();
mm.Show();
}
private void Costumers_Orders_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Show_Edit_Customer mm = new Show_Edit_Customer();
mm.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Orders_report mm = new Orders_report();
mm.Show();
}
}
Assuming you only ever want one form open at a time, and that "Costumers" is the main form and can't be closed:
private void button1_Click(object sender, EventArgs e)
{
ShowForm(new NewCostumer());
}
private void ShowForm(Form newForm)
{
List<Form> forms = new List<Form>();
foreach (Form frm in Application.OpenForms)
{
if (!(frm is Costumers))
{
forms.Add(frm);
}
}
foreach (Form frm in forms)
{
frm.Close();
}
newForm.Show();
}
There are other ways you could do this as well.

Clear Data in chart c#

I want to create a delete button so when i press the delete button i can key in a new set of data and let it generate a new chart
private void button2_Click(object sender, EventArgs e)
{
chart1.Series.Clear();
}
and I had tried
private void button2_Click(object sender, EventArgs e)
{
chart1.Series["Data1"].Points.Clear();
chart1.Series["Data2"].Points.Clear();
chart1.Series["Total"].Points.Clear();
}
What I have did so far

Wpf detect when window is closed from a different window

private void newmail_Click(object sender, RoutedEventArgs e)
{
Nieuweemail _nieuweEmail = new Nieuweemail(_username);
_nieuweEmail.Show();
}
When that window is closed, I want to call a function inside my main window that will execute.
_nieuweEmail.Closed += setContent();
I could do this if I could call it in the window that is going to be closed. But that's not the case. How can I detect this?
Assuming setContent and newmail_Click are both methods in your main window...
private void newmail_Click(object sender, RoutedEventArgs e)
{
Nieuweemail _nieuweEmail = new Nieuweemail(_username);
_nieuweEmail.Closed += SetContentHandler;
_nieuweEmail.Show();
}
private void SetContentHandler(object sender, EventArgs e)
{
setContent();
}

Find method name which opened new window (c# wpf)

I'm trying to find a way to get the string name of the method call which pops up a new window. I have three button click event handlers which will open the new window but I need to know which called the .Show();
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
I don't want to have to have three separate windows! is there an opening event handler parameter which I can fetch the caller from?
well, you can simply add a public variable at MobilityPortfolioSettings class and set its value in each method, ex: in buttonSettingsPortfolio1_Click add MobilityPortfolioSettings.Variable = 1 and so on.
Here
Console.write(triggeredBy); you can output the value by logging to file or some other way . This value will indicate which path your code took.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio1_Click");
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio2_Click");
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio3_Click");
}
private Open(string triggeredBy){
Console.write(triggeredBy); // You can write to file or output in some different way here.
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
Try this:
Cast sender as button and then get it's name.
Change the MobilityPortfolioSettings constructor so that it needs a string parameter.
Pass the button name to the constructor.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
string buttonName = "";
if (sender is Button)
buttonName = ((Button)sender).Name;
Window settingsWindow = new MobilityPortfolioSettings(buttonName);
settingsWindow.Show();
}
BTW use Window as variable type instead of var.
Cheers

How to make a MessageBoxButton move?

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.

Categories

Resources