I got 2 forms, Form1 contains the datagridview and button "add", Form2 contains the textboxs and button "save",
I want to add row and form2 appears when add button is clicked, then save informations from form2 in the datagridview when save button is clicked
this is the code i'm using for both add and save buttons, but when i do, it only saves informations wrote from form1 (save button doesn't do much if not updating datagridview)
private void AddButton_Click(object sender, EventArgs e)
{
Form2 windowAdd = new Form2();
windowAdd.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
windowAdd.ShowDialog();
var frm2 = new Form2();
frm2.AddGridViewRows(textName.Text, textDescription.Text, textLocation.Text, textAction.Text);
textName.Focus();
this.stockData.Product.AddProductRow(this.stockData.Product.NewProductRow());
productBindingSource.MoveLast();
}
private void SaveButton_Click(object sender, EventArgs e)
{
productBindingSource.EndEdit();
productTableAdapter.Update(this.stockData.Product);
this.Close();
}
Try this approach.
Form2 could accept some parameters for construction. You will have to resolve the reference to the productBindingSource and productDataAdaptor.
public partial class Form2 : Form
{
private DataRow _theRow;
private bool _isNew;
public Form2(DataRow theRow, bool isNew)
{
InitializeComponent();
_theRow = theRow;
_isNew = isNew;
}
private void Form2_Load(object sender, EventArgs e)
{
textName.Text = _theRow["Name"];
// Etc
}
private void btnSave_Click(object sender, EventArgs e)
{
// This is your add / edit record save button
// Here you would do stuff with your textbox values on form2
// including validation
if (!ValidateChildren()) return;
if (_isNew)
{
// Adding a record
productBindingSource.EndEdit();
productTableAdapter.Update();
}
else
{
// Editing a record
}
this.Close();
}
}
This changes your call in the Form1.Add Button event. I have shown below the way to utilize a using block to show a form.
private void btnAdd_Click(object sender, EventArgs e)
{
DataRow newRow = stockData.Product.NewProductRow();
using (var addForm = new Form2(newRow, true))
{
addForm.StartPosition = FormStartPosition.CenterParent;
addForm.ShowDialog(this);
// Here you could access any public method in Form2
// You could check addForm.DialogResult for the status
}
}
This isn't the best way to do this, but this direction might be a easier way to try...
Hope it helps
Related
i have a binded textbox control (txtMtrlGrpNum) on form1, i can change text property from the same form:
// in frm1
public frm1()
{
InitializeComponent();
Da = new SqlDataAdapter("select * from mtrlGrp", cn);
Da.Fill(Dt);
txtMtrlGrpNum.DataBindings.Add("Text", Dt, "GrpNum");
matGrpCManager = (CurrencyManager)this.BindingContext[Dt];
}
public void btn_add1_Click(object sender, EventArgs e)
{
if (matGrpCManager.Position >= 0)
{
matGrpCManager.AddNew();
txtMtrlGrpNum.Text = "123";
}
}
but when i want to execute button click from another form (Form2) like this:
// in frm2
private void btn_add2_Click(object sender, EventArgs e)
{
frm1 = new Form1();
frm1.btn_add1_Click(sender, e);
frm1.ShowDialog();
{
its not changed, its shown empty (""), cause i am using databinding, how to remove binding temporary and re apply it again,... any idea?
Here's a short example describing the logic.
In the first form, I created the handler for the button click event. I also added a button to open the second form. When the second form is created, I set the reference to the first form.
public void button1_Click(object sender, EventArgs e) // set public // btn_add1_Click
{
textBox1.Text = "Clicked!"; // set text in text box
}
private void btnOpenForm_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(); // create second form
frm2.frm1 = this; // set reference to this form
frm2.Show(); // show second form
}
In the second form, add a reference to the first form along with a button to trigger the form1 button handler.
public Form1 frm1 = null; // reference to first form
private void button1_Click(object sender, EventArgs e)
{
frm1.button1_Click(sender, e); // call handler in first form
}
After the second form is launched, either button will trigger the form1 handler:
I'm having trouble passing values entered in form2(citacao) to form1(principal).
Principal.cs (form1)
richEditControl1.Document.AppendText(citacao.valor_edit[0]);
Citacao.cs (form2)
public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
valor_edit[0] = memoEdit1.Text;
valor_edit[1] = comboBox1.SelectedItem.ToString();
valor_edit[2] = textEdit1.Text;
}
But when I click the button nothing happens , the values are not inserted into the richedit I like it.
I already have this on form (Pass DataGrid to ComboBox)
Form1 (principal)
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
citacao cita = new citacao(this);
cita.Show();
}
form2(citação)
public citacao(principal gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
{
comboBox1.Items.Add(row.Cells[0].Value.ToString());
}
comboBox1.SelectedIndex = 0;
}
let's see whether I understood your situation :)
declare your variable in Form 1 as a class variable
private citacao cita;
then initialize it in the button press event
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
cita = new citacao(this);
// subscribe to the closing event
cita.FormClosing += form_FormClosing;
cita.Show();
}
// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
// BUT! you have to use the variable name!
richEditControl1.Document.AppendText(cita.valor_edit[0]);
}
EDIT:
Ok after looking at the entire code:
please remove the button3! and this entire code:
private void button3_Click(object sender, EventArgs e)
{
cita = new citacao(this);
richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}
The function AppendText probably needs a string as parameter and you give the entire array!
If you subscribe to the closing event in Form1 / principal and also implement
the event, your data will be transmitted automatically as soon as the Form 2 disappears from the screen :)
I have 2 forms
Form1
Form2
I have one button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.ShowDialog();
f2.Dispose();
}
but issue is while opening form it's bliking and diasparing
i have tried to use show() also but not solved the problem
If i have not used Disposed method then first time when run the form it appering and disappered but sencond time onward by clicking on button it's working fine...
In Form2_Load event i am using this two property
private void Form2_Load(object sender, EventArgs e)
{
this.RightToLeft = RightToLeft.Yes;
this.RightToLeftLayout = true;
}
Don't change the form layout while its loading. Change it before you launch. Remove the code from Form2_Load and put it in button1_Click:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.RightToLeft = RightToLeft.Yes;
f2.RightToLeftLayout = true;
f2.ShowDialog();
}
I would guess you want to show and close the form2 using the same button. And I doubt your initial problem description
"issue is while opening form it's bliking and diasparing"
I think form2 is not 'blinking' while opening, but is 'blinking' while you try to click the button again in form1
ShowDialog() will exit your execution after u called it. Mean, it will exit the execution after you click the button.
Thus, you should try Show() with conditional statement within the button click event
In form1.cs
bool flag = false;
Form2 frm2;
private void button1_Click(object sender, EventArgs e)
{
if (flag == false)
{
frm2 = new Form2();
frm2.Show();
frm2.Load += new EventHandler(frm2_Load);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
flag = true;
}
else
{
frm2.Close();
flag = false;
}
}
void frm2_Load(object sender, EventArgs e)
{
//set what ever properties you like
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
flag = false;
}
See also: A dialog disables all of the windows that your program displays
Remove this Property
this.RightToLeft = RightToLeft.Yes;
and run your form...
Try This :
private void button1_Click(object sender, EventArgs e)
{
using(Form2 f2 =new Form2())
{
f2.ShowDialog();
}
}
My mainForm contains two buttons(btnLoad & btnChange) and a panel
When the btnLoad is clicked, it loads the other forms(there are 5 different froms with different controlers) into the panel. Let me assume one of it named Form2 which contains a label(labelMessage)
My problem is, when I click the btnChange the following statement won't work.
f2.labelMessage.Text = "Button Change Clicked";
My codes are
// codes on mainFrom
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = new From2();
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
private void btnLoad_Click(object sender, EventArgs e)
{
panelDock.Controls.Clear();
Form f2 = new Form2();
f2.TopLevel = false;
panelDock.Controls.Add(f2);
f2.Show();
}
is this wrong?
Since Form2 is already shown you should use Application.OpenForms instead of creating a new instance of Form2
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
From your comment that Form2 is in a panel you can try
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)panel1.Controls["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
I have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}