I'd like to create, say 32 Windows Forms based upon a single template Form, and those instances should be linked to each other. That is, every Form has a button for calling the next instance and so on. I am able to create as many forms as I like but how would I link those instances together ?
This is what I use to create several child forms:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm child = new ChildForm();
child.Show();
}
}
Sequence of events would be like:
User starts application, main form is displayed (only has "Open Child" button)
User pushes "Open child" button, first instance of child form opens
first child form (caption "Child Form 1") has button "Open Child Form 2"
if user pushes "Open Child Form 2" child form 1 is hidden and child form 2 is displayed
if the last child form is reached wrap-around to child form 1
Any ideas are welcome !
regards
Chris
You can create a static collection of the form, in the constructor add the form instance to the list (and remove it during dispose). To figure out the next form, you can find the index of the current form and grab the next form in the list based on that. Create a form with two buttons and modify it as below to test it out.
public partial class Form1 : Form
{
static List<Form1> formList = new List<Form1>();
public Form1()
{
InitializeComponent();
formList.Add(this);
}
private void button1_Click(object sender, EventArgs e)
{
int idx = formList.IndexOf(this);
int nextIdx = (idx == formList.Count()-1 ? 0: idx+1 );
Form1 nextForm = formList[nextIdx];
nextForm.changeTextAndFocus("next form: " + nextIdx);
}
// moves to the next form and changes the text
public void changeTextAndFocus(string txt)
{
this.Focus();
this.Text = txt;
}
//Creates 5 forms
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
Form1 newForm = new Form1();
newForm.Show();
}
}
}
I don't really know what you are planning to do :). If you want to count several forms, you can add a property Number to the form. And searching upwards from there.
Mainform;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm first = new ChildForm();
first.Number = 1;
first.Show();
}
}
ChildForm
public partial class ChildForm : Form
{
public ChildForm()
{
// createButton here
}
private void button_Click(object sender, EventArgs e)
{
ChildForm _childForm = new ChildForm();
_childForm.Owner = this;
_childForm.Number = this.Number + 1;
this.Hide();
_childForm.Show();
}
public void FirstChildForm()
{
if (this.Number != 1) //maybe not that static
{
(this.Owner as ChildForm).FirstChildForm();
this.Close(); // or hide or whatever
}
}
public int Number
{ get; set; }
}
Not tested code, hope this helps a bit :).
Related
I have a Form (Form1) where the ComboBox is located, and another form (Form2) where the label is located.
All I want to do is increase that label number when selecting a specific item from the ComboBox and clicking a button.
The code from Form2:
public partial class Form2 : Form
{
public static int counter;
private void btnCadastrar_Click(object sender, EventArgs e)
{
if (combobox.SelectedItem.ToString() == "Item1")
{
counter = Int32.Parse(Form2.labelincrease);
counter++;
Form2.labelincrease = counter.ToString();
}
}
}
The code from Form 1:
public partial class Form1 : Form
{
public static string labelincrease;
}
private void Form1_Load(object sender, EventArgs e)
{
labelincrease = lblIncrease.Text.ToString()
}
I am getting a lot of errors and I am trying a lot of methods but can't get there, I would appreciate any help, thanks.
Hope this will help you.
Instantiated Form2 f2 = new Form2();
labelincrease = f2.lblIncrease.Text.ToString();
I have a dialog that I can drag it around the screen.When I close the dialog and open again. I want it to be shown in the last position it was before.
How can I save the last position of the dialog in win forms?
Sample & dirty solution.
In solution we have two forms. Form1, Form2, and Settings static class.
public static class Settings
{
public static int X = 0;
public static int Y = 0;
}
Inside Form1 we have button1 that will be responsible to show Form2.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(Settings.X, Settings.Y);
frm.ShowDialog();
}
}
Inside Form2 we have ClosingEvent Handler :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.X = this.Location.X;
Settings.Y = this.Location.Y;
}
}
When Form2 is closing we are storing location to static Settings class. Later we are reading from this class X and Y position.
I hope it was helpful.
You need to:
1) create your custom "dialog", which is basically Windows.Forms.Form.
FormStartPosition property set to Manual.
2) once created, in its OnClosing event save its position somewhere from Location property
3) next time you are going to show it, assign the value you've saved before.
I have 2 forms. The second form is opened from a method in the first form and I wish to be able to update the textbox that exists within that second form.
Basically I have the following code:
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI();
sendOutGUI.Show();
sendOutGUI.sendConsoleTextBox.Text = "Test";
}
When I press the button the second form (SendConsoleGUI form) opens but "Test" is never added to its textbox.
Am I doing something wrong here?
You need to use invoke method.
sendOutGUI.Invoke((MethodInvoker) delegate { sendOutGUI.sendConsoleTextBox.Text = "Test"; });
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI("Test");
sendOutGUI.Show();
}
}
public partial class ChildForm : Form
{
public ChildForm(string str)
{
InitializeComponent();
sendConsoleTextBox.Text = str;
}
}
This will work for you if you only wish to update it when the ChildForm is initially created.
I've tried this code to add a row of function that I created on the main form (Form1) in DataGridView in Form2, but there is no result add row when I run its function, does anyone know where the mistake?
Thanks
[edit]Code in MainForm1 :
public partial class Form1 : Form
{
Form2 sub = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
sub.AddRow(new string[] { "1", "2" }); // able to loop
sub.Show();
this.Enabled = false;
}
}
[edit] Code in Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void AddRow(string[] values)
{
this.DGVFile.Rows.Add(values);
}
}
I get a few hints at some answers. My questions were answered when the data is loaded in datagridview in Form2 and also I want the mainform (Form1) can be disabled when opening Form2. Now it has been resolved. Thanks All.
Use sub.Show(); instead. This way, the code bellow sub.ShowDialog(); will not even get called.
ShowDialog opens a new form as a modal window, which means, that the focus is only in the new window and lines below this call are called when the new windows is closed.
You can find more information about these two methods on MSDN here and here.
UPDATE:
but i want disabled mainform if open form2
I can think of two options.
The first, and in my opinion better, is to postopne opening of the form
Form2 sub = new Form2();
sub.RegisterParent(this);
for (int x = 1; x <= 5; x++ )
{
string[] row;
row = new string[] { "1","2"};
sub.DGVFile.Rows.Add(row);
}
sub.ShowDialog(); // open when everything is prepared
The second is
sub.Show();
this.Enabled = false;
but this is not really a nice solution, because you would have to enable it from the other form before closing it and there are many ways how to close a form and you would have to consider all.
Take the creation of the second form from the button click event out: Form2 sub = new Form2();. This way the second form is created only once. Then use ShowDialog and Hide methods to show a modal second form (the first form will be inactive). Create an extra public method in the second form to add new row to the existing DataGridView.
A possible solution can look like this:
public partial class MainForm : Form
{
private DataForm dataForm = null;
public MainForm()
{
InitializeComponent();
// init data form
dataForm = new DataForm();
dataForm.Init();
}
private void buttonAddRow_Click(object sender, EventArgs e)
{
// add values
dataForm.AddRow(new string[] { "10", "20" });
// show a modal second form, so that the first can not be selected while the second is open!
dataForm.ShowDialog(this);
}
}
public partial class DataForm : Form
{
public DataForm()
{
InitializeComponent();
}
// init the grid
internal void Init()
{
this.dataGridView.Columns.Add("A", "A");
this.dataGridView.Columns.Add("B", "B");
for (int x = 1; x <= 5; x++)
{
string[] row;
row = new string[] { "1", x.ToString() };
this.dataGridView.Rows.Add(row);
}
}
// add values to the grid
public void AddRow(string[] values)
{
this.dataGridView.Rows.Add(values);
}
// hide second form
private void buttonClose_Click(object sender, EventArgs e)
{
Hide();
}
}
Output after two additions is:
The code after sub.ShowDialog(); is executed only after the form sub is closed. Try using sub.Show()
Please review the following code:
namespace TEST_CSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 sub = new Form2();
sub.Show(this);
this.Enabled = false;
sub.RegisterParent(this);
for (int x = 1; x <= 5; x++)
{
string[] row;
row = new string[] { "1", "2" };
sub.DGVFile.Rows.Add(row);
}
}
}
}
The command Show(this) makes Form1 the parent form of Form2 and also ensures that Form2 will be always displayed on top of Form1. To deactivate Form1 use this.Enabled = false; In order to reactivate Form1 when Form2 closes use the following code:
namespace TEST_CSA
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 _parent;
internal void RegisterParent(Form1 form)
{
this._parent = form;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_parent.Enabled = true;
}
}
}
First of all, I am a newcomer to C# and programming in general. I've searched pretty thoroughly, but I can only find instances where someone wants to open another form and hide the one that the button was pressed on.
In this instance, I'm having issues with my program continuously running when I press the (X) on any form other than the main "Form1".The form-to-form navigation works fine. i.e.: clicking a button hides the main window and opens the appropriate form, and the "back" button on that form hides itself and shows (I guess another instance) of the previous "main" form. --I could probably use some guidance in that, too. lol
I wouldn't mind if it closed the entire application if the X was pressed, but I need to have the "X" present on all windows and all windows need to exit the entire app if the X is pressed. Any suggestions?
Thanks in advance,
Code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void btnTransaction_Click(object sender, EventArgs e)
{
Transaction transactionForm = new Transaction();
Form mainForm = this;
transactionForm.Show();
mainForm.Hide();
}
}
Transaction Form:
public partial class Transaction : Form
{
public Transaction()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
Form1 mainForm = new Form1(); //not sure if I'm doing this right..
this.Hide(); //I don't know how to "reuse" the original Form1
mainForm.Show();
}
}
I would recommend you create an MDI Container for this. Drag and drop a MenuStrip from the ToolBox to Form1 and then create a ToolStripMenuItem "form2" in MenuStrip.
Now you can call your form2 in form1 like this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IsMdiContainer = true;
}
Form2 frm2;
public void CreateMdiChild<T>(ref T t) where T : Form, new()
{
if (t == null || t.IsDisposed)
{
t = new T();
t.MdiParent = this;
t.Show();
}
else
{
if (t.WindowState == FormWindowState.Minimized)
{
t.WindowState = FormWindowState.Normal;
}
else
{
t.Activate();
}
}
}
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateMdiChild<Form2>(ref frm2);
}
}
When by clicking ToolStripMenuItem you fire ToolStripmenuItem event , it will show Form2 as child in form1 i.e the mdi container and will be closed when you close form1.
public partial class Transaction : Form
{
Form1 _mainForm;
public Transaction(Form1 mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
private void button4_Click(object sender, EventArgs e)
{
this.Close(); //since you always create a new one in Form1
_mainForm.Show();
}
}
You can use the use Form.ShowDialog()
When this method is called, the code following (code below the 'ShowDialog()') it is not executed until after the dialog box is closed.
private void button4_Click(object sender, EventArgs e)
{
Form1 mainForm = new Form1();
this.Hide();
mainForm.ShowDialog();
this.Show();
}
You could use the ShowDialog() method that will require the user to interact with the new form before returning to the previous form. For instance you could try this:
public void btnTransaction_Click(object sender, EventArgs e)
{
using (var transactionForm = new Transaction())
{
this.Hide();
if (transactionForm.ShowDialog() == DialogResult.OK)
{
this.Show();
}
}
}
The DialogResult is something you can set on the TransactionForm like so:
private void button4_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
That's a pretty standard way of forcing interaction on a new form.
protected override void OnClosing(CancelEventArgs e)
{
this.Hide();
menu menu = new menu("administrator");
menu.ShowDialog();
this.Close();
}
//happy coding