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);
}
}
Related
want to make form1 pubId value changed from form2. pubId is always get null didn't change. How can I solve this problem?
Form1 code:
public string pubId = string.Empty;
public void button1_Click(object sender, EventArgs e)
{
try
{
form2 _frm2 = new form2();
_frm2 .FormClosed += _frm2_FormClosed;
}
catch (Exception ex)
{
MessageBox.show(ex);
}
}
private void _frm2_FormClosed(object sender, FormClosedEventArgs e)
{
if (pubId == "8")
{
MessageBox.show("works");
}
}
Form2 code:
public void buttonsend_Click(object sender, EventArgs e)
{
idfrm2 = "8";
form1 _frm1 = new form1 ();
_frm1.pubId = _idfrm2;
this.Close();
}
In this line you create a new form with new empty pubId.
form1 _frm1 = new form1 ();
Just create a constructor for form2 to pass it.
For example:
private string _pubId;
public form2(string pubId)
{
_pubId = pubId;
}
Then you can use it:
form2 _frm2 = new form2(pubId);
You can maintain reference to Form2 inside a List of controls maintained by Form1 and access it as below :
Form1 Code :
public partial class Form1 : Form
{
Form2 localfrm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FormClosed += Frm2_FormClosed;
localfrm2 = frm2;
frm2.Show();
}
private void Frm2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show(localfrm2.PubId);
}
}
Form 2 :
public partial class Form2 : Form
{
public string PubId { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PubId = textBox1.Text;
}
}
Upvote if this solves your problem
I have a Form1 where I create Medic objects and add them to listaMedici1. In Form2 I create Pacient objects, added them to listapacienti2 and them send that list to Form1.
In Form1 I have 2 listboxes: In listbox1_medici i show the list of Medic objects.
What I want to do is that when I select a Medic object from listbox1_medici, in listbox2_pacienti to show all the Pacient objects, that have the field codM, the same as the the selected object field codM. I have tried this method, but it doesn't work. What could be wrong? Thank you.
Form1:
Button3 is used to populate listbox1_medici with Medic objects
And the method SelectieItem and event SelectedIndexChanged were supposed to show the Pacient objects that have the same codM as the Medic object selected in listbox1_medici..but they don't work
public partial class Form1 : Form
{
public List<Medic> listaMedici1 = new List<Medic>();
public List<Pacient> listaPacienti1 = new List<Pacient>();
public ArrayList specialitati = new ArrayList();
public Form1()
{
InitializeComponent();
specialitati.Add("Endocrinologie");
specialitati.Add("Oftalmologie");
specialitati.Add("Neurologie");
specialitati.Add("Cardiologie");
for (int i = 0; i < specialitati.Count; i++)
comboBox1spec.Items.Add(specialitati[i]);
Medic m1 = new Medic(1, "Ionescu", "Endocrinologie");
Medic m2 = new Medic(2, "Popescu", "Cardiologie");
listaMedici1.Add(m1);
listaMedici1.Add(m2);
}
private void button1_Click(object sender, EventArgs e)
{//this is where i create objects using the textboxes
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2(listaMedici1);
// form.listaMedici2 = listaMedici1;
form.ShowDialog();
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
foreach (Medic m in listaMedici1)
{
if(!(listBox1_medici.Items.Contains(m)))
{
listBox1_medici.Items.Add(m.ToString());
}
}
}
private void SelectieItem(object sender, EventArgs e)
{
if(listBox1_medici.SelectedItems.Count==1)
{
Medic med = listBox1_medici.SelectedItem as Medic;
for(int i=0;i<listaPacienti1.Count;i++)
{
if(med.CodM==listaPacienti1[i].CodM)
{
listBox2_pacienti.Items.Add(listaPacienti1[i].ToString());
}
}
}
}
private void listBox2_pacienti_SelectedIndexChanged(object sender, EventArgs e)
{
SelectieItem(sender, e);
}
}
Form2:
public partial class Form2 : Form
{
public List<Medic> listaMedici2 = new List<Medic>();
public List<Pacient> listaPacienti2 = new List<Pacient>();
Form1 form1;
public Form2()
{
InitializeComponent();
Pacient p1 = new Pacient(1, "Danut", 2, 230);
Pacient p2 = new Pacient(2, "Cristi", 1, 120);
listaPacienti2.Add(p1);
listaPacienti2.Add(p2);
}
public Form2(List<Medic> listaMedici1)
{
InitializeComponent();
listaMedici2 = listaMedici1;
foreach (Medic med in listaMedici2)
{
comboBox1codmed.Items.Add(med.CodM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{//this is where i create Pacient objects using the textboxes
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.listaPacienti1 = listaPacienti2;
frm.ShowDialog();
this.Close();
}
}
I am developing window application POS. Requirement is: when user click on button 'seach item' on _mainform(form 1) then it open _Searchform(form2) and on the search form it display result in listview with select item button on form3 and close _searchform(form2). The item that we selected from listview , we add in _mainform(form1) listview.
I try to implement this functionality with delegate and event. On form3 (search result form) i have declared delegate and event and subscribe that even on form1(main form). but when i run application event on form 1 dot get fired.
following this code:
_mainform(form1):
namespace KasseDelegate
{
public delegate void ListViewUpdatedEventHandler(object sender, ListViewUpdatedEventArgs e);
public partial class Form1 : Form
{
private Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3();
frm3.ListViewUpdated += new ListViewUpdatedEventHandler(Frm3_ListViewUpdated1);
}
private void Frm3_ListViewUpdated1(object sender, ListViewUpdatedEventArgs e)
{
MessageBox.Show("hi");
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
_searchform(form2) :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(Properties.Settings.Default.ConnectionString))
{
con.Open();
SQLiteCommand cmd = new SQLiteCommand("select * from varer where varenummer=#Varenummer", con);
cmd.Parameters.AddWithValue("#Varenummer", "101");
SQLiteDataReader dr = cmd.ExecuteReader();
Form3 frm3 = new Form3(dr);
frm3.Show();
}
}
}
form 3:
namespace KasseDelegate
{
public partial class Form3 : Form
{
public event ListViewUpdatedEventHandler ListViewUpdated;
SQLiteDataReader dr1;
public Form3()
{
InitializeComponent();
}
public Form3(SQLiteDataReader dr)
{
InitializeComponent();
dr1 = dr;
}
private void Form3_Load(object sender, System.EventArgs e)
{
if (dr1 != null)
{
while (dr1.Read() == true)
{
ListViewItem LVI = new ListViewItem();
LVI.SubItems.Add(dr1[0].ToString());
LVI.SubItems.Add(dr1[1].ToString());
LVI.SubItems.Add(dr1[2].ToString());
LVI.SubItems.Add(dr1[3].ToString());
LVI.SubItems.Add(dr1[4].ToString());
listView1.Items.Add(LVI);
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
string sVareNummer = listView1.SelectedItems[0].SubItems[1].Text;
string sBeskrivelse = listView1.SelectedItems[0].SubItems[2].Text;
string pris = listView1.SelectedItems[0].SubItems[4].Text;
string enpris = listView1.SelectedItems[0].SubItems[5].Text;
if (ListViewUpdated != null)
{
ListViewUpdated(this, new ListViewUpdatedEventArgs() { VareNummer1 = sVareNummer, Beskrivelse1 = sBeskrivelse, Pris1 = pris, Enpris1 = enpris });
}
}
}
public class ListViewUpdatedEventArgs : System.EventArgs
{
private string VareNummer;
private string Beskrivelse;
private string pris;
private string enpris;
public string VareNummer1
{
get
{
return VareNummer;
}
set
{
VareNummer = value;
}
}
public string Beskrivelse1
{
get
{
return Beskrivelse;
}
set
{
Beskrivelse = value;
}
}
public string Pris1
{
get
{
return pris;
}
set
{
pris = value;
}
}
public string Enpris1
{
get
{
return enpris;
}
set
{
enpris = value;
}
}
}
}
So how i can get values of listview selected item from form3(displayitem) to form1.(mainform).
If I understand it correctly you have Form1 - which launches Form2 which in turn launches Form3. On selecting some item on Form3 you need to update it back on Form1.
So declare a delegate on Form1. Pass this delegate to Form2 [Form2 constructor should accept this as a default parameter - so that if Form2 is triggered from some other place we need not have hard dependency on delegate.]
maintain a private variable of this delegate type on Form2 and while launching Form3 pass the same delegate to Form3 constructor.
On Form3 you have the delegate now which is holding a reference to a method on From1, so whenever an item is selected you can assign this delegate on Form3 which will fire the method on Form1.
so here is an working example.
public delegate void MyDelegate(string selectedItem);
public class Form1
{
private MyDelegate delegate1;
public Form1()
{
delegate1 = new MyDelegate(ShowSelectedItem);
var form2 = new Form2(delegate1);
}
public void LaunchForm2()
{
}
private void ShowSelectedItem(string result)
{
}
}
public class Form2
{
private MyDelegate form2Delegate;
public Form2(MyDelegate del = null)
{
form2Delegate = del;
var form3 = new Form3(form2Delegate);
}
public void LaunchForm3()
{
}
}
public class Form3
{
private MyDelegate form3Delegate;
public Form3(MyDelegate del = null)
{
form3Delegate = del;
SelectedItemTriggered("tes");
}
public void SelectedItemTriggered(string selectedItem)
{
form3Delegate(selectedItem);
//This will trigger method ShowSelectedItem of Form1
}
}
You could set up an event in your Form3 and access it from outside like this:
public partial class Form2 : Form
{
public Form2()
{
Form3 new3 = new Form3();
// Access the event of form3 from outsite
new3.DisplayedItemChanged += ItemChanged;
}
public void ItemChanged(object sender, EventArgs e)
{
// This will be triggered.
}
}
public class Form3 : Form
{
// Create an event
public event EventHandler DisplayedItemChanged;
public void button1_Click(object sender, EventArgs e)
{
if (this.DisplayedItemChanged != null)
{
// Raise the event and pass any object
this.DisplayedItemChanged(yourObjectToPass, e);
}
}
}
According your comments below:
public partial class Form1 : Form
{
public Form1()
{
// This one is an instance of Form3
Form3 newForm = new Form3();
newForm.SomethingHappens += RaiseHere;
}
public void RaiseHere(object sender, EventArgs e)
{
// Do something...
}
}
public partial class Form2 : Form
{
public Form2()
{
// This one is NOT the same as on Form1. Its a NEW form.
Form3 newForm = new Form3();
newForm.Show();
}
}
public partial class Form3 : Form
{
public event EventHandler SomethingHappens;
public Form3()
{
//...
}
}
This wont work, never. You have to use the same instance:
Form3 newForm = new Form3();
newForm.SomethingHappens += RaiseHere;
newForm.Show();
If this isnt clear for you I cant help, sorry:
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]);
}
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)
{
}
}
}