Click item in listbox and View details in multiline textbox - c#

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.

Related

Is there a way to change the color of a specific word/string at run time of a WPF ListBox Item?

So all i want is if a specific word will be added in a listbox row/item, at run time, cause i am using a timer to add items at run time from my Database, i want that specific word/string to have a diferent color.
ie: All new item added that contains the string/word "Aproved", should be colored as green as soon as a new item its added to the WPF ListBox at run time.
private void dispatcherTimerMensagem_Tick(object sender, EventArgs e)
{
if (!(principalDB.testarConexãoDB()))
{
dispatcherTimerVendasFechadas.Stop();
dispatcherTimerMensagem.Stop();
LstMensagem.ItemsSource = null;
LbPbVendasFechadas.ItemsSource = null;
}
else
{
mensagem = principalDB.selectMessagemUsuario(null);
if (mensagem != string.Empty)
{
this.Activate();
LstMensagem.Opacity = 1;
LstMensagem.Items.Add(principalDB.mensagemRemetente + " (" + principalDB.mensagemData + ")" + ": " + mensagem);
voice.Voice = voice.GetVoices().Item(0);
myWorkerMensagem.WorkerReportsProgress = true;
myWorkerMensagem.WorkerSupportsCancellation = true;
myWorkerMensagem.RunWorkerAsync();
if (VisualTreeHelper.GetChildrenCount(LstMensagem) > 0)
{
Border border = (Border)VisualTreeHelper.GetChild(LstMensagem, 0);
ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
scrollViewer.ScrollToBottom();
}
}
else
{
LstMensagem.Opacity = 0.5;
}
}
}
So the LstMensagem will recieve a new item at run time, from the variables declared, in this line of code:
LstMensagem.Items.Add(principalDB.mensagemRemetente + " (" + principalDB.mensagemData + ")" + ": " + mensagem);
If a specific word/string comes up, as ie "aproved" i want that string with a different text color,as example, brushed in green.
Use a TextBlock instead string. For entire item:
var text = principalDB.mensagemRemetente + " (" + principalDB.mensagemData + ")" + ": " + mensagem;
var tb = new TextBlock();
tb.Text = text;
if(text.Contains("aproved"))
tb.Foreground = Brushes.Green;
LstMensagem.Items.Add(tb);
For only part of the item, use the Inlines property to add different formatted texts:
var tb = new TextBlock();
tb.Inlines.Add(new Run { Foreground = Brushes.Green, Text =
principalDB.mensagemRemetente});
tb.Inlines.Add(" (" + principalDB.mensagemData + ")" + ": " + mensagem);
LstMensagem.Items.Add(tb);

C# How to check a Listbox for a string + object?

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

how to put and use two flags(insert and update flags) on a single button to perform two operations in asp.net with C#

how to put two flags(insert and update flags) on a single button to perform two operations in asp.net with C#.
Please help me out
if (strflag.ToUpper() == "Insert")
{
du.ExecQry("insert into Crop_Master(Crop_Name,Created_On,Created_By,Created_By_Ip)values('" + TextBox1.Text + "',GETDATE(),'" + ViewState["user"] + "','" + ViewState["ip"] + "')");
Label2.Text = "Submitted";
Label2.ForeColor = System.Drawing.Color.Green;
}
else if(strflag.ToUpper()=="Update")
{
du.ExecQry("Update Crop_Master set Crop_Name= '"+TextBox1.Text+"' where Crop_Name='#Crop_Name' ");
}
var record = du.SelectQuery("select * from where Crop_Master Crop_Name='#Crop_Name");
if(record.Count > 0) {
du.ExecQry("Update Crop_Master set Crop_Name= '"+TextBox1.Text+"' where Crop_Name='#Crop_Name' ");
} else {
du.ExecQry("insert into Crop_Master(Crop_Name,Created_On,Created_By,Created_By_Ip)values('" + TextBox1.Text + "',GETDATE(),'" + ViewState["user"] + "','" + ViewState["ip"] + "')");
}
Label2.Text = "Submitted";
Label2.ForeColor = System.Drawing.Color.Green;
I Just help you out with task, but don't like about your coding and db design style...
be more professional
You can add new Class library project to your solution and
Extend Button
Add below reference
using System.Web.UI.WebControls
Create an enum
public enum StateOfInsertion
{
Update,Insert
}
public MyButton : Button
{
public StateOfInsertion State { get; set; }
}
And use this Control like other and drag it to design mode from Toolbox

What is my Combobox_SelectedIndexChanged mistake?

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

Implement a HierarchicalDataBoundControl for ASP.NET

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;
}

Categories

Resources