changing a button with a checkbox on another form. c# - c#

I am trying to change a buttons visible status with a checkbox that is on another form for example;
when the checkbox checked state changes on form 2 than the visible property for button 1 on form1 changes. If someone could point me in the right direction i would appropriate it thank you.

Define event on Form2. E.g.
public event EventHandler StateChanged;
Raise this event when checkbox checked state changes:
private void Checkbox_CheckedChanged(object sender, EventArgs e)
{
if (StateChanged != null)
StateChanged(this, EventArgs.Empty);
}
Subscribe to this event in Form1
form2.StateChanged += Form2_StateChanged;
Change a button in this event handler:
private void Form2_StateChanged(object sender, EventArgs e)
{
button.Visible = !button.Visible;
}

Normally when I do something like this I add a method that calls it.
Form1 with the button
public void SetVisibility(bool visible)
{
Button.Visible = visible;
}
Form 2 With CheckBox
CheckBox_CheckChanged(object sender, EventArgs e)
{
Form1.SetVisibility(CheckBox.Checked);
}
Adding a public method on your form also enables you to control it from others forms. Not sure what the best answer is, thats up to you.
EDIT:
Not related to the question but...
On Form 2
var form1Control = new Form1();
form1Control.SetVisibility(true);

Related

Hide a Form when a new Form is shown and show it again when the previous Form is closed

How can I hide the Main Form when any of the Buttons that create and show another Form is clicked?
Closing the Form that has been opened from the Main Form, should cause the Main Form to show up again.
This is the code for the Click event of any of the buttons in the Main Form.
private void button7_Click(object sender, EventArgs e)
{
publisher p = new publisher();
p.Show();
}
You can add a handler to the FormClosed event of the For you're about to show, Hide() the current Form, then, when the FormClosed event is raised, remove the handler and Show() the current Form again.
The sender object is the object instance that raised the event, so you can cast sender to Form - (sender as Form) - and remove the handler you added before. You can use this event handler to handle the FormClosed event of other Forms that need the same treatment.
If you inspect the sender object, you'll see that the Form instance is your publisher instance, IsDisposed is still false and IsHandleCreated is still true, so you can still interact with it.
For example, you could read the value returned by public (standard or custom) properties, in case it's needed.
private void button7_Click(object sender, EventArgs e)
{
publisher p = new publisher();
p.FormClosed += OnFormClosed;
this.Hide();
p.Show();
}
private void OnFormClosed(object sender, EventArgs e)
{
(sender as Form).FormClosed -= OnFormClosed;
this.Show();
}
to hide a form:
private void button7_Click(object sender, EventArgs e)
{
publisher p = new publisher();
p.Show();
this.Hide();
}

How to fire click event of panel programmatically?

I have a winforms app in vs2010 and a panel whose click event I would like to fire programatically. How do I do this? Button has a PerformClick but I cannot find the same in Panel.
Your panel's Click event is going to be attached to an event handler, right?
Then just call that event handler from the button's click event handler:
public void Panel1_Click(object sender, EventArgs e)
{
//Do whatever you need to do
}
public void Button1_Click(object sender, EventArgs e)
{
//Do anything you need to do first
Panel1_Click(Panel1, EventArgs.Empty);
}
The effect will be the same as clicking on the panel.

Why it is not Possible to call textBox1_KeyPress() event from some Button Click event.?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
MessageBox.Show("called btn 0 click..");
KeyPressEventArgs e0 = new KeyPressEventArgs('0');
textBox1_KeyPress(sender, e0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("called txtbox_keypress event...");
}
}
Am sorry if this is a silly question,I have just started to learn windows forms, I still find material on the internet confusing.I want to implement calculator. So when number button is pressed it should be filled in textbox. So I thought calling textBox1_keypress() event from button click event would work??? but its not working,
I can manually write the logic in button click event to fill text in text box but if i do so, i have to do the same thing in button1_KeyPress event too. so it would be duplication of code right??...so i thought solution was to call textBox1_KeyPress() event from both button click event and button key press event...but its not working .So what should i do???..is there any other approach which should i follow.
so it would be duplication of code right??
Yes, it would be. So you can do
private void btn0_Click(object sender, EventArgs e)
{
CommonMethod(e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
CommonMethod(e);
}
private void CommonMethod(EventArgs e)
{
//Your logic here.
}
The TextBox KeyPress event handler (textBox1_KeyPress) is called after the user presses a key. The KeyPressEventArgs parameter includes information such as what key was pressed. So calling it from your btn0_Click method isn't going to set the text for the TextBox.
Rather, you want to (probably) append whatever number the user pressed to the text already present in the TextBox. Something like
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
might be closer to what you're trying to accomplish.
You could put the logic in an extra function like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
NumberLogic(0),
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// I don't know right now if e contains the key pressed. If not replace by the correct argument
NumberLogic(Convert.ToInt32(e));
}
void NumberLogic(int numberPressed){
MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
}
}
You don't want to tie the events together like that.
A key-press is one thing, handled in one way.
A button click is something totally different and should be handled as such.
The basic reason is this,
The button doesn't know what number it is, you need to tell it that.
A key-press on the other hand, knows what number was pressed.
If you REALLY want to, for some reason, you could use SendKeys to trigger your key-press event in a round-about way, from the button.
SendKeys.SendWait("0");
I can suggest to you to use an Tag Property of the Buttons. Put in it the value of each button in Design mode or in Constructor, create one button event handler for all buttons and use Tag value:
Constructor:
button1.Tag = 1;
button2.Tag = 2;
button1.Click += buttons_Click;
button2.Click += buttons_Click;
Event hadler:
private void buttons_Click(object sender, EventArgs e)
{
textBox1.Text = ((Button)sender).Tag.ToString();
}

Put focus back on previously focused control on a button click event C# winforms

I have made a custom Number Keypad control that I want to place in my winform application. All of the buttons have an OnClick event to send a value to the focused textbox in my form where I have placed my custom control. Like this:
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null && focusedCtrl is TextBox)
{
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
focusedCtrl is supposed to be set on the MouseDown event of the button like this:
private void btnNum1_MouseDown(object sender, EventArgs e)
{
focusedCtrl = this.ActiveControl;
}
where this.ActiveControl represents the active control on the form.
My problem is that the button always receives the focus before the event detects what the focused control was previously. How can I detect which control had the focus before the button got the focus? Is there another event I should be using? Thanks in advance!
EDIT: Also, I would rather not use the GotFocus event on each textbox in the form to set focusedCtrl since that can be tedious and because I would like to have all the coding of my custom control be in the control itself and not on the form where it is placed. (I will do this, though, if there is no other practical way to do what I am asking)
Your requirement is fairly unwise, you'll want some kind of guarantee that your button isn't going to poke text into inappropriate places. You really do need to have the form co-operate, only it knows what places are appropriate.
But it is not impossible, you can sniff at input events before they are dispatched to the control with the focus. In other words, record which control has the focus before the focusing event is fired. That's possible in Winforms with the IMessageFilter interface.
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing your existing buttons.
using System;
using System.Windows.Forms;
class CalculatorButton : Button, IMessageFilter {
public string Digit { get; set; }
protected override void OnClick(EventArgs e) {
var box = lastFocused as TextBoxBase;
if (box != null) {
box.AppendText(this.Digit);
box.SelectionStart = box.Text.Length;
box.Focus();
}
base.OnClick(e);
}
protected override void OnHandleCreated(EventArgs e) {
if (!this.DesignMode) Application.AddMessageFilter(this);
base.OnHandleCreated(e);
}
protected override void OnHandleDestroyed(EventArgs e) {
Application.RemoveMessageFilter(this);
base.OnHandleDestroyed(e);
}
bool IMessageFilter.PreFilterMessage(ref Message m) {
var focused = this.FindForm().ActiveControl;
if (focused != null && focused.GetType() != this.GetType()) lastFocused = focused;
return false;
}
private Control lastFocused;
}
Control focusedCtrl;
//Enter event handler for all your TextBoxes
private void TextBoxesEnter(object sender, EventArgs e){
focusedCtrl = sender as TextBox;
}
//Click event handler for your btnNum1
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null){
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
you have an event called lostFocus you can use
button1.LostFocus +=new EventHandler(dataGridView1_LostFocus);
and in the event:
Control lastFocused;
void dataGridView1_LostFocus(object sender, EventArgs e)
{
lastFocused = sender as Control;
}
in that way you can always know what is the Control that was focused previously
now, correct me if i'm wrong, but you do it for the SendKeys.Send("1"); to know which textBox need to receive the number. for that you can use GotFocus event and register only the textBoxs to it.
you can also do what windows is doing and use just one textbox like here:
if it's fits your needs
What about using this with the parameter forward = false?
Control.SelectNextControl Method
You'd probably call it on your "custom Number Keypad control".

Closing a form and doing something on return to another form

I have a mainForm and a saveForm.
I never close the mainForm, just let the saveForm appear over the top.
When i close the saveForm, I want a piece of code to run on returning to mainForm.
What is the best way to achieve this?
In addition to #benPearce's answer, if you are content to have saveForm appear modally, then you can just call:
So in the mainForm, I am assuming you have a Save button (let's call it btnSave) of some kind that brings up saveForm, right? Right. So double click on that Save button and Visual Studio will create an event handler for you. Type in the code below.
private void btnSave_Click(object sender, EventArgs e)
{
saveForm sf = new SaveForm();
if (sf.ShowDialog() == DialogResult.OK)
{
// do your thing
}
}
Of course, you have to make sure that the saveForm is setting the DialogResult. For instance, assuming you have an OK button in the saveForm that is supposed to close the saveForm... In the Click event for the OK button you would do this:
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
In mainForm, subscribe to the FormClosed event on the saveForm, put your code in the event handler for this event
void saveForm_FormClosed(object sender, FormClosedEventArgs e)
{
/// code here
}

Categories