I am currently working at a project where I need to manage cities and their zip codes. Therefore, I created the fallowing object:
class Place
{
Guid Id { get; set; }
string Name { get; set; }
int ZipCode { get; set; }
... further fields
}
and filled a List<Place> with a number of Place. Now I have a Textbox where I can enter the name of the city or its zip code. I both cases I would like to get a suggestion list which match with the entered input like this
Example: input = "1234" or "City"
12341 CityOne
12342 CityTwo
12343 CityThree
...
If I than choose an item from this suggestion list I would like to get the related Place as a return value. How can I implement this feature in C# with a WindowsFormsApplication?
What you've asked isn't a simple or single question. I think you can have a hidden ListBox below the TextBox where the user will enter text.
Now TextBox has an event called OnTextChanged. This event is fired every time there is a change in the text. This means that this event is fired overtime a user adds or deletes a character in the corresponding TextBox.
You can use this event to filter the list to display in your ListBox.
Something like this
//Imagine the TextBox ID as CityZipTextBox and ListBox ID as CityZipSuggestionsListBox
protected override void OnTextChanged(EventArgs e)
{
string txtCityZip = CityZipTextBox.Text;
List<Place> suggestedPlaces = filterCode;//Code to filter the full list using the TextBox content
CityZipSuggestionsListBox.DataSource = suggestedPlaces;
}
Does this help?
Related
I have a combobox and the items of the group membership look like
200;Heater
300;Cooler
400;Fan
If I select a part, only the number of the membership e.g. 200 is stored in the database.
Now I want to do a pre selection of the combobox.
To check if the combobox contains a membership number is working.
void MembershipPreSelection(ComboBox cb, string text)
{
if (cb.Items.Cast<Part>().Any(c => c.Membership.Contains(text)))
{
// now I want the string for the item that contains the text.
}
}
But to get the text of the combobox item (e.g. 200;Heater)??
This is what I tried, but I do not get the item text.
string m = cb.Items.Cast<Part>().Where(c => c.Hauptbaugruppe.Contains(text)).ToString();
The biggest issue seems to be using Where in your Linq statement which will return a collection of Part. Then, when the ToString() is called on the result, it's probably is something cryptic like:
System.Linq.Enumerable+WhereEnumerableIterator`1[your_namespace.Part]
To fix, consider something like First or Single in place of Where to return a single item.
string m =
cb.Items.Cast<Part>().Single(c => c.Hauptbaugruppe.Contains(text)).ToString();
You've probably already done this, but make sure that you have an override to Part.ToString() that returns the format that you want.
// Example Part class
class Part
{
public int ID { get; set; } = 0;
public string Description { get; set; } = "New Item";
// What do you want to see for `Part.ToString()`
public override string ToString()
{
return $"ID: {ID} Description: {Description}";
}
}
I'm creating an app with two forms, one is for understanding which file I am choosing, and the second one is just to make a filter before, which means you can choose which properties you want to see in this file. I have a checkboxlist and class with my properties.
I also have a button in my first form where I have:
foreach (var item in ds)
{
DataGridViewRow row = new DataGridViewRow();
fileListDataGridView.Rows.Add(
item.Path,
item.PatientName,
item.PatientID);
}
I'm not sure that this is the correct way how to add data from the list in DataGridWiev, but right now I have this solution. Cause it's just adding a new item at the end of the list.
The problem is that I have checkedListBox in the second form and I need to bind it somehow with my properties.
Properties:
public string Path { get; set; }
public string PatientName { get; set; }
public string PatientID { get; set; }
When you click on checkbox with Patient Name that means you will get the information in your first form with only this property. I know that when we are making a checkedListBox, we also have an index there, but how I can get this index and bind it with my prop?
People who are new to programming a DataGridView tend to fiddle with the rows and cells of the DataGridView. This is cumbersome, hard to understand, hard to reuse and very difficult to unit test.
Using databinding is way more easy.
Apparently you want to show several properties of a Patient in your DataGridView.
class Patient
{
public int Id {get; set;}
public string Name {get; set;}
public string Path {get; set;}
... // other properties?
}
Using visual studio you have added a DataGridView and the columns that you want to show. In the constructor of your form:
public MyForm()
{
InitializeComponent();
// assign Patient properties to the columns:
this.columnId.DataPropertyName = nameof(Patient.Id);
this.columnName.DataPropertyName = nameof(Patient.Name);
this.columnPath.DataPropertyName = nameof(Patient.Path);
... // etc.
}
Somewhere in your form you have a method to fetch the Patients that must be displayed:
IEnumerable<Patient> FetchPatientsToDisplay()
{
... // TODO: implement; out-of-scope of this question
}
To display Patients we use a BindingList:
BindingList<Patient> DisplayedPatients
{
get => (BindingList<Patient>)this.dataGridView1.DataSource;
set => this.dataGridView1.DataSource = value;
}
Now to fill the DataGridView when the form is loaded:
void OnFormLoading(object sender, ...)
{
this.ShowPatients();
}
void ShowPatients()
{
this.DisplayedPatients = new BindingList<Patient>(this.FetchPatientsToDisplay().ToList());
}
That is all! The Patients are displayed; if allowed, the operator can add / remove / edit Patients. When finished editing, he notifies the program by pressing the OK or Apply Now button:
void ButtonOk_Clicked(object sender, ...)
{
// Fetch the edited Patients:
ICollection<Patient> editedPatients = this.DisplayedPatients;
// find out which patients are changed and process them
this.ProcessPatients(editedPatients);
}
So you don't need to Add / Remove rows by yourself. Usually the operator does this. If a default Patient is not enough for you to show, use event BindingList.AddningNew:
void OnAddingNewPatient(object sender, AddingNewEventArgs e)
{
// create a new Patient and give it the initial values that you want
e.NewObject = new Patient()
{
Id = 0, // zero: this Patient has no Id yet, because it is not added to the database yet
Name = String.Empty,
Path = String.Empty,
}
}
Maybe the following properties might be useful:
Patient CurrentPatient => (Patient) this.dataGridView1.CurrentRow?.DataBoundItem;
And if you allow multiple selection:
IEnumerable<Patient> SelectedPatients = this.dataGridView1.SelectedRows
.Cast(row => row.DataGridViewRow)
.Select(row => row.DataBoundItem)
.Cast<Patient>();
In words: interpret every selected rows in the datagridView as a DataGridViewRow. From every DataGridViewRow get the item that is DataBound to it. We know that this is a Patient, so we can cast it to a Patient.
I'm a Student struggling and working for our System, and the code I used can't the data needed
first form
public int transactionNo { get; set; }
public int processNo { get; set; }
and here's my code to get the data:
from a button click event
transactionNo = int.Parse(lblTRANSACTIONO.Text);
processNo = int.Parse(lblprocessNO.Text);
the lblTRANSACTIONO.Text and lblprocessNO has the right value,
and here's how I get the data to use it on labels of another form:
second form from the load event
FORMtransactionADD FtA = new FORMtransactionADD();//first form
lblTNo.Text = FtA.transactionNo.ToString();
lblPNo.Text = FtA.processNo.ToString();
here's the output:
dont mind the 2017000 because it is the default text of those labels
The code doesn't work, is there any other way of setting auto data and getting it?
You need to pass data in other forms constructor.
I'll try to explain this the best I can!
I've written a program that after some input, it brings 4 different text items into 4 separate ReadOnly text boxes. I'm looking to, when a user presses a button, it will store the strings from the 4 text boxes into a sort of table, so when the user then enters more data and it calculates a new set of 4 strings, when the press the button again, it will also store these values. The user should then be able to print this "table" style storage or export it into excel.
One approach could be -
You can create a class with properties to hold all the text boxes values. Something like this -
public class TextBoxesDetails
{
public string TextBox1Value { get; set; }
public string TextBox2Value { get; set; }
public string TextBox3Value { get; set; }
public string TextBoxValue4 { get; set; }
}
Next declare a global list variable of this type
List<TextBoxesDetails> TextBoxesDetails = new List<TextBoxesDetails>();
Now at the click of your button, store the values of all the text boxes into this list -
TextBoxesDetails.Add(new TextBoxesDetails {
TextBox1 = TextBox1Value.Text ,
TextBox2 = TextBox2Value.Text,
TextBox3 = TextBox3Value.Text,
TextBox4 = TextBox4Value.Text"
});
Now just use this list to export to excel.
For exporting to excel, I would suggest, https://npoi.codeplex.com/. The tool is also available through Nuget packages.
I want to display data in one control Vertically with a window form control in C#, such as:
Title: The Game
Name: Andrew Smith
Age: 44
ISBN: 123456123456
Costs: £39.99
I'm looking for a simple display with code to add and remove items..
I have a list of data which I want to display quite simply from a list which will be displayed as Title/Item as above, which won't need to be amended only viewed.
Is there a way to find out the dimensions of the text and create padding, or is there an automatically adjusting multiple column type control? Any thing with some code to stop me going mad regarding special list Views and grid type views.
You can use DataGridView. It is simple to use.
You mentioned List Views in your post, and I think that is the answer for you. Try a ListView control with View set to Details. Use the Designer to create a column for each field in your data, and then use listView1.Items.Add() to add each item. (There is a constructor for ListViewItem which takes an array of strings; the strings are the values for each column.)
Create a BookItem class which has properties shown below.
public string Title { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int ISBN { get; set; }
public string Cost { get; set; }
Generate a lstBookItems item List<> type and add BookItem items. But first, you must create bookItemInstance and fill data one by one.
List<BookItem> lstBookItems = new List<BookItem>();
lstBookItems.Add(bookItemInstance);
After this point, if you using ASP.Net
Create a Asp:ListView or Asp:GridView using Toolbox and give data source and bind data on code behind PageLoad() method
grdBooks.DataSource = lstBookItems;
grdBooks.DataBind();
And now, you can change appearence this gridView as you wish at Design screen(ex: vertical).