From the picture above you can see my program. The problem i have is that my combobox for Countries on the bottom does show up on my listview. I dont know why, has it to do with that Countries is not a string? and whats the solution. I have been trying for a very long time with this, i need all help there is for you guys to help. Thanks in advance
Form 1 = listview
Form 2 = my customer manager (the picture above)
down here is inside Form1
public partial class MainForm : Form
{
CustomerFiles.Contact contact = new CustomerFiles.Contact();
CustomerFiles.Address address = new CustomerFiles.Address();
CustomerFrame customerframe = new CustomerFrame();
private void MainForm_Load(object sender, EventArgs e)
{
if (customerframe.ShowDialog() == DialogResult.OK) //if button OK is clicked then value will be inserted
{
// var item = string.Format("[{0}]",contact.ToString());
listView1.Items.Add(string.Format("[{0}]", customerframe.ToString()));
}
}
Down here is inside Form2
public partial class CustomerFrame : Form
{
CustomerFiles.Address address = new CustomerFiles.Address();
CustomerFiles.Contact contact = new CustomerFiles.Contact();
CustomerFiles.Countries country = new CustomerFiles.Countries();
public string firstName { get; set; }
public string lastName { get; set; }
internal CustomerFiles.Phone phone { get; set; }
internal CustomerFiles.Email email { get; set; }
internal CustomerFiles.Address addressinfo { get; set; }
public string city { get; set; }
internal CustomerFiles.Countries countryinfo { get; set; }
public string street { get; set; }
public string zipcode { get; set; }
public CustomerFrame()
{
InitializeComponent();
List<CustomerFiles.Countries> countries = new List<CustomerFiles.Countries> {
new CustomerFiles.Countries{ CountryId = 1, Name = "Bulgaria"},
new CustomerFiles.Countries{ CountryId = 2, Name = "France"},
new CustomerFiles.Countries{ CountryId = 3, Name = "Brazil"},
new CustomerFiles.Countries{ CountryId = 4, Name = "Russia"},
new CustomerFiles.Countries{ CountryId = 5, Name = "South Africa"},
new CustomerFiles.Countries{ CountryId = 6, Name = "Kurdistan"},
new CustomerFiles.Countries{ CountryId = 7, Name = "China"},
new CustomerFiles.Countries{ CountryId = 8, Name = "Japan"},
new CustomerFiles.Countries{ CountryId = 9, Name = "United States of America"},
new CustomerFiles.Countries{ CountryId = 10, Name = "UK"},
new CustomerFiles.Countries{ CountryId = 11, Name = "Australia"},
new CustomerFiles.Countries{ CountryId = 12, Name = "Germany"},
new CustomerFiles.Countries{ CountryId = 13, Name = "Sweden"},};
cbCountry.DataSource = countries;
cbCountry.DisplayMember = "Name";
cbCountry.ValueMember = "CountryId";
cbCountry.SelectedValue = 1;
btnOk.DialogResult = DialogResult.OK;
contact.ToString();
address.ToString();
}
private void btnOk_Click(object sender, EventArgs e)
{
// inside contact
contact.FirstName = tbFirstName.Text;
firstName = contact.FirstName;
contact.LastName = tbLastName.Text;
lastName = contact.LastName;
contact.PhoneData = new CustomerFiles.Phone(tbCellPhone.Text);
phone = contact.PhoneData;
contact.PhoneData = new CustomerFiles.Phone(tbHomePhone.Text);
email = contact.EmailData;
//inside address class
address.City = tbCity.Text;
city = address.City;
address.Country = new CustomerFiles.Countries(cbCountry.Text);
countryinfo = address.Country;
address.Street = tbStreet.Text;
street = address.Street;
address.ZipCode = tbZipCode.Text;
zipcode = address.ZipCode;
}
public override string ToString()
{
return string.Format("[{0}, {1}]", contact.ToString(), address.ToString());
}
And down here is inside my address class
class Address
{
private string city;
public Countries country;
private string street;
private string strErrMessage;
private string zipCode;
public Address()
{
string strErrMessage = string.Empty;
string street = string.Empty;
string zipCode = string.Empty;
string city = string.Empty;
}
public Address(string street, string zipCode, string city)
{
Street = street;
ZipCode = zipCode;
City = city;
}
public Address(string street, string zipCode, string city, Countries country)
{
Street = street;
ZipCode = zipCode;
City = city;
Country = country;
strErrMessage = string.Empty;
}
public Countries Country
{
get
{
return country;
}
set
{
country = value;
}
}
public override string ToString()
{
return string.Format("[{0}, {1}, {2}, {3}]", city, zipCode, street, country);
}
}
It looks like you are adding the Text property from the form:
listView1.Items.Add(string.Format("[{0}]", customerframe.ToString()));
Change that to use the value you want to use. It's not very clear what you want to display in the ListView.
Maybe it's as simple as:
listView1.Items.Add(cbCountry.SelectedItem);
Check for null, etc.
Make sure your Country class overrides the ToString() function like you have in your other classes.
Related
I am attemping to read a text file in the format of
(The # at end is just the number of classes they're in, but I dont save the course name with the fac/students class)
Course Biology
Faculty Taylor Nate 0
Student Doe John 3
Student Sean Big 0
Course Art
Faculty Leasure Dan 1
The first input should be a course, followed by the faculty and students of the specific course. The Course class should contain a collection of faculty members and a collection of students.
I have been able to put each course/student/faculty into their respective class, but I am having trouble visualizing a way to add the students/faculty to the course.
My current idea putting the data into their respective classes would be to keep the current index of the course- therefore I have it saved as
courses[currentCourse++]
so when I parse the next line, (being a faculty/student) I already know what the course index should be.
using (StreamReader reader = new StreamReader(fileName))
{
while (!reader.EndOfStream)
{
lineCounter++;
line = reader.ReadLine();
string[] words = line.Split(' ');
Console.WriteLine(words[0]);
if (words[0] == "Course")
{
string nameOfCourse = words[1];
courses[currentCourse++] = new Course
{
Name = nameOfCourse
};
}
if (words[0] == "Faculty")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
faculty[currentFaculty++] = new Faculty
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
};
}
if (words[0] == "Student")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
students[currentStudent++] = new Student
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
};
}
I know the problem lies in the courses class itself- but i'm not sure the terminology to add a class to another class.
public class Course
{
public override string ToString()
{
return $"{Name}";
}
public string Name { get; set; }
}
public class Student
{
public override string ToString()
{
return $"{FirstName} {LastName} {NumOfClasses}";
}
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string NumOfClasses { get; set; } = string.Empty;
}
Thanks for reading!
You want to add a collection of Student and Faculty to the course class, correct? You can do so like this by simply adding a List<T> to your Course class and then initializing it in a constructor.
public class Course
{
public override string ToString()
{
return $"{Name}";
}
public string Name { get; set; }
public List<Student> Students { get; set; }
public List<Faculty> FacultyMems { get; set; }
public Course()
{
Students = new List<Student>();
FacultyMems = new List<Faculty>();
}
}
And in your using block, you can add each student/faculty to the course as so:
if (words[0] == "Course")
{
string nameOfCourse = words[1];
currentCourse++;
courses[currentCourse] = new Course
{
Name = nameOfCourse
};
}
if (words[0] == "Faculty")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
courses[currentCourse].FacultyMems.Add(new Faculty
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
});
}
if (words[0] == "Student")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
courses[currentCourse].Students.Add(new Student
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
});
}
With this, each time you encounter "Course" your course list will add a new item and then you can append students/faculty/etc when those values occur.
This can be simplified even further but the concept is there for you to follow. Hope this helps.
If I'm understanding you correctly, you want your courses to have a list of faculty and students?
public class Course
{
public override string ToString()
{
return $"{Name}";
}
public string Name { get; set; }
public List<Student> Students { get; set; }
public List<Faculty> FacultyMembers {get; set;}
}
Just be sure to initialize the Lists before trying to add things to them otherwise you'll get a null ref exception.
I have retrieve a list of employees. my employee class columns(employeeId, lastname, genderid)
List<m_employees> Items = new List<m_employees>
{
new m_employees{ employeeid = 1, lastname = "mike", genderid = 1 },
new m_employees{ employeeid = 2, lastname = "jeni", genderid = 2 }
};
then i have my gender class columns (id, title)
List<m_genders> genders = new List<m_genders>
{
new m_genders{ id = 1, title = "Male" },
new m_genders{ id = 2, title = "Female" }
};
//then i tried joining the retrieved list of employees to the genders
var x = from emp in Items
join sex in genders
on emp.genderid equals sex.id
into a from b in a.DefaultIfEmpty(new m_genders())
select new
{
emp.lastname,
emp.genderid,
sex = b.title
};
red error line is showing to the keyword join and says "the type of one of the expressions in the join clause is incorrect..."
how can i join them properly?
This happens because types emp.genderid, sex.id are different and you need to cast or convert them explicitly like that:
(int)emp.genderid equals sex.id
I was able to reproduce the error with the following class definition:
class m_genders
{
public int id { get; set; }
public string title { get; set; }
}
class employee
{
public int id;
public uint genderid;
public string lastname { get; set; }
}
Your question is not clear, the code work without any problem :
namespace WindowsFormsApp1
{
public class m_genders
{
public int id;
public string title;
}
public class m_employees
{
public int employeeid { get; set; }
public int genderid { get; set; }
public string lastname { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<m_genders> genders = new List<m_genders>
{
new m_genders {id = 1, title = "Male"},
new m_genders {id = 2, title = "Female"}
};
List<m_employees> Items = new List<m_employees>
{
new m_employees{ employeeid = 1, lastname = "mike", genderid = 1 },
new m_employees{ employeeid = 2, lastname = "jeni", genderid = 2 }
};
var x = from emp in Items
join sex in genders
on emp.genderid equals sex.id
into a
from b in a.DefaultIfEmpty(new m_genders())
select new
{
emp.lastname,
emp.genderid,
sex = b.title
};
}
}
}
How can I send a data which is saved in a class:
public class Person
{
public int Id { get; set; }
public string Fullname { get; set; }
public string Details { get; set; }
public string UserType { get; set; }
}
to the variable?
I'm saving data (in another class) like this:
var persons = new List<Person>();
while (dr1.Read())
{
// get the results of each column
int id = (int)dr1["ID_Instructor"];
string firstname = (string)dr1["f_name"];
string lastname = (string)dr1["l_name"];
string school = (string)dr1["d_school"];
string category = (string)dr1["category"];
var person = new Person
{
Id = id,
Fullname = firstname + " " + lastname,
Details = school + " " + category
};
persons.Add(person);
}
and in the same class I want to send it to the variable from menuItem by the BindingContext. I just need to print all of this data (from one person from list)
private void MenuItem_Clicked_1(object sender, EventArgs e)
{
var menuItem = sender as MenuItem;
if (menuItem != null)
var name = menuItem.BindingContex;
I want to order by pincode with string empty, when I am trying to convert pincode to an integer for sorting, I am getting an error.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Pincode { get; set; }
}
List<Student> objStudentList = new List<Student>();
objStudentList.Add(new Student() { Id = 1, Name = "gopi", City="Chennai", Pincode = "600002" });
objStudentList.Add(new Student() { Id = 2, Name = "ravi", City = "Bangulor", Pincode = "600 001" });
objStudentList.Add(new Student() { Id = 3, Name = "mani", City = "Madurai", Pincode = "600 007" });
objStudentList.Add(new Student() { Id = 4, Name = "anbu", City = "Thiruchi", Pincode = "600 005" });
objStudentList.Add(new Student() { Id = 4, Name = "kumar", City = "Thiruchi", Pincode = "" });
objStudentList = objStudentList.OrderBy(a => int.Parse(Regex.Replace(a.Pincode.Replace(" ", "").Replace("\t", ""), #"\t|\n|\r", ""))).ToList();
can anyone tell me what the problem at hand is and how to fix it?
you can avoid empty strings by first filtering them out. i.e.
objStudentList = objStudentList.Where(a => !string.IsNullOrEmpty(a.Pincode))
.OrderBy(a => int.Parse(a.Pincode)).ToList();
However, if you want to keep the Student objects that have an empty string but rather replace their Pincode property with "0" you can try this:
objStudentList = objStudentList.OrderBy(a => string.IsNullOrEmpty(a.Pincode) ? int.Parse("0") : int.Parse(a.Pincode))
.ToList();
The reason is that you're trying to covert an empty string to int. If you want to get 0 as a result, you must replace "" to "0"
I'm creating an WindowsForms application that is using a list of persons with 4 parameters (ID, Name, Surname, Permissions):
public List<Osoba> ListaOsoba()
{
Osoba nr1 = new Osoba(1, "Name", "Surname", Permissions.Administrator);
Osoba nr2 = new Osoba(2, "Name2", "Surname2", Permissions.Użytkownik);
Osoba nr3 = new Osoba(3, "Name3", "Surname3", Permissions.Użytkownik);
listaOsób.Add(nr1);
listaOsób.Add(nr2);
listaOsób.Add(nr3);
return listaOsób;
}
I would like to post all those Parameters to CheckedListBox, but show only name and surname to the user. The ID and Permissions should be hidden, but they need to exist, because I want to use them later.
Every help will be appreciated.
public static bool CheckBoxListPopulate(CheckBoxList CbList, IList<T> liSource, string TextFiled, string ValueField)
{
try
{
CbList.Items.Clear();
if (liSource.Count > 0)
{
CbList.DataSource = liSource;
CbList.DataTextField = TextFiled;
CbList.DataValueField = ValueField;
CbList.DataBind();
return true;
}
else { return false; }
}
catch (Exception ex)
{ throw ex; }
finally
{
}
}
here Cb list is the control name and
List item Ilist is the list source name
Text field (should be concatination ) ="Name" + "Surname"
Value field will be Hidden it can be "1,2,3"
so only Text field will be visible to user
To bind only name and surname to checkedboxlist first store name and surname together and then try this:
NameS = "Name" + "Surname";
((ListBox)checkedListBox).DataSource = listaOsób;
((ListBox)checkedListBox).DisplayMember = "NameS";
try this, here you have to make arbitrary compound properties for display and value member like DisplayName and HiddenId and then you can easily bound with checkedlistbox.
public class Osoba
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Permissions Permission { get; set; }
public string DisplayName { get; set; }
public string HiddenId { get; set; }
public Osoba()
{ }
public Osoba(int id, string fname, string lname, Permissions p)
{
Id = id;
FirstName = fname;
LastName = lname;
Permission = p;
DisplayName = FirstName + " " + LastName;
HiddenId = Id + "_" + Permission.GetHashCode();
}
public void ListaOsoba()
{
List<Osoba> objList = new List<Osoba>();
Osoba nr1 = new Osoba(1, "Name", "Surname", Permissions.Administrator);
Osoba nr2 = new Osoba(2, "Name2", "Surname2", Permissions.Uzytkownik);
Osoba nr3 = new Osoba(3, "Name3", "Surname3", Permissions.Uzytkownik);
objList.Add(nr1);
objList.Add(nr2);
objList.Add(nr3);
((ListBox)checkedListBox1).DataSource = objList;
((ListBox)checkedListBox1).DisplayMember = "DisplayName";
((ListBox)checkedListBox1).ValueMember = "HiddenId";
MessageBox.Show(((ListBox)checkedListBox1).Text);
MessageBox.Show(((ListBox)checkedListBox1).SelectedValue.ToString());
}
}
public enum Permissions
{
Administrator,
Uzytkownik
}
I had a similar thing with SQL. I returned many columns, but only wanted one to show.
Anyway
ArrayList arr = new ArrayList();
foreach (object o in ListaOsoba)
{
arr.Items.Add(o[1].ToString()+" "+o[2].ToString());
}
foreach (var item in arr)
{
chkNames.Items.Add(arr.ToString()); //chkNames is your CheckListBox
}
Then later when querying which ID and such goes where, loop through you original list, and see who was ticked based on the name and surname combo, find the ID related to that person and you should be sorted