I have a listview in my Mainform and I need to get the value in the textbox and label in the other form name Add_Order?
Add_Order add = new Add_Order();
ListViewItem item = new ListViewItem();
item.Text = add.textBox3.Text;
item.SubItems.Add(add.label6.Text);
item.SubItems.Add(add.textBox2.Text);
item.SubItems.Add(add.textBox1.Text);
item.SubItems.Add(add.textBox3.Text);
mainform.listView2.Items.Add(item);
I personally would not expose the controls in your Add_Order Form. Your calling Form should not be aware of the internals of the Add_Order Form, only its public Methods and Properties. I would make a Public Method and use that to retrieve the information you need. something like this:
Add_Order.cs
public partial class Add_Order : Form
{
public Add_Order()
{
InitializeComponent();
}
public List<string> GetData()
{
List<string> list = new List<string>();
list.Add(textBox3.Text);
list.Add(label6.Text);
list.Add(textBox2.Text);
list.Add(textBox1.Text);
return list;
}
}
MainForm
private void button1_Click(object sender, EventArgs e)
{
Add_Order add = new Add_Order();
add.ShowDialog();
ListViewItem item = new ListViewItem();
List<string> data = add.GetData();
item.Text = data[0];
item.SubItems.Add(data[1]);
item.SubItems.Add(data[2]);
item.SubItems.Add(data[3]);
item.SubItems.Add(data[0]);
listView2.Items.Add(item);
}
You can pass the data to other forms in different ways like creating public classes to maintain data common data between forms or you can pass data using form constructor like :
Add_Order frmAddOrder=new Add_Order(data1,data2);
frmAddOrder.show();
and in your Add_Order Constructor :
public Add_Order (string data1,string data2)
{
InitializeComponent();
//you can access data1 and data2 here ...
}
I write a simple one for you:
Set element Modifiers to true in Add_Order form :
and get it in main form:
public partial class main : Form
{
public main()
{
InitializeComponent();
Get_Frm2_Data();
}
private void Get_Frm2_Data()
{
Add_Order frm2 = new Add_Order();
List<string> info= new List<string>;
info.Add( frm2.textBox1.Text);
.
.
.
}
}
edit
or make an structure:
Add_Order.cs
public partial class Add_Order : Form
{
public Add_Order()
{
InitializeComponent();
}
public Info Get_Data()
{
return new Info() { _textBox3 = textBox3.Text,
_label6 = label6.Text,
_textBox2 = textBox2.Text,
_textBox1 = textBox1.Text,
};
}
}
struct Info
{
public string _textBox3;
public string _label6;
public string _textBox2;
public string _textBox1;
}
Mainform.cs
public partial class main : Form
{
public main()
{
InitializeComponent();
Get_Frm2_Data();
}
private void Get_Frm2_Data()
{
Add_Order frm2 = new Add_Order();
frm2.ShowDialog();
Info lst_data= frm2.Get_Data();
ListViewItem item = new ListViewItem();
item.Text = lst._textBox3;
item.SubItems.Add(lst._label6);
item.SubItems.Add(lst._textBox2);
item.SubItems.Add(lst._textBox1);
mainform.listView2.Items.Add(item);
}
}
Related
I know how to send value Form1 to Form2 like and it works fine;
FORM1;
public static string sirket = "";
public static string personeladı = "";
public static string desc = "";
public static string toplampay = "";
private void prew_Click(object sender, EventArgs e)
{
List<ListViewItem> myList = new List<ListViewItem>();
foreach (ListViewItem lvi in this.listView1.Items)
{
myList.Add(lvi);
}
sirket = companyname.Text;
personeladı = personelcombo.Text;
desc = paydesc.Text;
toplampay = totalpay.Text;
Form2 frm2 = new Form2(listView1);
frm2.Show();
}
FORM2 ;
string com = Form1.sirket;
string pers = Form1.personeladı;
string descrip = Form1.desc;
string toppay = Form1.toplampay;
My question is I have a form name Form1(it is a form) and I wrote Listviewprint.cs(not Form).I want to send value Form1 to Listviewprint.cs but when I try to use like above it give me error ;
The name 'Form1' does not exist in the current context.
How can I send values Form1 to Listviewprint.cs
Not 100% clear for me what you really would like to do, but I think this will help you. Comment something if it's not clear for you.
Sou you have the Form1 class, and you declare your ListViewPrint in it. After that you can set it's public properties in Form1.
public partial class Form1 : Form
{
private ListViewPrint _mylistviewPrint;
public Form1()
{
InitializeComponent();
_mylistviewPrint = new _mylistviewPrint(/*or with your parameters*/);
}
public void DoSomethingInForm1()
{
_mylistviewPrint.SomeVariable = 52;
}
}
public class ListViewPrint
{
private int _somevariable;
public int SomeVariable
{
get
{
return _somevariable;
}
set
{
_somevariable = value;
}
}
private string _othervariable;
/// like the above
}
My situation:
I have a populated checklistbox control on Form1.
Then I have a listView control on Form2.
I would like for the user to be able to check items on the checklistbox on Form1, then click on a button on Form1 to open Form2.
Form2 contains the listView control, that I want to populate with only the checked items from checklistbox on Form1.
I tried
namespace Boodschappenlijst
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string[] strKruideniersw = new string[] { Boodschappenlijst.producten[0], Boodschappenlijst.producten[1], Boodschappenlijst.producten[2] };
public static string[] strVerswaren = new string[] { Boodschappenlijst.producten[3], Boodschappenlijst.producten[4], Boodschappenlijst.producten[5] };
public static string[] strVerzorgingspr = new string[] { Boodschappenlijst.producten[6], Boodschappenlijst.producten[7], Boodschappenlijst.producten[8], Boodschappenlijst.producten[9] };
public static List<string> kruidenierswList = new List<string>(strKruideniersw);
public static List<string> verswarenList = new List<string>(strVerswaren);
public static List<string> verzproductenList = new List<string>(strVerzorgingspr);
public static string[] strKruidenierswCh;
public void Form1_Load(object sender, EventArgs e)
{
clbKruidenierswaren.Items.AddRange(strKruideniersw);
clbVerswaren.Items.AddRange(strVerswaren);
clbVerzproducten.Items.AddRange(strVerzorgingspr);
strKruidenierswCh = clbKruidenierswaren.CheckedItems;
}
// TODO
// public string kruidenierswChecked = clbKruidenierswaren.CheckedItems;
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 form2 = new Form2();
// Show the settings form
form2.Show();
}
}
public abstract class Boodschappenlijst : Form1
{
public static string[] producten = new string[] { "Peper", "Zout", "Kruidnagel", "Sla", "Komkommer", "Tomaten", "Tandpasta", "Shampoo", "Wax", "Deodorant" };
// Not working.. clbKruidenierswaren is not static.
List<string> items = clbKruidenierswaren.CheckedItems.Cast<string>().ToList();
// Make form1 controls accessible for other classes?
// Form1 form1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
}
}
but the I get the error
A field initializer cannot reference the non-static field, method, or property 'Form1.clbKruidenierswaren'.
Can you please direct me to a solution that works?
You should make a constructor in the class like this:
Class itself:
public class Boodschappenlijst
{
public static string[] producten { get; set; } = new string[] { "Peper", "Zout", "Kruidnagel", "Sla", "Komkommer", "Tomaten", "Tandpasta", "Shampoo", "Wax", "Deodorant" };
private List<string> Items { get; set; }
public Boodschappenlijst(List<string> items)// << Constructor
{
Items = items;
}
}
And then make an instance the class like so:
On the place (Form?) you have clbKruidenierswaren:
Boodschappenlijst boodschappenLijst =
new Boodschappenlijst(clbKruidenierswaren.CheckedItems.Cast<string>().ToList());
The problem is you're passing UI objects around, you should be passing data around instead. Here's an example of a Form that can be given data during construction.
public class BaseForm : Form
{
public object InitialisationData { get; set; }
}
public partial class MagicForm : BaseForm
{
public string MyBindableGuy;
public MagicForm()
{
InitializeComponent();
MyBindableGuy = InitialisationData as string;
}
}
and open it with:
var myForm = new MagicForm();
myForm.InitialisationData = "Hi, I'm a string.";
myForm.Show();
public partial class Form1 : Form
{
// Todo declare the variables
private List<string> kruidenierswList;
private List<string> verswarenList;
private List<string> verzproductenList;
public Form1()
{
InitializeComponent();
// call the instance once and add that to the variable lijst
Boodschappenlijst lijst = Boodschappenlijst.Instance; // <- # others on S/O this is just used as information I know I could do a new as well.
// initialize the variables
kruidenierswList = new List<string>() { lijst.Products[0], lijst.Products[1], lijst.Products[2] };
verswarenList = new List<string>() { lijst.Products[3], lijst.Products[4], lijst.Products[5] };
verzproductenList = new List<string>() { lijst.Products[6], lijst.Products[7], lijst.Products[8], lijst.Products[9] };
}
public void Form1_Load(object sender, EventArgs e)
{
// populate the checklist boxes
clbKruidenierswaren.Items.AddRange(kruidenierswList.ToArray());
clbVerswaren.Items.AddRange(verswarenList.ToArray());
clbVerzproducten.Items.AddRange(verzproductenList.ToArray());
}
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 form2 = new Form2();
// Show the settings form
form2.Show();
}
}
public class Boodschappenlijst
{
private static Boodschappenlijst instance;
public string[] Products
{
get;
private set;
}
private Boodschappenlijst()
{
Products = new string[] { "Peper", "Zout", "Kruidnagel", "Sla", "Komkommer", "Tomaten", "Tandpasta", "Shampoo", "Wax", "Deodorant" };
}
public static Boodschappenlijst Instance
{
get
{
// singleton initialization => look for design pattern - singleton <- Design patterns can brighten your day.
//
return instance == null ? instance = new Boodschappenlijst() : instance;
}
}
}
I have 2 forms, form1 and form2.
form1 has 2 buttons, btnVanilla and btnConfirm.
form2 has a listView, listView1.
On form1 when vanilla is clicked once, it should show "1" in Quantity column next to Vanilla.
But at the moment it is showing "1" underneath vanilla. How do I move it across onto the next column?
There is a screenshot which shows what I mean.
form1
public partial class form1 : Form
{
private string vanilla = "Vanilla";
private List<string> _values = new List<string>();
public form1()
{
InitializeComponent();
}
int vanillaCount = 0
public void btnVanilla_Click(object sender, EventArgs e)
{
if (!_values.Contains(vanilla))
{
_values.Add(vanilla);
}
vanillaCount++;
}
private void btnConfirm_Click(object sender, EventArgs e)
{
form2 frm2 = new form2(_values, vanillaCount);
frm2.Show();
this.Hide();
}
}
form2
public partial class form2 : Form
{
private List<string> _values;
public form2(List<string> _values)
{
this._values = _values;
}
public form2(List<string> passedValues, int vanillaCount)
{
InitializeComponent();
foreach (var item in passedValues)
{
listView1.Items.Add(item);
}
listView1.Items[0].SubItems[1].Text = vanillaCount.ToString();
}
}
Are you sure that this line
listView1.Items[0].SubItems[1].Text = vanillaCount.ToString();
is correct? You aren't creating subitem anywhere, it should be like
listView1.Items[0].SubItems.Add(vanillaCount.ToString());
One solution for your actual problem:
public Form2(List<string> passedValues, int vanillaCount)
{
InitializeComponent();
// This line let you show more Columns
listView1.View = View.Details;
// Define your needed Columns
listView1.Columns.Add("Item-Name", -2); //(the width with -2 means, that the column will be autosized)
listView1.Columns.Add("Quantity");
foreach (var item in passedValues)
{
// Create a new ListViewItem "Vanilla", add your needed Subitems like Quantity, Price, ...
var newItem = new ListViewItem(item);
newItem.SubItems.Add(vanillaCount.ToString());
// add the new Item to your ListView
listView1.Items.Add(newItem);
}
}
But you should create a list with a own object-type like
public class Product
{
public string Name { get; set; }
public int Quantity { get; set; }
}
For your problem use like this:
ListViewItem row = new ListViewItem();
row.SubItems.Add(value.ToString());
listview1.Items.Add(row);
you repeat the second row as many times as you have columns in the listview
When I go to run the program it is empty, no data displayed
namespace _0000003
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
soulpets = new ListView();
ListViewItem lvi6001 = soulpets.Items.Add("6001");
lvi6001.SubItems.Add("Tough Ent, Rare");
ListViewItem lvi6004 = soulpets.Items.Add("6004");
lvi6004.SubItems.Add("Stone Fist Ent, Rare");
ListViewItem lvi6007 = soulpets.Items.Add("6007");
lvi6007.SubItems.Add("Healing Ent,Rare");
Controls.Add(soulpets);
soulpets = new ListView();
should be soulpets.Items.Clear();
You probably want:
add a listview via the designer with the name soulpets
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
fillListViewSoulpets();
}
private void fillListViewSoulpets()
{
soulpets.Items.Clear(); //it should already be empty
soulpets.Items.Add(
new ListViewItem({ "Tough", "Ent", "Rare" }),
new ListViewItem({ "Stone", "Fist", "Ent", "Rare" }),
new ListViewItem({ "Healing", "Ent", "Rare" })
);
}
}
I generated a list on form1 who's contents are numbers and a letter in the form of K000-000. I then want to open form2. On form 2 I have a text box, a list box and a button. In the text box you will type some more number in like 12345. When you click the button I want it to add the contents of form1's list with a "-" on the end and the contents you typed in Form2's textbox. So the listbox will be K000-000-12345. I'm not sure how to properly use Form1's list on Form2 and also add to it.
Form1
DesignNo.FindByItem(electype, (int.Parse)(dwgno));
List<DesignNo> Electrode = DesignNo.FindByItem(electype, (int.Parse)(dwgno));
if (Electrode.Count <= 0)
{
MessageBox.Show("Unknown Electrode Number");
}
frmElectrode frmelec = new frmElectrode();
frmelec.Show();
frmelec being Form2 in the example.
1-Using Static property
public static List<int> list;
public Form1()
{
list=new List<int>();
InitializeComponent();
}
in Form2 access list like this
Form1.list.Add(item);
2-using constructor
public static List<int> list;
public Form1()
{
list=new List<int>();
InitializeComponent();
}
public void ShowForm2()
{
var form2=new Form2(List);
form2.show();
}
in Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public static List<int> _list;
public Form2(List<int> list)
{
_list=list;
InitializeComponent();
}
}
create a public property inside Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string Content { get; private set; }
public void ButtonOkOnClick()
{
this.Content = this.textBox1.Text;
this.Close();
}
}
Consume the public property in Form1 after Form2 gets closed
public Form1()
{
InitializeComponent();
Form2 form2 = new Form2();
form2.Show();
form2.Closed += (sender, args) => this.list.Add(form.Content);
}
This way the dependency is the right direction, form2 is just a input form and can be reused without any dependencies.
A clean way to share some state (in your case dictionary of keys) is via some shared service (singleton), so:
You could create a class (e.g. ElectrodeManager) which will hold a dictionary of electrodes (empty in the first place).
In the Form1 you will populate that dictionary via specified method on that class (e.g. AddElectrode(string electrodeType, string electrodeKey) -> which will add new item into the dictionary)
- so you will have Dictionary<string, string> that holds e.g. {"T1", "K000-000"}, {"T2", "K000-0001"} ...
In the Form2 you will work that dictionary from the ElectrodeManager and you will append string from the textbox onto electrode's key.
Example:
public class ElectrodeManager
{
#region Singleton Pattern
private static ElectrodeManager instance;
public static ElectrodeManager Instance
{
get
{
if (instance == null)
instance = new ElectrodeManager();
return instance;
}
}
private ElectrodeManager()
{
electrodes = new Dictionary<string, string>();
}
#endregion
#region Fields
private Dictionary<string, string> electrodes;
#endregion Fields
#region Methods
public void AddElectrode(string eType, string eKey)
{
if (!electrodes.ContainsKey(eType))
{
electrodes.Add(eType, eKey);
}
}
public void AppendStringToElectrodeKey(string eType, string keyAddendum)
{
string electrodeKey = String.Empty;
if (electrodes.TryGetValue(eType, out electrodeKey))
{
electrodes[eType] = String.Format("{0}-{1}", electrodes[eType], keyAddendum);
}
}
public IDictionary<string, string> GetElectrodes()
{
return electrodes;
}
#endregion Methods
}
Usage inside Form1 (somewhere in generation logic):
ElectrodeManager.Instance.AddElectrode("T1", "K000-000");
ElectrodeManager.Instance.AddElectrode("T2", "K000-001");
Inside Form2 (button click):
ElectrodeManager.Instance.AppendStringToElectrodeKey("T1", textBox.Text);
ElectrodeManager.Instance.AppendStringToElectrodeKey("T2", textBox.Text);
Of course, you could easily switch data type to the List<string> if that suits you better.