Access Form1 controls from Form2 - winforms

Have two forms-Form1 & Form2.Form1 has a button(btnNew),which opens Form2 on click, and is disabled.I need to enable the button again, only when Form2 is closed.User needs to use Form1 also simultaneously.This code is not enabling the button again. Where am I missing.
In Form1:
private void btnNew_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
btnNew.Enabled = false;
}
public void EnableButton()
{
btnNew.Enabled = true;
}
In Form2:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
f1.EnableButton();
}

You code creates a new Form1 which will be different from the one already running in your application.
You could try adding a reference to Form1 in your Form2 and operate on it's controls that way.
Give form2 a property like:
public Form ParentForm {get; set;}
And assign it form1 in your button click:
Form2 f2 = new Form2()
f2.ParentForm = this;
f2.show();
Then in your closing you should be able to do something like:
this.ParentForm.EnableButton();

Subscribe to your Form2 closing event from within the class that is instantiating it (Form1).
private void btnNew_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Closing += f2_Closing;
f2.Show();
btnNew.Enabled = false;
}
public void f2_Closing(object sender, FormClosingEventArgs e)
{
this.EnableButton();
}
public void EnableButton()
{
btnNew.Enabled = true;
}

Related

Open another Childform over currrent child form in tabpage of form1 Dont want to close or hide previous form in tab

i have single tab page in form1 and a button is placed inside tab to open a childForm(Form2).... Form2 loads pefectly in tabpage1 on buttonclick ..... and now on ChildForm(Form2) i have another button to call For ChildForm(Form3) ...but somehow it doesn't Load..nor throw some error ....well here is my Code
public Form1//
private void LoadForm(object Form)
{
Form childForm = Form as Form;
childForm.TopLevel = false;
int curr = tabControl1.SelectedIndex;
TabPage tbp = tabControl1.TabPages[curr];
tabControl1.TabPages.Contains(tbp);
tbp.Controls.Add(childForm);
childForm.WindowState = FormWindowState.Maximized;
childForm.Show();
Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
LoadForm(new Form2());
}
public void OpenForm();
{
LoadForm(new Form3());
}
Form2//
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.OpenForm(new Form3());
}
}enter image description here
}
public Form1//
private void LoadForm(object Form)
{
Form childForm = Form as Form;
childForm.TopLevel = false;
int curr = tabControl1.SelectedIndex;
TabPage tbp = tabControl1.TabPages[curr];
tabControl1.TabPages.Contains(tbp);
tbp.Controls.Add(childForm);
childForm.WindowState = FormWindowState.Maximized;
childForm.Show();
childForm.BringToFront();
Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
LoadForm(new Form2());
}
public void OpenForm();
{
LoadForm(new Form3());
}
Form2//
static Form1 f1;
Public Form2( Form1 refer)
{ InitializeComponent();
f1 = refer;
}
private void button1_Click(object sender, EventArgs e)
{
f1.LoadForm();
}

I get confused with Form.Close()

So I link delegate from Form2 to This_Hide() fuction in Form1, then i link delegate from Form3 to delegate in Form2(which link to function in Form1 as I said) but I get confused that it still work although Form2 is CLOSED when I making instance of Form3. If Form.Close() method closes Form2 and dispose it how delegate from Form3 can still call function(delegate) from Form2..?
namespace TestingMachine
{
public delegate void FxDelegate();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 fr2 = new Form2();
fr2.Delegate_Father_Hide = new FxDelegate(This_Hide);
fr2.Show();
}
public void This_Hide()
{
this.Hide();
}
}
}
namespace TestingMachine
{
public partial class Form2 : Form
{
public FxDelegate Delegate_Father_Hide;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form3 fr3 = new Form3();
fr3.Delegate_Hide_Grandfather = new FxDelegate(Hide_Grandfather);
fr3.Show();
this.Close();
}
public void Hide_Grandfather()
{
Delegate_Father_Hide();
}
}
}
namespace TestingMachine
{
public partial class Form3 : Form
{
public FxDelegate Delegate_Hide_Grandfather;
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Delegate_Hide_Grandfather();
}
}
}

Cannot set the value of a global variable

Im pretty new to programming, but im very eager to get more into this stuff, and in particular, c#. I have a made some code for an autotyper (spam bot if i may), only to be used as a goal for me to create. Essentially, what i want the program to do, is as following:
When i start my Form1, the global variable "_timerValue" is set to
1000
When i hit Start button, the text from the textbox on will be sent at
the interval of "_timerValue"
When i hit the Speed button, Form2 will show.
When i hit very fast, "_timerValue" is set to 5000 (testing purposes)
Form1 code:
public partial class Form1 : Form
{
static class TimerIntervalValue
{
Form2 f2 = new Form2();
TimerIntervalValue = f2._timerValue;
}
public Form1()
{
InitializeComponent();
f2._timerValue = "1000";
}
public void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(f2._timerValue);
timer1.Interval = Convert.ToInt32(f2._timerValue);
if (timer1.Enabled == false)
{
timer1.Enabled = true;
textBox1.Enabled = false;
button1.Text = ("Stop");
}
else if (timer1.Enabled == true)
{
timer1.Enabled = false;
textBox1.Enabled = true;
button1.Text = ("Start");
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
Form2 code:
public partial class Form2 : Form
{
public string TimerValue;
public string _timerValue
{
get { return TimerValue; }
set { TimerValue = value; }
}
public Form2()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._timerValue = "5000";
}
}
I originally tried to create a Form2 instance just under "InitializeComponent();" in Form1, but that didnt seem to be accessible through the other funtions.
I just know its something very simple like im using the wrong class to create the Form2 instance or something like that ...
Anyway, thank you in advance
Just mark TimerValue and _timerValue as static. Then you don't need to use
Form2 f2 = new Form2(); or Form2 frm2 = new Form2();
anymore. In Form 1, just use Form2._timerValue instead of f2._timerValue. In Form 2, just change:
public void button1_Click(object sender, EventArgs e)
{
_timerValue = "5000";
}

Interaction between forms

I am using visual studio 2012 (windows form application) and I have two forms, one with a label and the other with a button. I want it so that when you click the button the label on the other form goes up by one. I already have:
Label1 = Label1 + 1
I just need to know how to make the connection with the two forms. Maybe call a function?
Btw I am new to the program and script so in simple terms plz.
Here is a sample I create for you. Add Fomr2 Like this:
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1.Instance.Controls.Find("label1", true).First().Text = "Some thing";
}
}
And Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Instance = this;
}
private static Form1 _Instance;
public static Form1 Instance
{
get { return _Instance; }
set { Instance = value; }
}
private void button1_Click(object sender, EventArgs e)
{
new Form2().Show();
}

Close form after raising an event

I have a mainform that opens a StartForm at some point:
private void TestChosen(object sender, TestChosenEventArgs e)
{
var frm = new TestStartForm(e.Test, StartTest);
frm.ShowDialog();
}
On this StartForm I have a button to actually start the test.
public TestStartForm(Test test, StartTestHandler startTestHandler)
{
InitializeComponent();
_test = test;
OnStartTest = startTestHandler;
}
public delegate void StartTestHandler(object sender, StartTestEventArgs e);
public event StartTestHandler OnStartTest;
public void InvokeOnStartTest(StartTestEventArgs e)
{
StartTestHandler handler = OnStartTest;
if (handler != null) handler(this, e);
}
private void btnStart_Click(object sender, EventArgs e)
{
InvokeOnStartTest(new StartTestEventArgs(_test));
Close();
}
The problem I'm facing is that the StartForm stays open untill the work of the StartTestHandler is completely done.
In the StartTestHandler another form is opened with the actual test.
Is there a way to force the StartForm to close without waiting for the test to be finnished?
EDIT
As #cowboydan suggested I've used BeginInvoke to show the form.However, I had to do it for the StartForm as well as the actual TestForm before it worked properly.
private void TestChosen(object sender, TestChosenEventArgs e)
{
BeginInvoke((Action)delegate
{
var frm = new TestStartForm(e.Test, StartTest);
frm.ShowDialog();
});
}
private void StartTest(object sender, StartTestEventArgs e)
{
BeginInvoke((Action)delegate
{
var frm = new TestForm(e.Test);
frm.ShowDialog();
});
}
You could use BeginInvoke which would launch StartForm in a separate thread from the ThreadPool. This is a fairly common practice.

Resources