I have two forms, form1 and form2.
form1 has three buttons, button1(vanilla), button2(chocolate) and button3(nextpage).
form2 has a listView1
On form1 the user will click button1 or/and button2. If they click on both how do I display it underneath onto the next row on the listView1 on form2.
There's a screenshot below which shows what I mean
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
public static string buttonValue = "";
public static string buttonValue1 = "";
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanillaaaa";
}
private void button2_Click(object sender, EventArgs e)
{
buttonValue1 = "Chocolate";
}
private void button3_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue + buttonValue1);
form2.Show();
this.Hide();
}
}
Form2
public partial class form2 : Form
{
private string _passedValue = "";
private string _passedValue1 = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
listView1.Items.Add(_passedValue1);
}
I want the Chocolate to show underneath Vanilla on the next line.
Because you are concatenating both the strings to one string and passing that to the second forms constructor. What you should do is to pass a list of strings and loop through each one of them and add that to your listView.
So update your second form's constructors to accept a list of strings.
public partial class form2 : Form
{
public form2(List<string> passedValues)
{
InitializeComponent();
foreach(var item in passedValues)
{
listView1.Items.Add(item);
}
}
}
And in your first form,
public partial class form1 : Form
{
private string vanilla = "Vanilla";
private string chocolate= "Chocolate";
private List<string> _values= new List<string>();
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
if (!_values.Contains(vanilla))
{
_values.Add(vanilla);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (!_values.Contains(chocolate))
{
_values.Add(chocolate);
}
}
private void button3_Click(object sender, EventArgs e)
{
form2 form2 = new form2(_values);
form2.Show();
this.Hide();
}
}
EDIT : If you want to allow multiple instances of one string(When user clicks a button more than one time), You can simply remove the if(!_values.Contains( condition check before adding the item to the list. If you want to get the quantity of a string, you need to group it then do the count.
var grouped = _values.GroupBy(k => k, v => v,
(k, v) => new { Name = k, Count = v.Count()}).ToList();
foreach (var item in grouped)
{
var name = item.Name;
var count = item.Count;
//do something with the name and count now.
}
You can do some trick like
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
public static string buttonValue = "";
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanillaaaa";
}
private void button2_Click(object sender, EventArgs e)
{
buttonValue += "~Chocolate";
}
private void button3_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}
public partial class form2 : Form
{
public form2(string passedValue)
{
InitializeComponent();
string[] _passedValue = passedValue.Split('~');
listView1.Items.Add(_passedValue[0]);
listView1.Items.Add(_passedValue[1]);
}
Related
Form1:
public ArrayList listem = new ArrayList(20);
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(listem[0].toString());
} // this is working
I added strings to listem from listem.txt file.
Using MessageBox.Show(listem[0].toString()) on form1 is working.
Form2 :
Form1 frm1 = new Form1();
public Form2()
{
InitializeComponent();
}
private void sayfa_guncelle()
{
Form1 frm1 = new Form1();
MessageBox.Show(frm1.listem[0].toString().toString());
}
private void button5_Click(object sender, EventArgs e)
{
sayfa_guncelle();
} // this is not working
I get an error:
System.ArgumentOutOfRangeException
You can use Application.OpenForms Property to do it.
Code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public ArrayList listem = new ArrayList(5);
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(listem[0].ToString());
Form2 form = new Form2();
form.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
listem.Add(1);
listem.Add(2);
listem.Add(3);
listem.Add(4);
listem.Add(5);
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form =(Form1) Application.OpenForms["Form1"];
string a = form.listem[0].ToString();
MessageBox.Show(a);
}
}
Result:
// GlobalVars.cs
public class GlobalVars
{
// member variables
private static Lazy<GlobalVars> INSTANCE = null;
private ArrayList list = new ArrayList();
// constructor
private GlobalVars()
{
}
// get single instance of GlobalVars from here
public static GlobalVars getInstance()
{
if (INSTANCE == null)
{
INSTANCE = new Lazy<GlobalVars>(() => new GlobalVars());
}
return INSTANCE.Value;
}
// properties
public ArrayList MyList { get => list; set => list = value; }
// add others public properties in here
// ...
}
// Form1.cs
public partial class Form1 : Form
{
private GlobalVars globalVars;
private Form2 form2;
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
// instance of GlobalVars
globalVars = GlobalVars.getInstance();
// set capacity for 4 list
globalVars.MyList.Capacity = 4;
// add list1
if (!globalVars.MyList.Contains("list1"))
{
globalVars.MyList.Add("list1");
}
// add list2
if (!globalVars.MyList.Contains("list2"))
{
globalVars.MyList.Add("list2");
}
// show list1
MessageBox.Show(globalVars.MyList[0].ToString());
// show list2
MessageBox.Show(globalVars.MyList[1].ToString());
// show all list
String items = "";
for (int i = 0; i < globalVars.MyList.Count; i++)
{
items = items + globalVars.MyList[i].ToString() + "\n";
}
MessageBox.Show(items);
}
private void Button2_Click(object sender, EventArgs e)
{
// show form2
if(form2 == null) {
form2 = new Form2();
}
form2.Show();
}
// Form2.cs
public partial class Form2 : Form
{
GlobalVars globalVars;
public Form2()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
// instance of GlobalVars
globalVars = GlobalVars.getInstance();
// add list3
if (!globalVars.MyList.Contains("list3"))
{
globalVars.MyList.Add("list3");
}
// add list4
if (!globalVars.MyList.Contains("list4"))
{
globalVars.MyList.Add("list4");
}
// show list3
MessageBox.Show(globalVars.MyList[2].ToString());
// show list4
MessageBox.Show(globalVars.MyList[3].ToString());
// show all list
String items = "";
for (int i = 0; i < globalVars.MyList.Count; i++)
{
items = items + globalVars.MyList[i].ToString() + "\n";
}
MessageBox.Show(items);
}
}
These are my 2 Forms.
These are the codes for Form 1-->
namespace Passing_Values
{
public partial class Form1 : Form
{
string a="preset value";
public Form1()
{
InitializeComponent();
}
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public void set(string p)
{
MessageBox.Show("This is Entered text in Form 2 " + p);
a = p;
MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
textBox1.Text = "Test 1";
textBox2.Text = a;
textBox3.Text = p;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(a);
}
}
}
These are the codes for Form 2-->
namespace Passing_Values
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
Form1 j = new Form1();
j.set(g);
}
}
}
See the picture.You can understand the design.
This is what I want to do. 1st I open Form2 using button in Form1. Then I enter a text and click the button("Display in Form1 Textbox"). When it's clicked that value should be seen in the 3 Textboxes in Form1.I used Message Boxes to see if the values are passing or not. Values get passed from Form2 to Form1. But those values are not displays in those 3 Textboxes but the passed values are displayed in Message Boxes. Reason for the 3 Text Boxes can be understood by looking at the code. So what's the error?
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
I would simply pass it in the constructor.
So, the code for form2, will be:
public partial class Form2 : Form
{
string _input;
public Form2()
{
InitializeComponent();
}
public Form2(string input)
{
_input = input;
InitializeComponent();
this.label1.Text = _input;
}
}
And the call in Form1 will be:
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2(this.textBox1.Text.ToString());
fm2.Show();
}
public partial class Form1 : Form
{
Form2 fm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2();
fm2.Show();
fm2.button1.Click += new EventHandler(fm2button1_Click);
}
private void fm2button1_Click(object sender, EventArgs e)
{
textBox1.Text = fm2.textBox1.Text;
}
}
And code in form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
set modifier property of textbox1 and button1 to public
Place a static string in your Form2
public static string s = string.Empty;
and, in your Display in Form1 Textbox button click event, get the value from the textbox in your string s:
s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();
once, the Form1 is showed up again, then in the Form1_Load event, just pass your Form2's text value to your Form1's textboxes, the value of which was gotten by the variable s:
foreach (Control text in Controls)
{
if (text is TextBox)
{
((TextBox)text).Text = Form2.s;
}
}
Please, i need help to send data from a textbox to a listview (column quantity) of another form.
I have in form1
namespace officine
{
public partial class FormClav : Form
{
public FormClav()
{
InitializeComponent();
}
......
private void validation_Click(object sender, EventArgs e)
{
// need code here
// onclik thisend textbox1 to listview1 in formOrd
}
I have a lisview1 in a second form
I fill this listview from a dabase after getting bar code.
Then I call form1 (a numeric keyboard) to change quality column..
So I need to send data from Textbox1 (in FormClav ) to lisview1 in FormOrdo
namespace officine
{
public partial class FormOrdo : Form
{
.......
private void loadproduct()
{
listView1.Items.Clear();
cn.Open();
cmd.CommandText = "select * from vente";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string[] row = { dr[1].ToString(), dr[2].ToString()};
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}
}
cn.Close();
}
any idea pls?
You can set a public variable in FormClav and put there the value that you need.
public partial class FormClav : Form
{
public FormClav()
{
InitializeComponent();
}
public string yourvalue = ""
}
private void validation_Click(object sender, EventArgs e)
{
yourvalue = textbox1.text;
}
Try passing a delegate when you instantiate the second form that points to a thread-safe method in the first form that can update your listview.
Hello to past textbox value to another form use this code:
private void validation_Click(object sender, EventArgs e)
{
form2 a = new form2();
a.MdiParent = this.MdiParent; // sets form2 as 'parent window'
a.value = yourTextBox.Text; // sets variable 'value' in form2 equal to yourTextBox value after button is clicked
a.Show(); //opens form2
}
In form 2 generate get set value
public string value { get; set; }
Now you can operate with value easily.
e.g. listView1.Items.Add(value);
On form load adding to listView:
private void Form2_Load(object sender, EventArgs e)
{
listView1.Items.Add(value);
}
Edit:
public partial class Form1 : Form
{
Form2 Frm2;
public Form1()
{
InitializeComponent();
Frm2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
Frm2.Show();
Frm2.textBox1.Text = "From Form1";
}
}
public partial class Form2 : Form
{
Form1 Frm1;
public Form2(Form1 F)
{
InitializeComponent();
Frm1 = F;
}
private void button1_Click(object sender, EventArgs e)
{
Frm1.textBox1.Text = "From Form2";
Frm1.listView1.Items.Add(textBox1.Text);
}
}
My objective is to be able to enter a value in the textbox in form1 then press enter (when pressing the enter button the value will be pass to a method called setvalue. When the switch button is pressed then the form one will hide and open form2. Form2 has two buttons, show and exit. When show is clicked i need to display a messagebox that display the data in textbox in form1 by calling the getvalue method.
Please I'm open for ideas.
This is form one
public partial class Form1 : Form
{
private int secretValue;
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Visible = true;
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
this form two
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Add a constructor value on your form 2. It should look like this:
public Form2(int secValue)
Then you can call it on your form 1 by passing the value
Form2 frm2 = new Form2(secretValue);
Maybe you can assign it to a global variable in your form 2, so that it can be referenced by your code in all of the form. Your form two can now be:
public partial class Form2 : Form
{
int passedValue = 0;
public Form2(int secValue)
{
InitializeComponent();
passedValue = secValue;
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
}
}
In my opinion, there's no need for the property you created. Always remember, the values you assign to a class will be null if you change from form to form.
Following code will not work becuase you are creating new instance of form1 and trying to get the value from there, It will only have the default vaue of int.
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
}
Simply what you can do is to have a public property defined within Form2. and set the value of this property before showing Form2,
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
frm2.SecrectValueOfForm2 = ValueInForm1;
frm2.Visible = true;
this.Hide();
}
Plz try code as shown below:
public partial class Form1 : Form
{
private int secretValue;
Form2 frm2 = new Form2();
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frm2 .Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
frm2.Owner = this;
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
And On Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = ((Form1 )this.Owner).GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Form2
public Int32 _txtval { get; set; }
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(_txtval.ToString());
}
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._txtval = Convert.ToInt32(textBox1.Text);
frm2.Visible = true;
this.Hide();
}
I have a listView1 in Form1 and in Form2 a method which adds elements to listView1 of Form1.
I am getting an error that listView1 does not exist. How can I remove this error.
My code is
Form2:
public static string s;
public void button1_Click(object sender, EventArgs e)
{
s = textBox1.Text;
ListViewItem lvi = new ListViewItem(DodajWindow.s);
listView1.Items.Add(lvi);
this.Close();
}
Please use this sample Code am using 2 Forms,
Code for Form1
public delegate void ListViewAddDelegate(string text);
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void AddItem(string item)
{
listView1.Items.Add(item);
}
private void button1_Click(object sender, EventArgs e)
{
ListViewAddDelegate Del = new ListViewAddDelegate(AddItem);
Form2 ob = new Form2(Del);
ob.Show();
}
}
}
Code for Form2
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public ListViewAddDelegate deleg;
public Form2()
{
InitializeComponent();
}
public Form2(ListViewAddDelegate delegObj)
{
this.deleg = delegObj;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Equals(""))
{
deleg(textBox1.Text);
}
else
{
MessageBox.Show("Text can not be emopty");
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}