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)
{
}
}
}
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 this form with a button and َPanel. When I press the button I want to add a Row to dataGridView1
User control code:
public partial class UserControl1 : UserControl
{
private static UserControl1 _instance;
public static UserControl1 Instance
{
get
{
if (_instance == null)
_instance = new UserControl1();
return _instance;
}
}
public UserControl1()
{
InitializeComponent();
}
public void setRow()
{
String[] row = { "TEST", "TEST", "TEST", "TEST" };
dataGridView1.Rows.Add(row);
}
}
Form1 code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
panel1.Controls.Add(new UserControl1());
UserControl1.Instance.Dock = DockStyle.Fill;
UserControl1.Instance.BringToFront();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 user = new UserControl1();
user.setRow();
}
private void button2_Click(object sender, EventArgs e)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 user = new UserControl1();
user.setRow();
}
Should be
private void button1_Click(object sender, EventArgs e)
{
UserControl1.Instance.setRow();
}
and constructor of UserControl1 should look like
public UserControl1()
{
_instance = this;
InitializeComponent();
}
In your code you are creating 3 instances of your UserControl and it seems like you want to only have one. When you call a method on one of those instances it doesnt in no way influence the other instances.
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 2 forms, Form1 is the parent and ALog is the child. My goal is to have a textbox's text from Form1 (form1textbox) contents transfer over to a textbox on ALog (alogcheckbox)
This has to be done on the formload event on Alog and when the form shows from a button click on Form1
This is what I have currently:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string LabelText
{
get { return form1textbox.Text; }
set { form1textbox.Text = value; }
}
private void button1_Click(object sender, EventArgs e)
{
ALog alogform = new ALog();
alogform.Show();
}
}
ALog:
public partial class ALog : Form
{
public ALog()
{
InitializeComponent();
}
public Form Alog;
private void button1_Click(object sender, EventArgs e)
{
}
private void ALog_Load(object sender, EventArgs e)
{
this.Form1.LabelText = textBox1.Text;
}
}
I've seen other questions similar to mine and answers as well, but I can't seem to manage to get this to work.
Any help is appreciated, thanks.
You want to add a constructor to ALog that takes the value, and initialize it that way.
ALog becomes:
public partial class ALog : Form
{
public ALog(string value)
{
InitializeComponent();
this.alogcheckbox.Text = value;
}
public Form Alog;
private void button1_Click(object sender, EventArgs e)
{
}
}
And from Form1:
private void button1_Click(object sender, EventArgs e)
{
ALog alogform = new ALog(form1textbox.Text);
alogform.Show();
}
Trying to get this active in another form. I want the objects from listBox1 to go to the labels: label3 and label4 in form2. PropertyA should assign to label3 and PropertyB to label4. It will simply load the data from form1. The question is in the form2 code. When an item is selected form the listbox it will write the properties for prepare them for the next form2 instance.
Most of this is fairly short and simple code...
Made a simple program to illustrate...
FOUND THE SOLUTION.... SEE FORM2, it was a problem of casting between forms the listbox1.SelectedItems as object of SpecialClass type.
Class
namespace WindowsFormsApplication1
{
class SpecialClass
{
public string PropertyA { get; set; }
public double PropertyB { get; set; }
#region [Methods]
public override string ToString()
{
return string.Format("PropertyA: {0}, PropertyB: {1}", this.PropertyA, this.PropertyB);
}
#endregion
}
}
Form1
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void AddBtn_Click(object sender, EventArgs e)
{
SpecialClass o = new SpecialClass();
o.PropertyA = textBox1.Text;
o.PropertyB = double.Parse(textBox2.Text);
listBox1.Items.Add(o);
}
#region [Currently unused methods]
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
#endregion
private void openNewForm(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show(this);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SpecialClass o = ((ListBox)sender).SelectedItem as SpecialClass;
label3.Text = o.PropertyA.ToString();
label4.Text = o.PropertyB.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Form2
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 form1Data = new Form1();
public Form2(Form1 _form1)
{
InitializeComponent();
form1Data = _form1;
}
private void label3_Click(object sender, EventArgs e)
{
SpecialClass oF2 = form1Data.listBox1.SelectedItem as SpecialClass;
label3.Text = oF2.PropertyA.ToString();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}