I am trying to maximize a form from a class file but when I make a new instance of the form in the class nothing happens. I have spent a long time searching for an answer and did not find anything. This is the code I have in the class.
public void maxForm() //code from the class
{
Options options = new Options();
options.WindowState = FormWindowState.Maximized; //not working
}
This is the code I have in the form.
private void button1_Click(object sender, EventArgs e) //code from form
{
Class1 class = new Class1();
class.maxForm();
}
If I do the same code but for a different form it will work. It acts like its being blocked.
Don't you need to show form?
Options options = new Options();
options.WindowState = FormWindowState.Maximized;
options.Show();
when I make a new instance of the form
That's your problem right there. You don't want a new instance, you want the instance that already exists.
If your button1 is part of your Options form, then just do this:
private void button1_Click(object sender, EventArgs e) //code from form
{
WindowState = FormWindowState.Maximized;
}
Or if, as you say, you need to maximize it from a different class (why?), then you can do something like this:
public void maxForm(Options options) //code from the class
{
options.WindowState = FormWindowState.Maximized;
}
private void button1_Click(object sender, EventArgs e) //code from form
{
Class1 class = new Class1();
class.maxForm(this);
}
Related
I have an application that sends information from two forms (Form 2 to Form 1) and I have a menu screen that enters Form 2 but when I try to open form two I get the error (CS7036). Basically, I have the error that says
"Error CS7036 There is no argument given that corresponds to the
required formal parameter 'incoming' of 'difficulty.difficulty(Easy)"
(difficulty is "form 2" and easy is "form 1"
FROM Menu
private void btnStart_Click(object sender, EventArgs e)
{
this.Close();
difficulty diff = new difficulty();
diff.ShowDialog();
}
FROM DIFFICULTY
public partial class difficulty : Form
{
public difficulty(Easy incoming)
{
InitializeComponent();
}
private void btnEasy_Click(object sender, EventArgs e)
{
this.Close();
Easy easy = new Easy();
easy.ShowDialog();
}
}
How do I make it work so I can enter the menu then difficulty then easy without any errors? Any help appreciated.
You haven't passed in the parameter you added to your constructor while instantiating the class.
Here's a fixed version of your code:
private void btnStart_Click(object sender, EventArgs e) {
//use whatever easy you have here
Easy easy = new Easy();
difficulty diff = new difficulty(easy);
diff.ShowDialog();
this.Hide();
}
You need to have an Easy class created beforehand to pass in. And if you want to use that instance in the class, you can do this:
public partial class difficulty : Form {
private Easy easy;
public difficulty(Easy incoming)
{
easy = incoming;
InitializeComponent();
}
private void btnEasy_Click(object sender, EventArgs e) {
....
}
}
Then in any of the class functions, reference the easy variable to get data.
I have a project, where a wizard form is called to make a project. On the end of the wizard I want to send a 'world' object back to a variable in the Main form. But If I give the main form with it through the constructor I can't access it's methods or properties. Am I doing something wrong?
here is my code:
main form
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
this.NewProject();
}
private void NewProject()
{
var myForm = new ProjectWizard(this);
myForm.Show();
}
Wizard form code:
public ProjectWizard(Form form)
{
InitializeComponent();
MainForm = form;
}
private void finishButton_Click(object sender, EventArgs e)
{
//World world = new World();
//MainForm.CurrentWorld = world;
}
Thanks in advance.
You just need to make CurrentWorld public or internal on the MainForm class. Honestly, you're doing everything else right!
UPDATE: also make sure that the MainForm private field declared in Form1 is of the type MainForm and not just Form. So, change the constructor a tidge too:
public ProjectWizard(MainForm form)
I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.
I have a DataGridView in Form1 and I'm using this code to display another form called Generator:
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator();
// Form gen = new Generator(Form this); //* I tried this but is not working *//
gen.Show();
}
In the Generator form I need to read or modify something in the datagridview which is in the Form1.
public partial class Generator : Form
{
public Form myForm;
public Generator()
{
InitializeComponent();
}
public Generator(Form frm)
{
myForm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
myForm.mydatagridview.! // this is not working
}
}
How can I resolve this problem, so I can manipulate the DataGridView from the Generator form?
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator(this.mydatagridview);
gen.Show();
}
Generator Form:
DataGridView _dataGridView;
public Generator(DataGridView dataGridView)
{
InitializeComponent();
this._dataGridView = dataGridView;
}
private void button1_Click(object sender, EventArgs e)
{
this._dataGridView...! // this will work
}
Things that you must do, and know (just tips, you are not forced to do these but I believe you will be a better programmer if you do! ;)
Always call InitializeComponent() in all form constructors. In your sample you didn't call it in one of the constructors.
C# knows only information of the type you have passed. If you pass a Form, then you only get Form properties (i.e. the properties of type Form), not the properties of your own form.
Try to encapsulate things. Do not pass a whole form to another form. Instead, pass things that you would like to use on the other form.
A few things are going on here.
You have to use the constructor of Generator that take in a form as a parameter.
You have to expose the datagridview as a public or internal property on the form that you will pass into Generator.
The normal Form class will not know about this property, so you should cast the variable appropriately.
You should call the default constructor of Generator when the other constructor is used to make sure everything is initialized correctly. See code sample below.
Something like this should work:
public class Generator
{
private MyForm myForm;
public Generator()
{
InitializeComponent();
}
public Generator(MyForm frm) : this() // DON'T FORGET THIS()
{
myForm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
myForm.MyDataGridView... // Yay, it works!
}
}
public class MyForm : Form
{
public MyForm()
{
InitializeComponent(); // a datagridview is created here, say "datagridview1"
}
public DataGridView MyDataGridView
{
get { return datagridview1; }
}
}
And then in your button click event (which I assume is defined somewhere in MyForm):
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator(this);
gen.Show();
}
The easiest way from there is to open the designer for the DataGridView (myDataGridView) on Form1 and set the Modifiers property from private to internal or public
This will let you call myForm.myDataGridView from the Generator form.
I have "formA" and 2 buttons on it (button1 and button2). What I want to do is:
When I click on button1 to call "formB" display text written in label1.
When I click button2 to call the same form ("formB") hide label1 and display label2.
The problem is that I don't know how to check what button is clicked on "formA".
Edit: Thanks very much folks for the quick answer. Problem is solved!
This is where events come in handy:
public class FormA
{
private void button1_Click(object sender, EventArgs e)
{
formB.Button1WasClicked();
}
private void button2_Click(object sender, EventArgs e)
{
formB.Button2WasClicked();
}
}
public class FormB
{
public void Button1WasClicked()
{
label2.Visible = false;
label1.Visible = true;
label1.Text = "Button 1 was clicked!";
}
public void Button2WasClicked()
{
label1.Visible = false;
label2.Visible = true;
label2.Text = "Button 2 was clicked!";
}
}
button1 and button2 have their own separate Click event handlers. This way we can differentiate the two when they are clicked.
If you have the same event handler for both buttons (as mentioned in one of the comments), you can identify them with the sender parameter using:
Object.ReferenceEquals(sender, button1);
or
Object.ReferenceEquals(sender, button2);
Then your code would look like this:
private void button_Click(object sender, EventArgs e)
{
if (Object.ReferenceEquals(sender, button1))
{
formB.Button1WasClicked();
}
else
{
formB.Button2WasClicked();
}
}
FormB can't find out, the buttons are a private implementation details of FormA. They might not even be a button, surely you are going to add a menu or a toolbar to FormA some day.
The workaround becomes much simpler if you stop thinking of "calling a form". You never call a form, you create an instance of it. And then you make it visible by calling its Show() method. Lots of things you can do in between those two steps.
Add a public method to FormB. For lack of a better name:
public void MakeLabel2Visible() {
this.label1.Visible = false;
this.label2.Visible = true;
}
Now it becomes simple. Implement button2's Click event handler like this:
private void button2_Click(object sender, EventArgs e) {
var frm = new FormB();
frm.MakeLabel2Visible();
frm.Show();
}
Adding another constructor to a form that lets you initialize it differently is another very common approach. These are just classes, standard programming techniques are appropriate.
Because you are using winforms you can do all this very easily due to the fact that you have a stateful environment.
Assuming a very basic set up with:
event handlers in the code behind of form a
a reference to an instance of form b in form a (or the button click creating such an instance)
a method to use in form b to pass it data
Your code will be something like this:
public partial class FormA : Form
{
private FormB formB;
public FormA()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (formB == null || formB.IsDisposed)
{
formB = new FormB();
}
formB.UpdateLabel("Button A");
formB.Show();
}
private void button2_Click(object sender, EventArgs e)
{
if (formB == null || formB.IsDisposed)
{
formB = new FormB();
}
formB.UpdateLabel("Button B");
formB.Show();
}
}
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
public void UpdateLabel(string message)
{
label1.Text = message;
}
}
Of course, there are lots of improvements to this - using events and alerts more intelligently and refactoring to remove duplication, but this is a basic example of the sort of things you can do.