Can someone tell me what i'm doing wrong here?
I've got a list in a database class that I want to view in a listbox on my form yet it's not showing anything.
I call the form from a button click of my first form which is where I enter the data and it works if I put a listbox on that form, but I'm wanting to open another form which will only show the data if that makes sense?
here's my code for the form that I want to view what's in the list:
public partial class Summary : Form
{
public Summary()
{
InitializeComponent();
}
private Database viewlist = new Database();
private void Summary_Load(object sender, EventArgs e)
{
}
private void sum()
{
List<String> listofPicks = viewlist.listPickups();
listBox1.Items.AddRange(listofPicks.ToArray());
}
private void button1_Click(object sender, EventArgs e)
{
sum();
}
}
Also may i make it clear that this code works if it's all done on the same form
Try this (kind of), load the list when the form loads, not on a button click:
public partial class Summary : Form
{
private Database _viewlist = new Database();
public Summary()
{
InitializeComponent();
}
private void Summary_Load(object sender, EventArgs e)
{
LoadList();
}
private void LoadList()
{
listBox1.Items.Clear();
listBox1.Items.AddRange(_viewlist.Pickups.ToArray());
}
}
public class Database
{
public List<string> Pickups
{
get { return new List<string> {"alfa", "beta"}; }
}
}
Related
I want some data to be displayed in a listbox when the form is loaded. But after it is loaded nothing changes.
I added DataSource and used foreach to get the items.
public partial class MainForm : Form
{
public CDiary diary;
public MainForm()
{
InitializeComponent();
diary = new CDiary("mongodb://localhost:27017");
}
private void MainForm_Load(object sender, EventArgs e)
{
listOfEvents.DataSource = diary.Events;
foreach(CEvent ev in diary.Events){
listOfEvents.Items.Add(ev.Date);
}
}
}
Thanks.
I've created a new form, in which I have a toolbox. When I press a button in that form, it should relay that information that has been entered by the user(toolboxbox value) to the main form, in which it should say that piece of information in a label.
Since the method to create that username from the toolbox is private, I cannot access it from any other way. Making it public does not seem to make a difference, neither does get,set (from the way I've been trying to atleast).
Picture that may help explaining it:
Code (in which to create user):
namespace WindowsFormsApplication3
{
public partial class Newuserform : Form
{
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
string uname = textboxUsername.ToString();
}
public void Unamecreate()
{
}
}
}
Form1 Code (To receive created user):
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Aboutform form2 = new Aboutform();
form2.Show();
}
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.Show();
}
}
}
you have a lot of options.
One way is to create an event and handle it in the main form.
public partial class Newuserform : Form
{
//the public property
public event EventHandler<string> UnameChanged;
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
if (UnameChanged != null)
UnameChanged(textboxUsername.ToString()); //fire the event
}
}
Now, to "handle" the event, do the following in your main form:
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.UnameChanged += Handler;
formnewuser.Show();
}
private void Handler (object sender, string Uname)
{
// do something wit the new Uname.
}
note: recreating the Newuserform will require to cleanup previous attached resources.
I'm trying to fill a List<String> from another form while both forms are open.
In the form WorkOrder i have a list: List<String> materialsList, then in my other form AddMaterials i have other List<String> list, this is the list that i want to pass to the WorkOrder form, but both forms are open and i don't know it is possible to do that.
Here are my forms:
If i click the Add Materials button in the WorkOrder form then the form AddMaterials opens. Now with the form AddMaterials open, i fill one by one the elements of the list with the button Add new Material, then when i click finish i want to pass the List<String> list to the WorkOrder list: List<String> materialsList.
This is the code that i'm trying to solve this:
In the WorkOrder form:
List<String> materialsList = new List<string>();
public void setList(List<String> l)
{
this.materialsList = l;
}
In the AddMaterials form:
public partial class AddMaterials : Form
{
List<String> list = new List<string>();
public AddMaterials()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Adding Materials
list.Add(material.Text);
}
private void button2_Click(object sender, EventArgs e)
{
//Passing the list with Method setList()
WorkOrder o = new WorkOrder();
o.setList(list);
}
}
Any question post on comments.
You could pass a reference to either the WorkOrder Form or the WorkOrder.materialsList in the constructor of your AddMaterials Form.
So your AddMaterials code could be
public partial class AddMaterials : Form
{
WorkOrder wo;
public AddMaterials(WorkOrder wo)
{
InitializeComponent();
this.wo = wo;
}
private void button1_Click(object sender, EventArgs e)
{
//Adding Materials
this.wo.materialsList.Add(material.Text);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
In WorkOrder you'd have this function which opens a new AddMaterials Form. You could place in logic here before you create a new AddMaterials Form to clear the list or do something else if you have multiple materials list for a WorkOrder.
private void button1_Click(object sender, EventArgs e)
{
// Add logic here to deal with multiple AddMaterial Forms
// on one work order
new AddMaterials(this).Show();
}
What you're doing is not working since you are using two completely different lists.
You can create a custom constructor for AddMaterials:
public AddMaterials(List<string> materials)
{
InitializeComponent();
this.materials = materials;
}
and declaring materials as a member of AssMaterials class:
private List<string> materials;
If you want to keep things even more separated you can pass a delegate:
public delegate void Del(string str);
class AddMaterials
{
private Del addMaterialDelegate;
public AddMaterials(Del AddMaterialDelegate) : Form
{
InitializeComponent();
this.addMaterialDelegate = AddMaterialDelegate;
}
private void button1_Click(object sender, EventArgs e)
{
//Adding Materials
addMaterialDelegate(material.Text);
}
}
and then from WorkOrder:
private void button1_Click(object sender, EventArgs e)
{
new AddMaterials(OnAddMaterialDelegate).Show();
}
private void OnAddMaterialDelegate(string material)
{
this.materialsList.Add(material);
}
This will allow you to change your mind in the future without too much rework (for example if you want to switch from a List to a different type).
trying to get data from the main form to form 2. The main form has a textbox
and a button. when the button is pressed it opens form 2 which will display the data entered in the main form as a series of text blocks.
However I cant get the data to transfer between the forms. the code is bellow.
can anyone help or suggest anything I can do differently?
WPF 1 main form:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenForm_Click(object sender, RoutedEventArgs e)
{
//btnset: Takes the values contained in the text boxes and updates
//the student class
//properties.
Student.sFname = firstname.Text;
Student.sSname = secondname.Text;
Window1 details = new Window1();
details.Show();
}
WPF 2 code:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void details_Load(object sender, EventArgs e)
{
Fname.Text = Student.sFname;
Sname.Text = Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
There are a number of ways to "pass data" between 2 classes. The easiest way is to expose property or method on Window1 and just set the text you need passed. Another way is to create a constructor on Window1 that takes in the data as parameters. Here is code that demonstrates these approaches.
public class Program
{
public static void Main(string[] args)
{
var c1 = new Class1();
c1.DoStuff();
}
}
public class Class1
{
public void DoStuff()
{
var c = new Class2("stuff");
var c2 = new Class2();
c2.AcceptStuff("stuff2");
c.Print();
c2.Print();
c2.MyData = "stuff3";
c2.Print();
}
}
public class Class2
{
private string _myData;
public Class2()
{
}
public Class2(string myData)
{
_myData = myData;
}
public string MyData
{
set { _myData = value;}
}
public void AcceptStuff(string myData)
{
_myData = myData;
}
public void Print()
{
Console.WriteLine(_myData);
}
}
Prints
stuff
stuff2
stuff3
I assume you have a class in MainWindow like:
`Public class Student
{
public static string sFname;
public static string sSname;
}`
When you click open button you are assigning values to those variable, but if you want to access them in another window mention the window name and then class name.
Check this code if its working?
`public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void details_Load(object sender, EventArgs e)
{
Fname.Text = MainWindow.Student.sFname;
Sname.Text = Mainwindow.Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}`
I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)