I want to set the values of my selected item in my combo box to a variable(person) so that I can compare a person value to a value in my sqlite database. Here is my code:
private void cboSelectClient_SelectedIndexChanged(object sender, EventArgs e)
{
string str;
personSearch = new Person();
selectedPersonList = new List<Person>();
My problem is in the next line, where it should set my person variable items equal too the items that is in the combobox.
personSearch = cboSelectClient.SelectedItem as Person;
_pBl.PopulateSelectedPersonList(personSearch, ref selectedPersonList);
foreach (Person person in selectedPersonList)
{
txtAge.Text = Convert.ToString(person.Age);
txtEmail.Text = person.Email;
txtFirstName.Text = person.FirstName;
txtID.Text = Convert.ToString(person.ID);
txtLastName.Text = person.LastName;
lstItemsAdded.Items.Add(person.Item);
}
}
This is how i populate my combobox:
foreach (Person PersonItem in _listAllData)
{
if (PersonItem.FirstName != equalPerson.FirstName) //Add name en add nie die name wat klaar in is nie... VIND UIT HOE OM NET N SEKERE DEEL TE ADD..
{
cboSelectClient.Items.Add(PersonItem.ID +" : "
+ PersonItem.FirstName + " "
+ PersonItem.LastName + " "
+ PersonItem.Age + " "
+ PersonItem.Email + " "
+ PersonItem.Item.ItemCode
+ " " + PersonItem.Item.ItemDescription
+ " " + PersonItem.Item.ItemName + " "
+ PersonItem.Item.ItemPrice);
}
equalPerson = PersonItem;
}
Problem here is that you are populating your ComboBox with string, not with actual Person object. Of course this string cannot be converted directly back to object, therefore your conversion fails and personSearch is assigned to null.
To resolve the issue you need to:
Populate ComboBox with actual Person objects. Make sure ComboBox.ValueMemeber is null.
In Person class define some property like Text and set DisplayMember to it or override ToString to control how entries in CombobBox appear on the UI
Related
I'm trying to search for a certain number(object) in a listbox which comes together with a string in order to highlight it. In the following bit of code i override a ToString() method to contain all my objects.
public override string ToString()
{
string reservatiestring;
reservatiestring = "Kamer: " + roomNumber + "" + " Op datum: " + datum + " Aantal personen: " + personen.Count + " Naam: " + reservatienaam;
return reservatiestring;
}
Following this I add it to my listbox in the following bit of code:
listBox1.Items.Add(reservatie.ToString());
I now want to search for all the items in my listbox containing the same roomNumber object. To do this i tried the Contains() method with the text before it: "Kamer: " and the object which I'm looking for +comboBox1.SelectedItem. This however always fails and my code goes to the else option giving me the error message.
private void buttonSearch_Click(object sender, EventArgs e)
{
listBox1.SelectionMode = SelectionMode.MultiExtended;
Reservations reservaties = new Reservations();
reservaties.roomnumberstring = "Kamer: " + comboBox1.SelectedValue;
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (listBox1.Items[i].ToString().ToLower().Contains(("Kamer: " + comboBox1.SelectedValue)))
{
listBox1.SetSelected(i, true);
}
else
{
MessageBox.Show("error");
}
Please note: All my roomNumber objects are stored in the combobox, so whenever i select for example roomNumber 3 in my combobox and hit search all the items in the listbox containing "Kamer: 3" should be selected.
The roomnumberstring is a option I tried which did not work unfortunately.
reservaties.roomnumberstring = "Kamer: " + comboBox1.SelectedValue;
Your override of the ToString method is wrong and won't modify anything. Try this :
public override string ToString(this string reservatiestring)
{
reservatiestring = "Kamer: " + roomNumber + "" + " Op datum: " + datum + " Aantal personen: " + personen.Count + " Naam: " + reservatienaam;
return reservatiestring;
}
I can see one thing that might make your code fail. you are comparing
.ToLower()
with "Kamer", where the "K" isnĀ“t in lowercase
I trying to display a List into Console
My List code:
var order = new List<Orders>();
order.Add(new Orders { Date = "" + orders[0].date_created, Name = ""+ orders[0].billing.first_name , Adress = ""+ orders[0].shipping.address_1 + " " + orders[0].shipping.address_2 });
order.Add(new Orders { Date = "" + orders[1].date_created, Name = "" + orders[1].billing.first_name, Adress = "" + orders[1].shipping.address_1 + " " + orders[1].shipping.address_2 });
order.Add(new Orders { Date = "" + orders[2].date_created, Name = "" + orders[2].billing.first_name, Adress = "" + orders[2].shipping.address_1 + " " + orders[2].shipping.address_2 });
order.Add(new Orders { Date = "" + orders[3].date_created, Name = "" + orders[3].billing.first_name, Adress = "" + orders[3].shipping.address_1 + " " + orders[3].shipping.address_2 });
order.Add(new Orders { Date = "" + orders[4].date_created, Name = "" + orders[4].billing.first_name, Adress = "" + orders[4].shipping.address_1 + " " + orders[4].shipping.address_2 });
return order;
I have tried to display it like this:
Debug.WriteLine(order.ToString());
and like this:
order.ForEach(i => Debug.WriteLine(i.ToString()));
But gives the warning:
Unreachable code
How I can display the list?
Using Linq, as in your second try is close to the actual printing, you just need to format the string properly instead of simply call ToString method:
order.ForEach(o => Debug.WriteLine("Date: " + o.Date + " Adress: " + o.Adress + "Name: " + o.Name));
And I know it is not the point of the question, but I suggest you to use a ForEach instruction to populate the list too, as it will add more flexibility to your code.
Try this one:
foreach (var item in order)
{
Debug.WriteLine(item.ToString());
}
Or if you have mulitple properties as mentioned above, you can try like this:
foreach (var item in order)
{
Debug.WriteLine("Date : {0}, Name : {1}, Adress : {2}",item.Date.ToString(), item.Name.ToString(), item.Adress.ToString());
}
hi I want to enter three values into hash table so I entered one value as key and other two converted them into object and passed as value`
class UsingHashTable
{
public int empId;
public String empName { get; private set; }
public decimal salary { get; private set; }
public UsingHashTable(string empName, decimal salary)
{
this.empName = empName;
this.salary = salary;
}
Hashtable employeeHash = new Hashtable();
UsingHashTable emp;
Console.WriteLine("Enter Employee ID ");
empId = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Employee Name ");
empName = Console.ReadLine();
Console.WriteLine("Enter Salary of the employee ");
salary = Convert.ToDecimal(Console.ReadLine());
emp = new UsingHashTable(empName,salary);
employeeHash.Add(empId, emp);
}
Now the actual problem comes! How to retrieve the values from the hashtable where key is coming but from value I need to retrieve two values how??
foreach(DictionaryEntry temp in employeeHash)
{
Console.WriteLine(temp.Key + " " + temp.Value.eName + " " + temp.Value.salary);
}
I am exactly getting the error at temp.Value.eName and temp.Value.salary
You need to cast temp.Value to your type
Console.WriteLine(temp.Key + " " + ((UsingHashTable)temp.Value).eName + " " + ((UsingHashTable)temp.Value).salary);
But I would definitely recommend using Dictionnary<TKey,TValue> which will avoid the need to cast.
var employeeHash = new Dictionary<int,UsingHashTable>
var emp = new UsingHashTable(empName,salary);
employeeHash.Add(empId, emp);
And your loop will work just fine
foreach(var temp in employeeHash)
{
Console.WriteLine(temp.Key + " " + temp.Value.eName + " " + temp.Value.salary);
}
Hashtable.Key and Hashtable.Key are of object types. You need to cast these to your desired objects (UsingHashTable in your case) and then retrieve its properties (empName, empId)
foreach (DictionaryEntry temp in employeeHash)
{
int id = (int) temp.Key;
UsingHashTable employee = (UsingHashTable) temp.Value;
Console.WriteLine(id + " " + employee.eName + " " + employee.salary);
}
Its better to use Dictionarty() here,
It is because late binding not supported in C# by default. it means C# Identifies the temp.Value as Object not UsingHashTable. and object class does not have Name property.
to resolve this simply cast temp.Value to UsingHashTable.
example:
var temp2=(UsingHashTable)temp.Value;
Console.WriteLine(temp2.Name);
I would like to click on an item in a listbox and display the attributes that were passed into that listbox to a multiline textbox.
Below is the code I have written on form initialisation
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
foreach (KeyValuePair<int, Individual> kvp in ReadFromFile.individuals)
{
listBox2.Items.Add("ID = " + kvp.Value.id + " Name = " + kvp.Value.name.givenName + " " + kvp.Value.name.surname + " DoB = " + kvp.Value.birth.date);
}
int testIndividual = 94;
string genderOut = "";
if (ReadFromFile.individuals[testIndividual].gender == "M")
{
genderOut = "MALE";
}
else if (ReadFromFile.individuals[testIndividual].gender == "F")
{
genderOut = "FEMALE";
}
try
{
textBox1.AppendText(
"Name = " + ReadFromFile.individuals[testIndividual].name.givenName + " "
+ ReadFromFile.individuals[testIndividual].name.surname
+ Environment.NewLine + "Gender = " + genderOut
+ Environment.NewLine + "Birth date = " + ReadFromFile.individuals[testIndividual].birth.date
+ Environment.NewLine + "Birth place = " + ReadFromFile.individuals[testIndividual].birth.place
+ Environment.NewLine + "Death date = " + ReadFromFile.individuals[testIndividual].death.date
+ Environment.NewLine + "Death place = " + ReadFromFile.individuals[testIndividual].death.place);
}
catch
{
MessageBox.Show("This individual doesnt exist");
}
}
}
I would like to add more so I can click on a listbox item and the details for that item will be shown in the textbox
I get the feeling I may have to override the ToString() method or regex it. Im still quite a novice programmer so go easy on me :) THANK YOU
You need to handle the SelectedIndexChanged event for your listbox.
One way to do this is to bring up Form1.cs[Design] and select the listbox. In the property grid (Alt+Enter) click the icon that looks like this:
Find the event SelectedIndexChanged and double click it. That will hook up an event handler for you in the auto generated Form1.cs.designer file.
Next, replace the code for your Form1 class with the following:
public partial class Form1 : Form
{
private Dictionary<int, Individual> _individuals;
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
_individuals = ReadFromFile.individuals;
listBox1.DataSource = _individuals.Select(individual => individual.Value).ToList();
listBox1.DisplayMember = "name";
listBox1.ValueMember = "id";
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
var individual = listBox1.SelectedItem as Individual;
string genderOut = (individual.Gender == "M") ? "MALE" : "FEMALE";
var displayText
= String.Format("Name = {0} {1}\r\n" +
"Gender = {2}\r\n" +
"Birth date = {3}\r\n" +
"Birth place = {4}\r\n" +
"Death date = {5}\r\n" +
"Death place = {6}"
, individual.name.givenName
, individual.name.surname
, genderOut
, individual.birth.date
, individual.birth.place
, individual.death.date
, individual.death.place);
textBox1.AppendText(displayText);
}
}
A few notes about some of the things i've changed.
I've moved the code that was setting the textbox value into the SelectedIndexChanged event handler
I've refactored that code so that it's more readable by using the static String.Format method (all those Environment.NewLine repeats you had were messy).
I've setup the data for the list box using the DataSource property instead of your foreach loop.
Also, one thing you'll notice with this is that the list items in the listbox will not show the correct text. This is because you appear to be using some custom classes or structs for the name, birth and death of an Individual? To fix this, you need to add a new property to the Individual class like this:
public class Individual
{
// ... your code
public string DisplayName
{
get { return String.Format("{0} {1}), name.givenName, name.surname; }
}
// ... the rest of your code
}
Then you will need to change the line in my code above that looks like this:
listBox1.DisplayMember = "name";
to this:
listBox1.DisplayMember = "DisplayName";
Final note: You should probably be using "Upper Camel Case" for your property names. That means that they start with an upper case letter and then the first letter of each word is also upper case. For example, name.givenName should be Name.GivenName. This is a widely used convention.
I want to implement a Hierarchical data bound control for ASP.NET.
I used Implementing IHierarchy Support Into Your Custom Collection as a basis to create my hierarchical collection. I also created a HierarchicalDataSourceControl for the collection. Everything works: I can bind it to an ASP.NET TreeView and it renders the data correctly.
However, I'm stuck on the HierarchicalDataBoundControl. I found examples, but I am still too new at c# / .Net to understand them. I don't quite understand how to implement the examples:
Rendering a databound UL menu
nor
HierarchicalDataBoundControl Class
Does anyone have any tips, or better examples of implementing a control like this?
As another DataBound controls you have to override PerformDataBinding method as follows.
protected override void PerformDataBinding()
{
base.PerformDataBinding();
if (!IsBoundUsingDataSourceID && this.DataSource == null) return;
HierarchicalDataSourceView view = this.GetData(string.Empty);
if (view != null)
{
IHierarchicalEnumerable root = view.Select();
}
}
Whereas your control supports DataSource and HierarchialDataSource control in this case, should check if there is an assigned DataSource, get its DataSourceView to accomplish data binding. It's practicable through GetData method. Then call view's Select method to get the root collection.
Afterwards, you have to iterate through root collection that is an object of type IHierarchialEnumerable. It has a simple method called GetHierarchyData that gives an object which is an item of the collection and returns an associated IHierarchyData that describes a node. This methods is there, therefore in most times you don't know the actual type of item. IHierarchyData represents some information about a node and its children.
Lastly, you don't need to write a new DataSource control. If you are after creating a custom navigation control, it's better to write a new provider for SiteMapDataSource.
I faced similar problem and created my own raw html.
private void LoadCategory()
{
String s = "";
// Prepare your dataset DataSet ds
// Note That I have Category Table having C_NAME AND PC_NAME coloums, change it as per your column anme
ds.Relations.Add("rsParentChild", ds.Tables[0].Columns["C_NAME"], ds.Tables[0].Columns["PC_NAME"]);
s = s + "\n <table>";
s = s + "\n <tr>";
s = s + "\n <td class='br4'> ";
s = s + "\n <table>";
s = s + "\n <tr>";
s = s + "\n <td>";
s = s + "\n <ul id='qm0' class='qmmc'>";
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["PC_NAME"] == DBNull.Value)
{
s = s + "\n <li><a class='qmparent' href='#'>" + dr["C_NAME"].ToString() + "</a>";
s = s + "\n <ul>" + PopulateTree(dr) + "</ul>";
}
}
s = s + "\n <li class='qmclear'> </li></ul>";
s = s + "\n <script type='text/javascript'>qm_create(0,true,0,500,'all',false,false,false,false);</script>";
s = s + "\n </td>";
s = s + "\n </tr>";
s = s + "\n </table>";
s = s + "\n </td>";
s = s + "\n </tr>";
s = s + "\n </table>";
Literal1.Text = s;
}
private String PopulateTree(DataRow dr)
{
String s = "";
String ss;
foreach (DataRow row in dr.GetChildRows("rsParentChild"))
{
s = s + " <li>" + row["C_NAME"].ToString() + "\n";
ss = PopulateTree(row);
if (ss != "")
s = s + " <ul>" + ss + "</ul></li>\n\n";
}
return s;
}