In my main form I have this method to stop a timer:
public void resettimer()
{
ShutdownTimer.Stop();
}
Now I need to activate this methode when a buttom was clicked on another form. This is how i coded it:
private void btn_doorgaan_Click(object sender, EventArgs e)
{
//bib_main is how the first form is called
Bib_main MainMenu = new Bib_main();
MainMenu.resettimer();
}
This is also what I find on the internet but nothing happens, the methode never triggers.
Can u guys tell me what i am doing wrong or if their is a better way to stop the timer in the other form when the buttom is clicked?
When you create the new form, you should include a reference to your existing form (the one with the method). Than with that reference you can call the method, instead of creating a new instance of Bib_main.
EDIT:
Constructor of the form should look like this:
private Bib_Main _mainForm;
public SomeForm(Bib_Main mainForm)
{
// store the reference to a field
_mainForm = mainForm;
}
When creating the new form in the main form:
SomeForm newForm = new SomeForm(this);
Then you can just call it like:
_mainForm.resettimer();
You can't just make a new Bib_main object and call the method on it. You need to call the method on your existing Bib_main.
Related
I am currently making an rpg using Winforms for a school project. However my knowledge on classes is so limited that I'm having trouble making a proper class that takes in data from 1 form, is used in the second form, then sent back to the first form.
The process I'm trying to accomplish is like this:
main form opens a second form that displays items in a listbox.
1
when you select an item and press a button to use it, the items effects are applied.
2
The data for the effect is in the first form where many other calculations are made with the same data.
3
I keep running into the problem of making a new object of a class and the data from the first form is reset. How would I go about either using an existing object from the first form, or creating a reference class maybe?
This is the function I want to run on the first form when the button on the second form is clicked.
public void SmallPot()
{
currentPHP += pHP * .25;
if (newPHP > pHP)
{
newPHP = pHP;
}
pHPBarUpdate = (int)(newPHP / pHP * 377);
pnlCurrentPHP.Width = pHPBarUpdate;
newPHP = currentPHP;
}
Expected:
When I click the use button on the popup form it closes and the items effects are displayed on the Main form.
What Happens:
Since I create a new object of form one in form two, all my variables are reset to 0 before the calculation, resulting in nothing happening after the second form closes.
I will give you a general guideline to implement a solution based on event definition and raising
Let's start from your second form where you need to communicate to the first form the event
public class Form2 : Form
{
// start creating the delegate type
public delegate void OnItemSelected(string itemName);
// declare the public event that this form will raise
public event OnItemSelected ItemSelected;
protected void cmdItemUse_Click(object sender, EventArgs e)
{
// When the user clicks to select an item....
string itemName = GetItemSelectedFromList();
// Check if someone is interested in this item selection
if(ItemSelected != null)
ItemSelected.Invoke(itemName);
}
}
Now we change something in the first Form. We need to create the second form and before displaying it we subscribe to the event exposed by the second form
public class Form1 : Form
{
... other stuff....
protected void cmdOpenSelection_Click(object sender, EventArgs e)
{
using(Form2 frm = new Form2())
{
// Subscribe the event giving it a method inside this class
// that doesn't return anything and receives a string
// as required by the delegate type of the event
frm.ItemSelected += handleItemSelection;
frm.ShowDialog(); // frm.Show();
}
}
private void handlerItemSelection(string itemName)
{
// This method is a custom Event handler and inside Form1
// will be called by Form2 through the Invoke on the event variable
}
}
In the example above I choose to pass a simple string, but of course you could pass anything including a reference type like an instance of a class containing all the info
required by the Item selection.
I am having a problem controlling a textbox. I need to change a value from outside of Form1.cs where the textbox is located for this I have found this snippet.
public void UpdateText(string newValue)
{
torrent_name0.Text = newValue;
}
this allows me in theory to control the textbox outside of Form1.cs, but here comes the problem, every time I want to access it I create a new instance of Form1 instead of using the old one and refreshing it.
Form1 frm = new Form1();
frm.UpdateText("aaaaaaaaaaaa");
frm.Show();
am I missing something? is there a better way to do this? I have tried multiple ways to update the new form but got nowhere.
Bokker,
You will have to have a reference to the singular form1 to which all things refer.
If this is a child form, then as Aybe commented, create a public member of your mainform and name it something.
Public Form myForm1;
You probably have some Event by which you would like Form1 be launched...
A Button click, menu item, toolbar item, etc. In that event you will have to check if the form exists and create if required.
private SomeEvent() {
if (myForm1 == null)
{
myForm1 = new Form1();
myForm1.Show(this);
}
myForm1.UpdateText("some text");
}
Alternatively, you could create the form in the Form Load event, just so long as you create the form prior to attempting the myForm1.UpdateText()
If you follow this paradigm, you are creating myForm1 as part of the main form, best practice says you should also dispose of the form in your main form Closing Event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (myForm1 != null)
{
myForm1.Close();
myForm1.Dispose();
}
}
This is all off the top of my head, so it might not be perfect.
In that case you can pass the instance of form in the method as argument and make the changes like
public void UpdateText(string newValue, Form1 frm)
{
frm.torrent_name0.Text = newValue;
}
After closing a form, I can't access it anymore, because the object does not exist anymore.
Is there a way to avoid this kind of behaviour, without initiating the object everytime I perform an event?
This is the first Form called status, it's not the only one I need to create, that's why Iam asking.
This does not work: After closing the form and click on the menu item I get an reference error "Object does not exist", and therefor can't be accessed.
public partial class Main : Form
{
StatusForm statusForm = new StatusForm();
public Main()
{
InitializeComponent();
statusForm.MdiParent = this;
}
private void statusToolStripMenuItem_Click(object sender, EventArgs e)
{
statusForm.Show();
}
private void Main_Load(object sender, EventArgs e)
{
statusForm.Show();
}
}
If you use Close to close a form, it is unusable after that point. You have to create a new one.
However, if you want a persistent Form object, just call Form.Hide instead. This hides the form but leaves it "open".
MSDN notes this as well:
When the Close method is called on a Form displayed as a modeless
window, you cannot call the Show method to make the form visible,
because the form's resources have already been released. To hide a
form and then make it visible, use the Control.Hide method.
I'm using two forms and I disable the first one when the second form shows up. I couldn't find a way to enable the first form when the second one is closed.
Passing a parameter could be a solution but I bet there is a simpler way.
First I thought of enabling the first form on the destructor of the second but could not do it.
Anyone have any suggestions?
You can show second form with ShowDialog() - form will be shown as modal, first form will be enabled only when second will be closed.
For future problems you can have a field in second form to have instance of first one, and use that instance, if you need, for example you can use custom constructor:
class SecondForm: Form
{
FirstForm _parentForm;
public SeconForm(FirstForm form)
{
InitializeComponent();
_parentForm = form;
}
void DoSomethingWithParent()
{
_parentForm.DoSomesting();
}
}
As has been mentioned, in this specific case it probably makes sense to use a modal dialog for opening the second form.
To cover the case when that isn't applicable, the accepted best practice would be to subscribe to the FormClosing event of the second form from the first, and in the event handler you could enable "yourself" and do anything else that you might want to do as a result of the other form being closed. Here is a simple example:
public partial class ParentForm : Form
{
private void button1_Click(object sender, EventArgs e)
{
ChildForm child = new ChildForm();
child.FormClosing += new FormClosingEventHandler(child_FormClosing);
Hide();
child.Show();
}
private void child_FormClosing(object sender, FormClosingEventArgs e)
{
Show();
}
}
I'm working on an application and I've encountered a strange issue, that I think should be simple, but isn't. What I'm trying to accomplish is set the main form's title caption to a specific value when the another form closes. Below is my most recent attempt at this.
// From the main form I have
ObjectForm Objects = new ObjectForm();
Objects.GameName = this.Text; // this is a public string on the ObjectForm side
// Here is what I have on the ObjectForm
private void btnOK_click(object sender, EventArgs e)
{
MainForm Main = new MainForm();
Main.Text = this.txtGameName.Text;
this.Close();
}
Any help will be gladly accepted, thanks :D
You can't just instantiate a new MainForm, you need to get a reference to the existing instance.
Have a look in the documentation at Application.OpenForms
This code you have in your button click handler
MainForm Main = new MainForm();
Main.Text = this.txtGameName.Text;
Instantiates a new MainForm and sets it's title, this is completely separate instance to the MainForm that houses your application.