I have 2 forms. Form 1 which shows a ListView and Form 2 a button called button1.
What I'm trying to do is when the button on form 2 is clicked. I want it to fill the Listview on form1.
The listview has 3 columns;
Flavour
Quantity
Sub-Total
When the button1 is pressed, it should show Vanilla, 1, £1.00 in the listview on form1.
I'm able to do this if the listview is on the same form as the button, but not if it's on different forms.
Form1
public partial class form1: Form
{
public form1()
{
InitializeComponent();
}
Form2
public partial class form2: Form
{
public form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("Vanilla");
lvi.SubItems.Add("1");
lvi.SubItems.Add("£1.00");
listView1.Items.Add(lvi);
}
Create a reference of form1 in form2 like this:
class Program {
static void Main() {
var form1 = new Form1();
var form2 = new Form2(form1);
}
}
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public void DoStuff(ListViewItem lvi) {
// TODO: Stuff
}
}
public partial class Form2: Form
{
private Form1 _form1;
public form2(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("Vanilla");
lvi.SubItems.Add("1");
lvi.SubItems.Add("£1.00");
listView1.Items.Add(lvi);
_form1.DoStuff(lvi);
}
}
Related
In Form1 I have one DataGridView and multiple textboxes. When I click A button in Form2 I need to save the data from DataGridView and multiple textboxes to Database. How to Implement in C sharp Windows Application
Form1 Button Click event. I opened Form2
public sealed partial class form1 : Form
{
private static form1 instance = null;
public static form1 Instance
{
get
{
if (instance == null)
{
instance = new form1();
}
return instance;
}
}
private void button1_Click(object sender, EventArgs e)
{
textbox2.Text=100;
form2 CO = new form2();
CO.Show();
}
}
I want to attach textboxes data and Datagridview content to
object SO and Call InsertSale function .textboxes and datagridview are in form1
This is Button Click Event in Form 2
private void button1_Click(object sender, EventArgs e)
{
clsSale SO = new clsSale();
SO.Totamount = Convert.ToDecimal(form1.Instance.textBox2.Text);
SO.InserSale(SO);
}
If Form2 wants to access the Form1 properties.
Pass ParentForm instance to the ChildForm constructor. Add a public method in the parent form to update its properties from child form.
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public void SetTextBoxValue(string val)
{
this.textBox1.Text = val;
}
private void CreateForm2()
{
var form2 = new Form2(this);
form2.Show();
}
}
public partial class Form2: Form
{
private Form1 form1;
public Form2(Form1 frm1)
{
InitializeComponent();
form1= frm1;
form1.SetTextBoxValue("Value from Form2");
}
}
Create a global class, such as Global.cs, in the project. Then declare the following variables:
public static Form1 frm1
public static Form2 frm2
Declare a variable of the Form class - Form frm1 or Form frm2 etc.
Now access the variables from any form as follows:
Global.frm1 = new Form1() // - for the Home Form1
Global.frm1.ShowDialog();
Global.frm2 = new Form2() // - for the Home Form1
Global.frm2.ShowDialog();
If you want to access a control in any form, just extend them as follows:
frm1.txtBox1.Text
frm2.button1.Click() // etc.
Hello I'm kind of new to C# and I'm trying to get one form to talk to another form in the same namespace. So the button the first button is on form1 and it opens up form2 then I want the user to click a button in form2 to make a button in form1 visible that was previously invisible.
This is what I have for button 1 on form1.
Form2 MainWindow = new Form2();
MainWindow.Show();
The is what I have for the button on form2.
Form1.button2.Visible = true;
You can do something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.ShowDialog();
}
///to set the visibility of things you want
public void SetVisibility(bool visibility)
{
button1.Visibility = visibility;
}
}
///Form2
public partial class Form2 : Form
{
private Form1 parentForm;
public Form1()
{
InitializeComponent();
}
public Form1(Form parentForm)
{
InitializeComponent();
this.parentForm = parentForm;
}
///to set the visibility of things you want
private void button1_Click(object sender, EventArgs e)
{
parentForm.SetVisibility(true);
}
}
But my advise is to learn basics first as this is simple stuff and is not worthy
posting here .
I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
I have an application with 2 Forms, for those forms I have create a Menu which I depose on the two forms.
There is only a menuStrip item on the menu, I just want when I click on "test1" to redirect to Form1 and when I click to "test2" I want to redirect to Form2.
But if test1 is already open/display I don't want to show him again and the same for test2.
My code in my Menu :
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.Hide();
f1.Hide();
f1.ShowDialog();
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Hide();
f2.Hide();
f2.ShowDialog();
}
}
My Form1 :
My Form2 :
I just want the same result like my Buttons in Form1 and Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
I thought that the property Visible for forms could help me but not...
The problem is when I click on my buttons it's open a new window but when my form is already open I don't want to open it again.
Thanks for your reply, I hope that I am clear sorry for my english in advance.
You are currently creating a new form each time the click handler code is executed.
Here is one way, but its nasty and I wouldn't really recommend it. I've assumed that form1 is the entry to your application and that its also the exit of the application. This solution uses a singleton to hold the f1/f2 instances.
public static class Global
{
static Global()
{
f2 = new Form2();
}
public static Form f1;
public static Form f2;
}
Your menu altered:
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Global.f2.Hide();
Global.f1.Hide();
Global.f1.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Global.f1.Hide();
Global.f2.Hide();
Global.f2.Show();
}
public void SetForm1(Form form)
{
Global.f1 = form;
}
public void SetForm2(Form form)
{
Global.f2 = form;
}
}
And the forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Global.f1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Global.f2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Global.f1.Show();
}
}
Hope this helps.
Your logic is wrong.
It seems to me that you want to display different content depending on the user selection on that 'menuStrip'. You need to look at dynamic controls loading not different forms loading.
You can have just a MainForm with that 'menuStrip' and a Panel. Define some User Controls and dynamically add them to that panel based on the user selection.
Snippet
public MainForm : Form
{
public MainForm()
{
// code
}
public void MenuStrip_OptionSelected(object sender, EventArgs e)
{
Panel1.Controls.Clear();
switch(MenuStrip.SelectedValue)
{
case "UserControl1" : Panel1.Controls.Add(new UserControl1()); break;
...
}
}
}
public UserControl1 : UserControl
{
// code
}
c# devexpress gridview selected row count error
i want to get row from anouther form(gridcontrol), i tryed form.gridview.selected.row.count but its not working. what can i write for the get this count? and from other forms of money, i want to print labels to collect this form.
""
gridView1.Columns["money"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
gridView1.Columns["money"].SummaryItem.DisplayFormat = "Toplam:{0:n2}";
gridView1.OptionsView.ShowFooter = true;
""
i tryed this code its working but on the gridview but i want to write other form in the label.
form1 have gridView1.
form2 have label2. label2 display Sum from gridView1.
form1 must has form2 as its field or property.
form2 must has method public void SetSum(money sum) to set sum for label2.
Every count sum (e.g by click button) call form2.SetSum(sum)
MainForm.cs - Hosts form1 and form2
public partial class MainForm : Form
{
private Form1 _form1;
private Form2 _form2;
public MainForm()
{
InitializeComponent();
_form2 = new Form2();
_form1 = new Form1(_form2);
}
private void btnForm1_Click(object sender, EventArgs e)
{
_form1.Show(this);
}
private void btnForm2_Click(object sender, EventArgs e)
{
_form2.Show(this);
}
}
Form1.cs
public partial class Form1 : Form
{
private Form2 _receivedForm;
public Form1()
{
InitializeComponent();
}
public Form1(Form2 receivedForm) : this()
{
_receivedForm = receivedForm;
}
private void btnSend_Click(object sender, EventArgs e)
{
_receivedForm.SetSum(txtSum.Text);
}
}
Form2.cs:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void SetSum(string sum)
{
lblSum.Text = sum;
}
}
Hope this help. :D
ı did with this code now is working. thanks for all answer
DataBaseDataContext db = new DataBaseDataContext();
var sum = (from ord in db.safes select ord.money).Sum();
lbl_safein.Text = sum.ToString();