I want to use a textBox, which is on my main form Form1 from other class. In class Form1 I can use:
this.Invoke(new EventHandler(displayText));
and then
private void displayAccFields(object o, EventArgs e)
{
tbAccRoll.AppendText(packParameters.getPackage(3) + "");
}
and it works fine.
How can I access this textbox for displaying something from a different class?
For sending values between two forms, you may
Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form.
You may take a reference in to your first form in the second form.
In second form,
public Form1 objForm1;
and in First Form,
Form2 objForm2=new Form2();
Form2.objForm1=this;
and then you can use Form2's objForm1 to refer to Form1's textbox.
Related
I am confused about how to navigate between Forms in c#
I want to do the following:
from Form1 opening the Form2 and make Form2 on the top of the original one Form1 and then get back to Form1 and user are not allowed to use Form1 untill they close the child Form2 to get back to Form1
with the same scenario but I may pass parameter from Form2 to Form1
I searched but think are not clear in my mind I found that there is something called MDI Parent And Child like the answer here but
I do not want the child from to be inside the original one
I do not the original from style to be change and be in that gray one
that way I think what I need to use is NOT MDI Parent And Child
please help I appreciate long explanation description with example
This assumes that the data you are interested in on Form2 can be accessed by read/write properties. In my example, I show that as a simple string property called UsefulData. Here's the normal way to do what you are asking:
private void button1_Click(object sender, EventArgs e)
{
var dialog = new Form2(); //create an instance of Form2
dialog.UsefulData = "Some Useful Data"; //set one or more properties of that form
string result = "No Result Yet"; //a place to store the eventual data from Form2
if (dialog.ShowDialog(this) == DialogResult.OK) //passing "this" properly parents the dialog to your Form1
{ //note that I only get the data if the user pressed OK
result = dialog.UsefulData; //get the data
}
//do something with that result
}
There you go!
I'm doing a shop with 7 forms, the form 1 is the basic of the store with picture's Box and a textbox saying how much should I pay. When I click in the picture's box, for example in a T-shirt, it opens a new Form with some information and a button saying "add to cart".
Basically I want to click on the button "add to cart" and then the Form2 closes and back's to form 1 and in the textbox (that shows how much should I pay) shows the value.
Forms are objects like any other, and you can pass references to them and call methods on them like any other.
For example, let's say you expose a method on Form1 which accepts the value and updates the UI:
public void UpdatePayAmount(double amount)
{
// use the supplied value to update your text box
}
Then you would want to call that method from Form2 when clicking on the "Add to Cart" button. Something like:
form1Instance.UpdatePayAmount(someAmountValue);
So your Form2 code needs a reference to an instance of Form1 in a variable somewhere. Since Form2 now depends on Form1, a sensible place to put that requirement would be in its constructor. Perhaps populating a private field:
private Form1 form1Instance;
public Form2(Form1 form1)
{
form1Instance = form1;
}
Now Form2 requires a reference to an instance of Form1 when you create it, so it can call a method on that instance when the button is clicked. So when you create Form2 in your Form1 code you would supply that instance:
form2 = new Form2(this);
form2.Show();
I'm trying to work with multiple forms, what I want is change the way the form is depending on the selected index of a combobox, the only way I could think of is hide form1 and show form2, but the problem is when you close form2, the process does not end...I tried the code below
private void Form2_FormClosing(object sender, FormClosedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("Process Name.exe"))
{
process.Kill();
}
}
if there isn't, is there a way the form can change on combobox selected index?
Try Application.Exit();
It exits your entire application and closes all your forms and threads.
Simply pass an instance of Form1 to the constructor of Form2, keep a reference to it in a form1 member
public class Form2 : Form{
private Form _form1;
public Form2(Form form1):this()
{
_form1 = form1;
InitializeComponent();
}
}
later you can simply use that reference :
_form1.Close();
This is a cleaner way to do it.
Other mechanisms are also ok, like implementing an eventhandler on form1 for an event in form2.
based on your pastebin code change this:
Form2 HeadquarterForm = new Form2(this);
you also only need the closed eventhandler and call close on the _form1 only once. So you don't really need the closing event handler.
The process is still running because form1 is still alive but hidden.
Try using Environmental.exit() to kill the process
Looking at your code in pastebin. Problem is, you're not passing Form1 in constructor of your Form2 when creating it. Change the part of your switch-case (4) to:
Form2 HeadquarterForm = new Form2(this);
I have 2 forms in C# desktop application. Form1 and Form2.
Form1 contains a public method that adds item in ListBox control as follows:
public void AddToList(string item)
{
listBox.Items.Add(item);
}
When I call this method directly on some button press then it works fine. But when I call this method from Form2, it doesn't add anything in ListBox control on Form1. Code in Form2 is as below:
Form1 frm = new Form1();
frm.AddToList("something");
When I run this nothing happens. No error nothing. It just doesn't add any item into ListBox.
What am I doing wrong?
You're creating a new instance of Form1 and adding an item to it's listbox, rather than getting an instance of the Form1 you no doubt already have and calling the method on that.
The naive approach is to have a parameter in the Form2 constructor that takes an instance of Form1 and saves it as an instance variable for use in this event handler.
I don't much like that approach from a design perspective.
I would suggest creating a public event in Form2, having Form1 subscribe to that event and add a handler that add the item to the listbox. The Event in Form2 would look something like this:
public event EventHandler ButtonClick
{
add
{
button1.Click += value;
}
remove
{
button1.Click += value;
}
}
Then you'll have a property that looks something like this:
public string SomeValueForm1NeedsOnButtonClick
{
get
{
return texbox1.Text;
}
}
Then in Form1 you'll have something like:
Form2 otherForm = new Form2();
otherForm.ButtonClick += (sender, args) =>
{
listbox1.Items.Add(otherForm.SomeValueForm1NeedsOnButtonClick);
};
This approach ensures that each form only knows as little as possible about each other form. It reduces Coupling between the two classes and makes it clearer to future users/readers of the forms exactly what communication takes place between them.
Form1 frm = new Form1();
this line is creating a brand new instance of Form2... not the same instance that is already displayed on the screen. so you are adding to the listbox of this secondary instance that is never shown on the screen.
Hmm
If you followed that code with frm.Show() you would have seen it. Isuspect that's not waht you want.
Your approach to the is problem is a tad naive.
You could add a property to form2 and set it to the Form1 instnace you want to use. (PS form1 and form2 are not helping, give them propernames. MainForm and DetailForm or somesuch).
The problem with the above is you've implemented a horrible dependancy.
Lots of ways to go with this, one would be a seperate class to hold the list (an interface and a class would be even better).
Then add a property to Form1 and Form2 of teh ihneterface or class type.
Form2 can then add things to the list.
That raises a list changed event.
Form1 hooks into with an event handler and then refreshes the listbox it's using to display the the doings.
Once you have the infrasturure in polace you can do all sorts of things with it, whereas the method you are using is a lot of code and messing about for very little reward.
When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).