|
public Country(string cAbs, string cName)
{
this._country = cName;
this._abs = cAbs;
}
private void btnOk_Click(object sender, EventArgs e)
{
Country myCountry = new Country();
CountryList countries = new CountryList();
countries.Add(new Country("AE", "United Arab Emirates"));
countries.Add(new Country("AF", "Afghanistan"));
countries.Add(new Country("AQ", "Antarctica"));
countries.Add(new Country("AR", "Argentina"));
countries.Add(new Country("AT", "Austria"));
countries.Add(new Country("AU", "Australia"));
countries.Add(new Country("BD", "Bangladesh"));
countries.Add(new Country("BE", "Belgium"));
countries.Add(new Country("BR", "Brazil"));
countries.Add(new Country("BS", "Bahamas"));
countries.Add(new Country("CA", "Canada"));
countries.Add(new Country("CH", "Switzerland"));
countries.Add(new Country("CL", "Chilie"));
countries.Add(new Country("CM", "Cameroon"));
countries.Add(new Country("CL", "Chilie"));
countries.Add(new Country("CN", "China"));
countries.Add(new Country("CO", "Columbia"));
countries.Add(new Country("CR", "Costa Rica"));
countries.Add(new Country("DK", "Denmark"));
countries.Add(new Country("DM", "Dominica"));
countries.Add(new Country("DO", "Domican Republic"));
countries.Add(new Country("EC", "Ecuador"));
countries.Add(new Country("EE", "Estonia"));
countries.Add(new Country("EG", "Egypt"));
countries.Add(new Country("ES", "Spain"));
countries.Add(new Country("ET", "Ethiopia"));
countries.Add(new Country("EU", "European Union"));
}
Question:
1. How to find the a country in a list?
|
|
|
|
|
Country foundCountry = CountryList.FirstOrDefault(c=>c.CountryAbreviation == this.textBox1.Text);
if (foundCountry==null)
{
}
Have you considered Using a
Dictionary<String, Country>
that would allow
CountryList.ContainsKey(this.textBox1.Text)
and
Country foundCountry = CountryList[this.textBox1.Text]
What you want to do will depend on if you have more than one item with the same abreviation
|
|
|
|
|
Just want to point out
Quote: Country foundCountry = CountryList[this.textBox1.Text]
in above code there may be chances of KeyNotFound Exception[^]
Anyway 5+
Thanks
-Amit Gajjar (MinterProject)
|
|
|
|
|
thanks, i should have been a little more clear, the CountryList.ContainsKey check should be called first before attempting access via CountryList[this.textBox1.Text].
Thanks for the 5
|
|
|
|
|
You can use TryGetValue (in recent .Net, but if you're already suggesting Linq then it is also available) which will do the contains check and the lookup in a single call.
Country c;
if(dict.TryGetValue(text, ref c)){
}
|
|
|
|
|
IEnumerable<country> filteredCountry = countries.Where(f => f.GetAbbreviation == "EG");
if (filteredCountry != null)
{
Country countryOne = filteredCountry.First();
}
try out the above code !!!!
|
|
|
|
|
if (filteredCountry != null)
this will not ever be null as it is assigned on the line above, but it may contain no results if there are no matches, causing First to return an InvalidOperationException.
FirstOrDefault then a null check would be safer
|
|
|
|
|
countries.FirstOrDefault(q=>q.cName == country_search);
Regards
Christian Amado
MCITP | MCTS | MOS | MTA
Olimpia ☆ ★★★
Please mark as answer, if it helps.
|
|
|
|
|
In a silverlight app, I have a BitmapImage defined as System.Windows.Media.Imaging.BitmapImage and it as a method called "SetSource" where I can set the source like this:
BitmapImage bitmap = new BitmapImage();
System.IO.Stream stream = _scene.GetStream();
if (stream == null) return;
bitmap.SetSource(stream);
In a WPF application I have also have a
Bitmap image defined as System.Windows.Media.Imaging.BitmapImage buty there is no SetSource method. How do I set the source in a WPF app like I do in a Silverlight app?
|
|
|
|
|
Xarzu wrote: How do I set the source in a WPF app like I do in a Silverlight app?
The Image class holds a FromStream[^]-method that you can use. The Bitmap class can be created and passed the Stream in it's constructor[^] as a parameter.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
This worked
System.IO.Stream stream = _scene.GetStream();
if (stream == null) return;
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
|
|
|
|
|
hi Chris Maunder
please visit our site and send your poll my web !
WWW.ZUNKAN.IR
very tancks...
hi!
i am mohammad mirshahi,i live in iran.please contact me....
|
|
|
|
|
Chris tends not to frequent this forum. If you need to get in touch with him, you need to post in the Site Bugs and Suggestions forum which is the best location to find him.
I seriously doubt you will get much joy though - I took a look at your site. Code Project is an English language site - why do you think part of it will sit well with your site?
|
|
|
|
|
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
By the above code I can change the system date of many version of MicroSoft Windows 7, but doesn't work on all of them. Do I miss something?
|
|
|
|
|
As this call requires the SE_SYSTEMTIME_NAME privilege, I suspect you've hit a permissions issue.
|
|
|
|
|
Only an administrator account can change the system time.
If your code is launched by a normal user it's not going to work.
|
|
|
|
|
Hi friends,
Are there any methods to refuse changes of controls in a form ?
For example: I have a form that include one text box: txtNumber, and one Exit button.
At the first time, txtNumber.Text = "30" (When showing form)
Then, I changed the its value into "45". After that, I don't want to accept changes, I clicked the Exit button. Then txtNumber.Text is still "45". I want this value is "30".
Are there any method to refuse changes of TextBox in this case ?
Thanks and regards,
|
|
|
|
|
Simply set the ReadOnly property to true to prevent anybody from being able to change it via the UI.
|
|
|
|
|
allow to change (not Readonly)
|
|
|
|
|
Are you meaning that you want to be able to either commit the changes if a "save/Commit" button is pressed, and undo changes if the exit button is pressed?
If that is the case, your exit button could simply trigger the method you used to set the values when showing the form.
|
|
|
|
|
Thanks Ed Hill_5_,
That is my mean, I will try it
|
|
|
|
|
Please, in future, try to be clearer with your questions. There is no way, from your original post, that I would have been able to guess what your question actually was.
|
|
|
|
|
simply save the pre value and return it in case of pressing exit buttom.
|
|
|
|
|
i have the code below and want to convert the results in var rows to a DataTable
var rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();
|
|
|
|
|
It would obviously depend on what sort of object is your Row class. Maybe your best bet would be to create the DataTable first and then add each DataRow while reading the csv files, instead of creating the List<row>.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|