Wpf detect when window is closed from a different window - c#

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();
}

Related

C# .xaml transfer an Object from one window another

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();
}
}

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.

C# - Form2 value to Form1

I'm having trouble passing values ​​entered in form2(citacao) to form1(principal).
Principal.cs (form1)
richEditControl1.Document.AppendText(citacao.valor_edit[0]);
Citacao.cs (form2)
public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
valor_edit[0] = memoEdit1.Text;
valor_edit[1] = comboBox1.SelectedItem.ToString();
valor_edit[2] = textEdit1.Text;
}
But when I click the button nothing happens , the values ​​are not inserted into the richedit I like it.
I already have this on form (Pass DataGrid to ComboBox)
Form1 (principal)
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
citacao cita = new citacao(this);
cita.Show();
}
form2(citação)
public citacao(principal gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
{
comboBox1.Items.Add(row.Cells[0].Value.ToString());
}
comboBox1.SelectedIndex = 0;
}
let's see whether I understood your situation :)
declare your variable in Form 1 as a class variable
private citacao cita;
then initialize it in the button press event
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
cita = new citacao(this);
// subscribe to the closing event
cita.FormClosing += form_FormClosing;
cita.Show();
}
// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
// BUT! you have to use the variable name!
richEditControl1.Document.AppendText(cita.valor_edit[0]);
}
EDIT:
Ok after looking at the entire code:
please remove the button3! and this entire code:
private void button3_Click(object sender, EventArgs e)
{
cita = new citacao(this);
richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}
The function AppendText probably needs a string as parameter and you give the entire array!
If you subscribe to the closing event in Form1 / principal and also implement
the event, your data will be transmitted automatically as soon as the Form 2 disappears from the screen :)

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

WPF MediaElement how to resume playing from where it pause?

i have 2 buttons , Play and Pause , When i click pause , the music stops and when i click Play it starts the audio from beginning . I want to do it like when i press Play , it resume from where i have stopped.
private void PlayAudio()
{
McMediaElement.LoadedBehavior = MediaState.Manual;
McMediaElement.Source = new Uri("../../SingAlong/Food Fit For A King/old king cole.mp3", UriKind.RelativeOrAbsolute);
McMediaElement.Play();
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
PlayAudio();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
McMediaElement.Pause();
}
Your PlayAudio() method reloads the media file when you set the Source property. This causes your object to play the newly loaded media from the beginning when you call Play(). Instead of doing this in the event handler button1_Click_1, you should call the Play() method only:
private void button1_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.Play();
}
This worked for me..
private void button1_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.LoadedBehavior = MediaState.Pause;
}
private void button2_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.LoadedBehavior = MediaState.Play;
}

Categories

Resources