Pass variable from window to page [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I would like to pass variable which I insert into textbox in Window to Page in WPF application I only found how could I do it other way.
Basically I need app to prompt for password which I need to use in different page.
I call window from page like this:
Password_Prompt PassWindow = new Password_Prompt();
PassWindow.Show();
It's just window with a textbox and a button and after I input a password and click ok, I would like to send the password into variable on page from I called the window.

The most efficient way to achieve this would be to raise an event when you click the button on the window and subscribe to it from the page.
Window
public event EventHandler<string> PasswordInput;
// the function you are going to call when you want to raise the event
private void NotifyPasswordInput(string password)
{
PasswordInput?.Invoke(this, password);
}
// button click event handler
private void OnButtonClick(object sender, RoutedEventArgs e)
{
// get the password from the TextBox
string password = myTextBox.Text;
// raise the event
NotifyPasswordInput(password);
}
Page
...
Password_Prompt PassWindow = new Password_Prompt();
// add this part to subscribe to the event
PassWindow.PasswordInput += OnPasswordInput;
PassWindow.Show();
...
// and the method to handle the event
private void OnPasswordInput(object sender, string password)
{
// use the password from here
}

You could add a property to PassWindow.xaml.cs that returns the value of the Text property of the TextBox or PasswordBox:
public string Password
{
get { return _passwordBox.Password; }
set { _passwordBox.Password = value; }
}
XAML:
<PasswordBox x:Name="_passwordBox" />
You could then retrieve the password using this property. You probably also want to block the calling thread until the window has been closed. Then you should call ShowDialog() instead of Dialog():
Password_Prompt PassWindow = new Password_Prompt();
PassWindow.ShowDialog();
string password = PassWindow.Password;
The other option would be to handle the Closed event:
Password_Prompt PassWindow = new Password_Prompt();
EventHandler handler = null;
handler = (s, e) =>
{
string password = PassWindow.Password;
PassWindow.Closed -= handler;
};
PassWindow.Closed += handler;
PassWindow.Show();

Related

How to Delete selected control in Winforms C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i need that when you click a garbage button, the "Selection mode" turn on (A bool variable became true) and when you click that control (let's say a button) the application "selects it" and with another button, (Ok button) the application should knew Wich controls you clicked, and then, delete them, the problem i Have is in the recognize witch controls you selected
update
For "selection" I mean that the app knows which control you clicked
You can implement the required functionality as follows. The idea is simple, to keep track of our last selection we have a "previousSelection" variable of type "Control" in our class. We can even use a list of type "Control" to keep track of multiple selections.
There's a toggle button to enable / disable "Garbage Mode" which maps to "IsGarbageModeEnabled" field of type "bool". Then we have an "InitControlsRecursiveMethod(ControlCollection collection)" which takes in a collection of controls, to which we would attach an event handler, which is "GenericClickHandler(Control c)" in our case. This handler simply updates the "previousSelection" on each button click.
And lastly we have our button "ConfirmDeletionBtn", upon clicking which, we would check whether "GarbageMode" is enabled or not, if it is, we would have some basic validation to check whether the control being deleted isn't our "Delete" or "GarbageModeToggle" button itself (this could cause trouble if user double clicks the 'Delete' button). Afterwards, it would remove / dispose the control that is to be deleted.
public partial class FormName : Form
{
//To keep track of the previously selected control (i.e. to be deleted)
private Control previousSelection { get; set; }
//To keep track of whether "Garbage Mode" is enabled or disabled
private bool IsGarbageModeEnabled { get; set; }
//Constructor
public FormName()
{
InitializeComponent();
IsGarbageModeEnabled = false;
previousSelection = new Control();
//Attach a generic click handling event to each control to
//update "previousSelection" with each click.
//Similar logic can be used for other events as well
//(e.g. GotFocus, which might even accomodate control selection via keyboard).
InitControlsRecursive(this.Controls);
}
//This attaches the GenericClickHandler(Control c) to each control on the form.
private void InitControlsRecursive(Control.ControlCollection collection)
{
foreach (Control c in collection)
{
c.MouseClick += (sender, e) => { GenericClickHandler(c); };
InitControlsRecursive(c.Controls);
}
}
//The generic click handling event we're using to update "previousSelection".
private void GenericClickHandler(Control c)
{
previousSelection = c;
}
//By clicking the confirm deletion / OK button, we would delete the last selected control.
private void ConfirmDeletionBtn_Click(object sender, EventArgs e)
{
if(IsGarbageModeEnabled == true)
{
if(previousSelection != ConfirmDeletionBtn || previousSelection != ToggleGarbageModeBtn)
{
this.Controls.Remove(previousSelection);
previousSelection.Dispose();
}
}
}
//This is used to enable/disable Garbage Mode. Controls can be deleted only once it is enabled.
private void ToggleGarbageModeBtn_Click(object sender, EventArgs e)
{
IsGarbageModeEnabled = !IsGarbageModeEnabled;
}
}
Further Reading:
Handling a click for all controls on a form
Removing a control from a form
Firstly you need somewhere to keep track of the controls that were "selected" (clicked). So add this to the form's codebehind:
List<Control> _itemsToDelete = new List<Control>();
And you need a flag to indicate whether the user has activated garbage mode:
bool _garbageMode = false;
To activate garbage mode:
async void GarbageMode_Click(object sender, EventArgs e)
{
_garbageMode = true;
}
Now when they "select" a control you add it to the list:
async void Control_Click(object sender, EventArgs e)
{
if (_garbageMode)
{
_itemsToDelete.Add((Control)sender);
}
}
Then to delete
foreach (var control in _itemsToDelete)
{
this.Controls.Remove(control);
control.Dispose();
}
_itemsToDelete.Clear();

How to close a form that opened another form using ShowDialog() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
so i have this frmUser but to close this i have to call frmPass (which i called using ShowDialog()instead of Show()) that will confirm first if the user is an admin but the problem is when i execute the codes below
frmUser us = new frmUser(lblEID.Text, lblAdmin.Text, lblType.Text);
us.Hide();
this.Hide();
frmPass only hides itself and not along with frmUser. Also here's my code calling frmPass from frmUser
frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module");
pass.ShowDialog();
In your password form, you should set DialogResult property of that form. In example; on your frmPass, when user clicks on button Ok, your code checks if username and password are valid. If everything is ok set dialog result as OK, like this:
private void btnOk_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Ok;
}
that will close form.
In frmUser you can, if needed, check what is DialogResult of frmPass form, like this:
frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module");
DialogResult dr = pass.ShowDialog();
if (dr == DialogResult.Ok)
{
//do code if form is closed with OK dialog result
}
else
{
// some warning to user...
}
The problem is that you are trying to hide the wrong instace of frmUser. I assume that the first code passage is inside the class frmPass. There you create a new instance which you try to hide. But you want to hide the original instance.
As Nino already suggested I also would suggest to use the DialogResult to check whether the admin is confirmed. But may be you have already a different mechanism. Nevertheless you need to place the hiding code line into the class frmUser after the call of pass.ShowDialog();
private void button1_Click(object sender, EventArgs e)
{
formPass pass = new formPass();
pass.ShowDialog();
if(AdminIsConfirmed)
{
this.Hide();
}
}
leave the other this.Hide() in the frmPass to close that.
EDIT
Another possibility would be to pass the frmUser instance into the constructor of frmPass and have a variable of type frmUser in frmPass. You just have to overload it :
public partial class formPass : Form
{
formUser us;
public formPass(/* all that stuff that you have plus*/, formUser us)
{
InitializeComponent();
this.us = us;
}
call frmPass like this now:
frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module", this);
now you can just remove the line:
frmUser us = new frmUser(lblEID.Text, lblAdmin.Text, lblType.Text);
and the whole thing will work and hide properly.

Label don't change value when I choose one CheckBox [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello for everyone I am new here,sorry if this post doens't match with at all with how should be,because It's my first post this.
I am wanting to change the value of the Label when I choose one CheckBox,but It's seems that the Label just changes when I click in the Label.
I was looking for see if the Label had some Events for make work it but I didn't found one.
The Label it's in another groupBox,It's seems like that :
In one GroupBox has 3 CheckBox and i want that when i choosing one or all from that CheckBox will change the value of the Label in the another GroupBox.
Please guys,help me here,I am really wanting to understand why It's not working.
You can just register an event handler for the CheckChanged event and change the label's caption in the event handler :
public partial class Form1 : Form
{
public MyForm()
{
InitializeComponent();
myCheckbox.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox1.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox2.CheckedChanged += new System.EventHandler(this.checkedChanged);
}
private void checkedChanged(object sender, EventArgs e)
{
myLabel.Text = "Some text";
}
}
use this code here, but change the event to the control you're looking for
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your value here";
}
you can do this by double clicking on the radio button control and then it will generate the event for you and then set your code as per the example here.

calling a method from another window form closing event. c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can i call method of a window form from another form closing event ?
Suppose second for is getting closed and i want to call method of first form when second get closed to update some changes on first window form.
You can add an event handler to the form_closing event in the first form and handle it accordingly.
Somewhere in form1
form2.Form_Closing += yourhandler;
This assumes that form 2 has a a control called TextBox1, when form 2 closes the lambda expression will be called and transfer data to form 1.
public partial class Form1 : Form
{
private Form2 openedForm2 = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Not sure if you would want more than 1 form2 open at a time.
if (this.openedForm2 == null)
{
this.openedForm2 = new Form2();
//Here is your Event handler which accepts a Lambda Expression, code inside is performed when the form2 is closed.
this.openedForm2.FormClosing += (o, form) =>
{
// this is Executed when form2 closes.
// Gets text from Textbox1 on form2 and assigns its value to textbox1 on form 1
this.textBox1.Text = ((Form2)o).Controls["TextBox1"].Text;
// Set it null so you can open a new form2 if wanted.
this.openedForm2 = null;
};
this.openedForm2.Show();
}
else
{
// Tells user form2 is already open and focus's it for them.
MessageBox.Show("Form 2 is already open");
this.openedForm2.Focus();
}
}
}
Pass a reference from your first form to your second. Say you create your second form this way (From your first form):
Form2 frm2 = new Form2();
frm2.referenceToFirstForm = this
In your second form you should have this:
public Form1 referenceToFirstForm
Then in your OnClosing event you can reference referenceToFirstForm

Why doesn't my second form show up? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I made a timer, but when I press a button nothing happens.
My code is:
private void button3_Click(object sender, EventArgs e)
{
Instellingen form = new Instellingen();
form.Show();
}
Instellingen.cs code:
public partial class Instellingen : Form
{
public Instellingen()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("text", "text");
}
}
}
The button3 event doesn't even fire (confirmed by adding a breakpoint, it doesn't get there)
Check if the button's event realy connected to the method button3_Click.
Put a breakpoint in the first line of button3_Click and check if the code get there.
Make sure your button's click event is button3_Click. Probably your button's click event is empty.

Categories

Resources