|
Judah Himango wrote: When will you be writing an article
When the new stuff becomes available and only if MSDN fails to document it properly.
I'll try and come up with some other article topics in the mean time...
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Judah Himango wrote: opposite of what he's looking for.
Well, not really. Since he wants the greatest, he just needs to know whether to get it from array[0] or array[array.Length(-1?)], once it is sorted.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
More than one way to skin a cat.
Likewise, you could use
var greatest = ints.OrderBy(i => i).Last();
I'd argue the LINQ way is more declarative, the intent is clearer, and allows for more advanced abstractions. Whatever the case, though, it probably doesn't matter to the guy asking the question.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
modified on Thursday, April 2, 2009 12:08 PM
|
|
|
|
|
Judah Himango wrote: More than one way to skin a cat
First you have to catch it.
Good job he's using .Net.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
How to create table using rectangle in pictureBox1???
Thanks before...
Iman Ridwan
|
|
|
|
|
Hook up to the pictureBox.Paint event. Inside the event handler, you can draw whatever you like onto the picture box using the Graphics[^] object passed in the event args.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Hi all,
Is there any build in string comparison or filter method that could ignore escaped character(like /r/n) & html tags(like <br> )?
I've tried the regular expression like "Regex.Replace(xml, "\\.", "");" for escape characters, but it didn't work.
Thanks,
<div class="ForumMod">modified on Wednesday, April 1, 2009 11:11 PM</div>
|
|
|
|
|
There isn't such a thing built in the .NET framework class libraries.
You could build your own like this:
public static string GetStringWithoutEscapedSequences(string input)
{
var escapedSequences = new[] { "\r\n", "<br>" };
foreach(var sequence in escapedSequences)
{
input = input.Replace(sequence, string.Empty);
}
return input;
}
Then compare your strings like this:
var first = GetStringWithoutEscapedSequences(strA);
var second = GetStringWithoutEscapedSequences(strB);
var areEqual = string.Equals(first, second);
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
I have a form with a particularly slow loading process. To acheive a more responsive UI I decided to use a seperate thread to load the data and populate the datagridview.
If called direct the form takes 10 secondes to display.
I have a separate method called LoadGrid to do that work.
The following is called in the form_load method
Thread oThread = new Thread(new ThreadStart(LoadGrid));
oThread.IsBackground = true;
oThread.Start();
LoadCombos();
UIUtils.BindObject(this, cashFlow);
/pre>
My expectation is that the form will display, the user can manipulate the controls and the DGV will load 10 seconds after the form is initiated. This does not happen, I get a partial display and the form locks, 10 seconds later the form completes loading and releases the UI.
Where am I screwing it up.
The LoadGrid is correctly delegated and the thread is invoked properly,
<pre> private delegate void LoadGridDelegate();
private void LoadGrid()
{
try
{
if (this.InvokeRequired)
{
this.BeginInvoke(new LoadGridDelegate(LoadGrid), null);
}
else
{
DataTable dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
UIUtils.dgLoad(dgAttr, dtAttr.DefaultView, "");
}
}
catch (Exception)
{ throw; }
}
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Try calling grid.Update() after invoking.
Also, don't catch the Exception if you're just gonna throw it.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
The grid is not a problem, it updates correctly when the data is returned. The issue is that the rest of the form also waits for the thread to complete before it finishes form_load.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Your problem is that the UI thread is still doing the main work. You spawn a thread, and when that thread executes, it simply delegates the work back onto the UI thread. Woops.
Assuming that getting the DataTable is the part that's freezing your UI, here's what the correct code should look like, using the recommended pattern of the BackgroundWorker component.
class Form1 : Form
{
public Form1()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (sender, e) =>
{
e.Result = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
};
worker.RunWorkerCompleted += (sender, e) =>
{
var dataTable = (DataTable)e.Result;
UIUtils.dgLoad(dataTable, dataTable.DefaultView, "");
};
worker.RunWorkerAsync();
}
}
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Thanks Judah, I pinched Navaneeths solution.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Your LoadGrid method has the problem..
private void LoadGrid()
{
try
{
if (this.InvokeRequired)
{
this.BeginInvoke(new LoadGridDelegate(LoadGrid), null);
Now <code>InvokeRequired</code> will be false and else conditions executes.
}
else
{
DataTable dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
UIUtils.dgLoad(dgAttr, dtAttr.DefaultView, "");
}
}
catch (Exception)
{ throw; }
} Here is the corrected code,
private void LoadGrid()
{
DataTable dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
this.BeginInvoke((MethodInvoker)delegate
{
UIUtils.dgLoad(dgAttr, dtAttr.DefaultView, "");
});
}
|
|
|
|
|
Magic stuff - exactly what I was expecting from my code crap. Now to do some research on MethodInvoker
Thanks a lot!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: Now to do some research on MethodInvoker
It's just a delegate which can point to any method which will have no parameters.
|
|
|
|
|
[MSDN]This API supports the .NET Framework infrastructure and is not intended to be used directly from your code[/MSDN]
This would normally make me leery of using it.
N a v a n e e t h wrote: method which will have no parameters.
I presume this limitation is the reason it is not recommended. In which case the background solution will need to looked into as well.
Thanks.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
In that case you can create your own delegate with required parameters. I have seen many applications using MethodInvoker when you need to invoke a simple parameter-less method. I am not sure why MSDN say's not to use it.
|
|
|
|
|
Hi Mycroft,
getting the DataTable is what takes most of 10 seconds and needs to be delegated to a thread.
Your LoadGrid() method uses Invoke to cause the GUI thread to do it anyway, so your efforts to speed up things are pointless so far.
Here is unchecked code to get the DataTable on the thread and the DGV update on the UI thread;
it does not rely on any fancy C# code, it should work on all .NET versions:
private void LoadGrid(DataTable dtAttr) {
if (this.InvokeRequired) {
dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
this.BeginInvoke(new LoadGridDelegate(LoadGrid), dtAttr );
} else {
UIUtils.dgLoad(dtAttr, dtAttr.DefaultView, "");
}
}
With anonymous methods/delegates you can further simplify it as others have shown.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi,
I am attempting to unit test some code i have written. Unfortunately I am having an issue when using a generic list of an object type I have defined. I'm basically checking to see that this list is equal to 0 and if it is return false. However when the check is performed I get a null exception error. Below is an example (not exact code) of the code I'm trying to test and the bold and underline is where i'm receiving the error. Thanks in advance to all that reply.
private Car _car = new Car();
//constructor
public CarValidation(Car carDetails)
{
if (carDetails == null)
throw new ArgumentNullException();
else
_car = carDetails;
}
public bool CheckForValues()
{
bool valid = true;
if(_car.OilPressure == null)
valid = false;
if (_car.TirePressure == null)
valid = false;
if (_car.GasAmount == null)
valid = false;
if (_car.Mp3SongsLoaded.Count == 0)
valid = false;
return valid;
}
|
|
|
|
|
car.Mp3SongsLoaded is null. Make sure you initialize it before using it, e.g. car.Mp3SongsLoaded = new List<string>();
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Thanks. Totally forgot to initialize the list.
|
|
|
|
|
Hi all,
I've been having a little problem with the WebClient class, and am hoping someone can help.
Here is what I'm currently working with:
<br />
string uri = "WebURL";<br />
<br />
byte[] postBytes = Encoding.ASCII.GetBytes(postString_Begin);<br />
byte[] postBytes2 = Encoding.ASCII.GetBytes(postString2);<br />
<br />
WebClient myClient = new WebClient();<br />
myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");<br />
<br />
byte[] response = myClient.UploadData(uri, postBytes);<br />
<br />
string myresponse = Encoding.ASCII.GetString(response);<br />
<br />
response = myClient.UploadData(uri2, postBytes2);<br />
Here is where I hit my wall.
After I upload the first queryString, the next step in the process is returned.
The URL does not change, but when I go to post the next section of values, it posts them back to the first page/form part.
Can anyone point me to How I can post my second querystring to the returned page from the first request?
Thanks
|
|
|
|
|
Hi,
I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:
private void PopulateCountries()
{
cbCountryId.DataSource = dsCountries.Tables["Country"];
cbCountryId.DisplayMember = "Country";
cbCountryId.ValueMember = "Id";
}
What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:
private void GetStateByCountryId(int CountryId)
{
this.dsWBGT_V4 = dataCommunicator.GetStateByCountryId(CountryId);
this.stateBindingSource.DataSource = this.dsWBGT_V4;
}
As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:
CountryId = Convert.ToInt32(cbCountryId.SelectedValue);
Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??
Illegal Operation
|
|
|
|
|
In your selectedindexchanged event test that there is a valid item
if(DGV.SelectedItems.count == 0)<br />
{return ;}
This is a common issue - an index changes twice - deselects on [0] and selects[4] so the event fires twice. This is needed for all collection controls, you just need to find the correct property to test for each control type.
Never underestimate the power of human stupidity
RAH
|
|
|
|