Im trying to add data to a listview from another form. I have tired doing it from the same form and that works like it should, but when I try to pass the info between the forms no data gets entered.
Form 1 Contains listview
private void button3_Click(object sender, EventArgs e)
{
newTask form = new newTask();
form.Show();
}
Form 2 Contains form to submit info to listview
public void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string size = textBox2.Text;
Form1 table = new Form1();
table.listView1.Items.Add(url);
table.listView1.Items.Add(size);
this.Hide();
}
With Form1 table = new Form1(); you are creating new object type Form1 and it doesn't reference to your old form.
What you need to do is change constructor of second form to
private Form1 myFirstForm;
public newTask(Form1 form1)
{
InitializeComponent();
myFirstForm = form1;
}
and your second part of code should then look like this:
public void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string size = textBox2.Text;
myFirstForm.listView1.Items.Add(url);
myFirstForm.listView1.Items.Add(size);
this.Hide();
}
Related
I'm trying to open a Panel that is in the main form (form1) from a button that is inside a UserControl , but the code runs but does not enable the panel of the main form
Can you help me?
//UserControl code
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
Button seta = (Button)sender;
var form = new Form1();
form.EnabledPanel1(seta.Tag.ToString());
}
//main form code
public void EnabledPanel(string order)
{
panel1.Visible = true;
}
Assuming the UserControl is also contained by Form1, then you can use TopLevelControl to get a reference to the Form:
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
Button seta = (Button)sender;
Form1 f1 = (Form1)this.TopLevelControl;
f1.EnabledPanel1(seta.Tag.ToString());
}
*Why does EnabledPanel() receive a string?
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 have form5 and form6 and use these code to display data from datagridview to textbox
private void button1_Click(object sender, EventArgs e)
{
Form6 FRM = new Form6();
FRM.ShowDialog();
}
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Form5 frm = new Form5();
frm.textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
frm.Show();
}
My question is: I want to add data to textbox not display data.
For example I have in textbox1 in form5 'mahmoud' I want to when action double click in datagridview to add 'johan' next to mahmoud
mahmoud,johan,jjjj,kkkk,jjjj,ahaha
try below codes
string name = dataGridView1.CurrentRow.Cells[1].Value.ToString();
frm.txtCustomer.Text = name;
frm.Show();
OR
StringBuilder textvalues = new StringBuilder();
textvalues.Append(dataGridView1.CurrentRow.Cells[1].Value.ToString());
frm.Show()
Why not this?
string selectedText;
private void dGV1_doubleClick(....){
selectedText= dataGridView1.SelectedCells[0].Value.ToString();
frm5.TextBox1.Text+= ","+selectedText;
frm5.BringToFront(); //I suppose the form is opened.
}
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
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
}