Data from form1 listbox to form2 textbox - c#

The goal of the program assignment I'm working on requires me to fill a listbox with items taken from a data file, and then allowing the user to modify parts of the selected item. To do this, I need assistance in figuring out how to pass part of a listbox's selected item in one form to a textbox in another form.
Here's the coding I have for the first form in my program:
public partial class Form1 : Form
{
const char DELIM = '\\';
const string FILENAME = #"C:\Visual Studio 2015\Data Files\Training Workshops data";
string recordIn;
string[] Fields;
static FileStream file = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);
public int X;
public Form1()
{
InitializeComponent();
}
public class Workshop
{
public string title { get; set; }
public int days { get; set; }
public string categrory { get; set; }
public double cost { get; set; }
public string[] categrorynames =
{
"Application Development",
"Databases",
"Networking",
"System Administration"
};
}
Workshop shop = new Workshop();
private void button1_Click(object sender, EventArgs e)
{
Form2 secondForm = new Form2();
secondForm.Show();
}
private void PopulateList(string filePath)
{
while(recordIn != null)
{
try
{
recordIn = reader.ReadLine();
Fields = recordIn.Split(DELIM);
X = Convert.ToInt32(Fields[0]);
shop.categrory = shop.categrorynames[X];
shop.days = Convert.ToInt32(Fields[1]);
shop.title = Fields[3];
shop.cost = Convert.ToDouble(Fields[2]);
}
catch (Exception A)
{
if (X < 0 && X > 3)
{
shop.categrory = "invalid";
}
if (shop.days != Convert.ToInt32(Fields[1]))
{
shop.days = 0;
}
if (shop.title != Fields[3])
{
shop.title = "invalid";
}
if (shop.cost != Convert.ToDouble(Fields[2]))
{
shop.cost = 0;
}
}
}
}
}
And below is a link to a screenshot of the second form:
http://i.stack.imgur.com/IRqVh.png
I need to transfer the shop.categrory data of form1's listbox's selected item to the second form's, and the shop.title, shop.days and shop.cost to the corresponding textbox's. From there I can make it so that whatever the user enters in the textboxes would change the data of the selected item when they press the "save and exit" button.
Any help would be appreciated, and if anyone notices an error in the coding I have now, please feel free to point them out.

Create a Parameterized constructor in form2 that accepts a String, and create it's instance in first form, and pass the value you want to pass to the second form:
private void button1_Click(object sender, EventArgs e)
{
Form2 secondForm = new Form2("DATA TO BE SENT");
secondForm.Show();
}
and in form2 Create a Constructor:
public Form2(string data)
{
InitializeComponent();
txtData.Text=data;
}

Related

How do I get a label in a form to display the values of a property

In my C# windows form I have 2 forms. I would like to display a collection of strings in a label on a form. When I debug I show the 2 elements in my array but they are not showing in the label I am passing it to. When I hover of toString the data is there but how to I pass it to sender so it will display in the label control I have on my form?
In the snippet of code below to data is in toString but how do I get it from there down to sender.ToString????
public AccountReport_cs(Func<string> toString)
{
this.toString = toString;
}
private void AccountReport_cs_Load(object sender, EventArgs e)
{
label1.Text = sender.ToString();
}
This is another piece of the code that will open form2 where the information should be displayed.
private void reportButton2_Start(object sender, EventArgs e)
{
AccountReport_cs accountReport = new AccountReport_cs(allTransactions.ToString);
accountReport.ShowDialog();
}
Here is the last piece of code and this will show how the data gets to EndOfMonth.
public class Transaction
{
public string EndOfMonth { get; set; }
}
public override List<Transaction> closeMonth()
{
var transactions = new List<Transaction>();
var endString = new Transaction();
endString.EndOfMonth = reportString;
transactions.Add(endString);
return transactions;
}
If you need to send information between forms, the best thing you can do is create a property in the target form and assign the value you want to send before displaying the form; thus you will not need to change the default constructor of the form.
// Destiny form
public partial class FormDestiny : Form {
// Property for receive data from other forms, you decide the datatype
public string ExternalData { get; set; }
public FormDestiny() {
InitializeComponent();
// Set external data after InitializeComponent()
this.MyLabel.Text = ExternalData;
}
}
// Source form. Here, prepare all data to send to destiny form
public partial class FormSource : Form {
public FormSource() {
InitializeComponent();
}
private void SenderButton_Click(object sender, EventArgs e) {
// Instance of destiny form
FormDestiny destinyForm = new FormDestiny();
destinyForm.ExternalData = PrepareExternalData("someValueIfNeeded");
destinyForm.ShowDialog();
}
// Your business logic here
private string PrepareExternalData(string myparameters) {
string result = "";
// Some beautiful and complex code...
return result;
}
}

refresh after .showDialog and show items in list - c# .NET

Currently in my program a user opens form 1 to create a new instance of a class and it is then saved to a list. After form 1 closes I would like the main form to reload and show the updated list on the screen. I am having trouble figuring out how to refresh the main navigation and how I would get the list to show on the form.
MainNaviagation
public partial class MainNavigation : Form
{
private Model m_modelObj;
public MainNavigation(Model modelObj)
{
InitializeComponent();
m_modelObj = modelObj;
m_modelObj.ChocolateAdded += m_modelObj_ChocolateAdded;
}
void m_modelObj_ChocolateAdded(Chocolate newChocolate)
{
//whole list of chocolates
List<Chocolate> chocolateList = m_modelObj.ChocolateList;
}
private void button1_Click(object sender, EventArgs e)
{
string candy = comboBox1.SelectedItem.ToString();
Form1 aForm1 = new Form1(textBox1.Text, candy, m_modelObj);
aForm1.ShowDialog();
}
}
Model Class:
{
public delegate void ChocolateAddedEventHander(Chocolate newChocolate);
public class Model
{
public event ChocolateAddedEventHander ChocolateAdded;
public List<Chocolate> ChocolateList = new List<Chocolate>();
public void AddChocolateInList(Chocolate chocolate)
{
ChocolateList.Add(chocolate);
if (ChocolateAdded != null)
ChocolateAdded(chocolate);
}
}
form1
public partial class Form1 : Form
{
Model m_model;
public Form1(string name, string candy, Model modelObj)
{
InitializeComponent();
m_model = modelObj;
string str = name + " selected : ";
label1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Chocolate newChocolate = new Chocolate(comboBoxChocolateSelection.SelectedItem.ToString(), 12.5, true, 2);
m_model.AddChocolateInList(newChocolate);
this.Close();
}
}
chocolates
public class Chocolate
{
#region Fields
public string flavor;
public double cost;
public bool giftWrap;
public int quantity;
#endregion End of Fields
#region Constructors
public Chocolate(string flavor, double cost, bool giftWrap, int quantity)
{
this.flavor = flavor;
this.cost = cost;
this.giftWrap = giftWrap;
this.quantity = quantity;
}
#endregion End of Constructors
}

How can I pass data between windows in my C# WPF application?

I have a login form in the MainWindow of my WPF application. If the user logs in successfully, I want to open the HomeWindow. My problem is that I need to pass the adminID variable from the MainWindow to the HomeWindow. How can I do this?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
int errors = 0;
if (txtUsername.Text == "")
{
lblUsernameStatus.Content = "This field is required.";
errors = errors + 1;
}
if (txtPassword.Text == "")
{
lblPasswordStatus.Content = "This field is required.";
errors = errors + 1;
}
if (errors == 0)
{
Administrator TryLogin = new Administrator();
if (TryLogin.VerifyUser(txtUsername.Text, txtPassword.Text))
{
HomeWindow home = new HomeWindow();
int adminID = TryLogin.userID;
home.Show();
this.Close();
}
else
{
lblLoginStatus.Content = TryLogin.status;
}
}
}
PS: I haven't written anything in the HomeWindow.xaml.cs file.
Define an initializer in HomeWindow to accept the data you wish to send:
private int AdminID;
public HomeWindow()
{
InitializeComponent();
}
public HomeWindow(int adminID) : base()
{
AdminID = adminID;
}
Then you can just:
HomeWindow home = new HomeWindow(TryLogin.userID);
home.Show();
this.Close();
Declaring static variable would be the simplest and easiest way because once login is authorized, the value doesn't change until logged off(application is exited)
I also used a way of passing value through Window constructor but static variables are much easier to utilize many fixed data of the logged-in users like customized setting data for each users. I also have a WPF app and pass 11 values and utilize easiily everywhere inside application.
Declare as static variable in MainWindow like,
public static int adminID;
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
adminID= TryLogin.userID;
}
And usage in HomeWindow is like,
MainWindow.adminID
Hope this helps..
There are different ways to achieve this.
Here is an example using singleton
public class User
{
private static readonly User _instance;
private static object syncRoot = new Object();
public string Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string SomeOtherProperty { get; set; }
private User()
{
// Initialize defaults
}
public void Reset()
{
// Clear existing values
}
public static User Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (_instance == null)
_instance = new User();
return _instance;
}
}
}
}
}
On your login form:
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
int errors = 0;
if (txtUsername.Text == "")
{
lblUsernameStatus.Content = "This field is required.";
errors = errors + 1;
}
if (txtPassword.Text == "")
{
lblPasswordStatus.Content = "This field is required.";
errors = errors + 1;
}
if (errors == 0)
{
Administrator TryLogin = new Administrator();
if (TryLogin.VerifyUser(txtUsername.Text, txtPassword.Text))
{
User.Instance.Reset(); // Make sure old data is removed
User.Instance.Username = txtUsername.Text;
User.Instance.Password = txtPassword.Text;
HomeWindow home = new HomeWindow();
int adminID = TryLogin.userID;
home.Show();
this.Close();
}
else
{
lblLoginStatus.Content = TryLogin.status;
}
}
}
On your home form:
You can retrieve the User credentials using User.Instance just make sure you reset it on logoff.

C# XML. Get data from XML to object, update in UI, save back to XML

let me describe the situation. Winforms C#
I have xml file with data. I load this data to an user defined class object using Deserialize.
Based on this object with data, I build [in Form] UI: many tabPages of custom controls (textBox, 2 buttons in groupBox). I can also save this user defined class object using Serialize to XML file.
Question:
When I update textBox.Text in Form UI in custom control I do not know how to keep connection with the object with data (Layout layout) and save the updated object with data to XML. The change of text happens only in user custom control TextBox. I want to update data from UI in data object (layout) and then simply save with Serialization.
user class:
public class Layout
{
public string type;
public List<TabPage> TabPageList;
public Layout()
{
this.TabPageList = new List<TabPage>();
}
}
public class TabPage
{
public string text;
public List<ActionGroup> ActionGroupList;
public TabPage()
{
this.ActionGroupList = new List<ActionGroup>();
}
}
public class ActionGroup
{
public string type;
public string text;
public string sourceLocal;
public string sourceRemote;
public ActionGroup()
{
this.type = string.Empty;
this.text = string.Empty;
this.sourceLocal = string.Empty;
this.sourceRemote = string.Empty;
}
}
Custom control:
public partial class ViewActionGroup : UserControl
{
public string type;
public string text;
public string sourceLocal;
public string sourceRemote;
public bool isRemote;
public bool isDone;
public ViewActionGroup()
{
this.type = string.Empty;
this.text = string.Empty;
this.sourceLocal = string.Empty;
this.sourceRemote = string.Empty;
this.isRemote = false;
this.isDone = false;
InitializeComponent();
}
public ViewActionGroup(ActionGroup actionGroup)
{
this.type = actionGroup.type;
this.text = actionGroup.text;
this.sourceLocal = actionGroup.sourceLocal;
this.sourceRemote = actionGroup.sourceRemote;
this.isRemote = false;
this.isDone = false;
InitializeComponent();
groupBox1.Text = text;
button1.Text = type;
button1.Click += new EventHandler(Button_Click);
textBox1.Text = sourceLocal;
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
}
public void ChangeToRemote()
{
isRemote = true;
textBox1.Text = this.sourceRemote;
}
public void ChangeToLocal()
{
isRemote = false;
textBox1.Text = this.sourceLocal;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.isRemote)
{
this.sourceRemote = textBox1.Text;
}
else
{
this.sourceLocal = textBox1.Text;
}
}
Creating UI where I loose connection between UI and data object:
private void CreateLayout(Layout layout)
{
this.Text = layout.type;
TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
int tabCount = 0;
foreach (TabPage tabpage in layout.TabPageList)
{
int actionCount = 0;
tabControl.TabPages.Add(tabpage.text);
foreach (ActionGroup actionGroup in tabpage.ActionGroupList)
{
ViewActionGroup view = new ViewActionGroup(actionGroup);
view.Location = new Point(0, actionCount * view.Height);
tabControl.TabPages[tabCount].Controls.Add(view);
tabControl.TabPages[tabCount].AutoScroll = true;
tabControl.TabPages[tabCount].AutoScrollMinSize = new System.Drawing.Size(tabControl.Width/2,tabControl.Height);
actionCount++;
}
tabCount++;
this.panelMain.Controls.Add(tabControl);
}
}
There are two common ways:
One is a routine WriteDataIntoControls and another ReadDataFromControls where you transfer the data to and from your visible controls manually (advantage: highest degree of control). In this case you'd have to read your object from your XML source, deserialize it into your business object and create all visible controls together with their value. On saving you'd have to transfer all values into your business object and serizalize it after this.
The second is DataBinding (advantage: highest degree of automation). Read here: https://msdn.microsoft.com/en-us/library/ef2xyb33%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
You can bind to simple values as well as to lists (including navigation) or complex objects.
You find a tutorial here: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
#Shnugo Thank You for your feedback. The tutorial you posted did not help because it is too hard for me but Data Binding topic gave me some clue.
Here easy tutorial in VB actually but simple. It helped me to do it quickly in C#.
https://www.youtube.com/watch?v=jqLQ2K9YY2A
C# solution
class MyObject
{
string name;
public MyObject()
{ }
public string Name
{
get { return name;}
set { name = value; }
}
}
public partial class Form1 : Form
{
MyObject obj = new MyObject();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
obj.Name = "Lucas";
textBox1.DataBindings.Add("Text", obj, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = obj.Name;
}
}

using an array in two different forms C#

I have a problem with arrays which are made from class; actually I can make an array from a class and in the first form I set the data in may array, but when I switch to my second form and create an object from my class, I find my array empty so I can't use the information witch I had entered into my array.
public partial class Form1 : Form
{
string _name;
string _department;
int _id;
int _count;
int counter = 0;
Mediator m = new Mediator();
Employee ee = new Employee();
public Form1()
{
InitializeComponent();
}
private void Add_to_Array_Click(object sender, EventArgs e)
{
_name = txtName.Text;
_department = txtDepartment.Text;
_id = int.Parse(txtID.Text);
_count = counter;
m.Set_Value(_name, _department, _id,_count);
counter++;
Exchange();
//-----------------------------------
txtDepartment.Text = "";
txtID.Text = "";
txtName.Text = "";
}
private void Search_Click(object sender, EventArgs e)
{
listBox1.Items.Add(m.array[0].E_Name);
listBox1.Items.Add(m.array[0].E_Department);
listBox1.Items.Add(m.array[0].E_ID.ToString());
//---------------------------------------------------
listBox1.Items.Add(m.array[1].E_Name);
listBox1.Items.Add(m.array[1].E_Department);
listBox1.Items.Add(m.array[1].E_ID.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
public partial class Form2 : Form
{
Mediator M;
public Form2()
{
InitializeComponent();
}
private void Show2_Click(object sender, EventArgs e)
{
listBox1.Items.Add(M.array[0].E_Name);
listBox1.Items.Add(M.array[0].E_Department);
listBox1.Items.Add(M.array[0].E_ID.ToString());
//---------------------------------------------------
listBox1.Items.Add(M.array[1].E_Name);
listBox1.Items.Add(M.array[1].E_Department);
listBox1.Items.Add(M.array[1].E_ID.ToString());
}
class Employee
{
string Name;
string Department;
int ID;
//**************************
public string E_Name
{
get { return Name; }
set { Name = value; }
}
public string E_Department
{
get { return Department; }
set { Department = value; }
}
public int E_ID
{
get { return ID; }
set { ID = value; }
}
}
class Mediator
{
public Employee[] array = new Employee[5];
public void Set_Value(string name,string department,int id,int count)
{
array[count] = new Employee();
array[count].E_Name = name;
array[count].E_Department = department;
array[count].E_ID = id;
}
}
I would strongly suggest that you don't make Mediator m static.
IMHO this is a hack and you don't want to get in the habit of effectively making global variables everywhere, it will come back to bite you as you progress through your career.
The better solution is to pass Form2 only the data it needs to work on.
I'm guessing Form2 only needs to display the list of Employees, so you should create a Property in Form2 as follows:
public Employee[] Employees {get; set;}
[If you need more than just the list of employees then you should rather have a property for the Mediator - public Mediator Mediator {get; set;} ]
You then need to send this data to the second form just before you show it:
Form2 f2 = new Form2();
f2.Employees = m.array;
f2.Show();
Because the data is sent by reference any changes you make in form2 will reflect in the objects stored in form1.
Another option is to structure your program using the MVC pattern. It's a little bit more advanced, but it's a great pattern to keep your winForms apps neat and tidy. If you are building a wizard style app then I would strongly suggest this approach.
As an aside note you can also clean up the code quite a bit by using a list instead of an array to store the employee data - this will also make the code more adaptable to larger numbers of employees in the future.
You can also use C#s automatic properties to reduce the amount of code you need to write. So your code can look like this this instead:
public partial class Form1 : Form
{
private Mediator _mediator = new Mediator();
public Form1()
{
InitializeComponent();
}
private void Add_to_Array_Click(object sender, EventArgs e)
{
var newEmployee = new Employee
{
Name = txtName.Text,
Department = txtDepartment.Text,
ID = int.Parse(txtID.Text)
};
_mediator.Employees.Add(newEmployee);
Exchange();
//-----------------------------------
txtDepartment.Text = "";
txtID.Text = "";
txtName.Text = "";
}
private void Search_Click(object sender, EventArgs e)
{
foreach (var employee in _mediator.Employees)
{
listBox1.Items.Add(employee.Name);
listBox1.Items.Add(employee.Department);
listBox1.Items.Add(employee.ID.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Employees = _mediator.Employees;
f2.Show();
}
}
public partial class Form2 : Form
{
public List<Employee> Employees { get; set; }
public Form2()
{
InitializeComponent();
}
private void Show2_Click(object sender, EventArgs e)
{
foreach (var employee in Employees)
{
listBox1.Items.Add(employee.Name);
listBox1.Items.Add(employee.Department);
listBox1.Items.Add(employee.ID.ToString());
}
}
}
class Employee
{
public string Name { get; set; }
public string Department { get; set; }
public int ID { get; set; }
}
class Mediator
{
public List<Employee> Employees { get; private set; }
public Mediator()
{
Employees = new List<Employee>();
}
}
You need an instance of Form1 in Form2 to use the array you've created.
You have two options;
First one, you can store an instance of Form1 in a common class so you can reach it from Form2.
Second option; you need to pass reference of Form1 to your Form2 instance.
A quick fix for your problem would be to add the static keyword in your array definition. this will make it stateless and therefor there won't be a new (instance of the) array being created each time you access it.
You should also consider placing this array in some kind of a static class that will be in charge of handling this kind of global variables.
As for your edits, you have at least 2 options here: the first one is making the Mediator class and the array inside static; or create a property in your second form like public string [] SomeArray {get; set;} and pass the array to it after declaring the ... = new Form2(); and before calling the .Open()

Categories

Resources