|
Thanks to all! this was very helpful!
|
|
|
|
|
Don't forget the 5's all round then
|
|
|
|
|
originSH wrote: 128 / 3 = 42.66 recurring. Since you are dealing with int's here this is rounded, and rounded to the nearest even number i.e. 42
42.49 = 42 (-)
42.50 = 43 (+)
This's rounding, that was dropping!
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
No it uses bankers rounding ... which is to the nearest even number.
i.e.
5.6 will round to 6
6.6 will round to 6
|
|
|
|
|
After further investigation ... your right!
I was wrong ... it uses truncation and not bankers rounding ... just goes to show it's always good to do a little follow up investigation on what info you find
|
|
|
|
|
Really!!!!
originSH wrote: bankers rounding...
6.6...
.. Thanks a million origin!
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
never.reply wrote: Returns 172 when it should return 175.
No, it shouldn't. You use integer division, so 128 / 3 = 42.
never.reply wrote: VB Code returns 175 which is correct as followed:
Dim Num As Integer = (((1024 / 8) / 3) * 4) + 4
That's because the / operator in VB is always a floating point operator. It converts both operands to double before doing the division.
The equivalent of x / y in VB is (double)x / (double)y in C#.
---
single minded; short sighted; long gone;
|
|
|
|
|
Hi, sorry for the basic question but I am new to C# and finding some of it confusing. Basically I want to fill lots of combo boxes with items when the form is loaded with the information coming from a mysql database. My question is what's the best method to do this so it does not freeze the program whilst it is retrieving the information and then adding them to the combo boxes. I have looked at background workers but that leads to cross threading.
Thank you for your time...
Karlos
|
|
|
|
|
if u r using datasets to bind the combo boxes then u should try to fill datasets on constructor of form the bind it to combo boxes on form load.
rahul
|
|
|
|
|
I havent been using datasets but i shall look at them now. I have a MDI so the user may have multiple forms open, whilst they are loading a form they may want to work on another. Will using datasets in the mentioned way although this?
Thanks
|
|
|
|
|
If you don't want to freeze the UI then you'll need another thread. If you do lots of work o nthe UI thread then it can't do anything else ... like update the UI
Thats why the BGW is handy ... it encapsulates alot of the threading stuff for you and makes things a bit easier.
As long as you keep things simple the only extra work needed for the threading is making sure you use control.InvokeRequired and control.Invoke.
Heres a quick example:
When ever you want to set a property or call a method you need to check InvokeRequired. If InvokeRequired is true then you need to use the Invoke method.
If if your original code is this:
<br />
comboBox1.Items.Add(item)<br />
your new code will be this:
<br />
if (comboBox1.InvokeRequired)<br />
comboBox1.Invoke(comboBox1.Items.Add(item));<br />
else<br />
comboBox1.Items.Add(item);<br />
THis will protect your code for use with threading.
Whilst this code looks pretty simple it's actually using some of the new features added in .net 2.0 to simplify it. It's using something called "Anonymous Methods".
What actually going on here is that delegates are being used.
An easy way to imagine delegates are as definitions of the method you want to call i.e. they define the parameters and return value of the method.
This allows you to pass a method to be called i.e. I want you to call MyMethod(string someParam) when your done.
Now rather than having to make the method and the delegate for Invoke to call we use "Anonymous Methods" to say, well you know we are going to have a delegate and a method here and it's going to be simple, so how about we just give you the method body, in this case 'comboBox1.Items.Add(item)', and you can work out the rest.
Hopefully I haben't used any concepts you haven't yet learnt about, and I haven't just confused you even more :P
Feel free to reply with any more questions and I'll do my best to explain and unconfuse.
|
|
|
|
|
Thats excellent, I shall have a little play around and see what I can do with it. I have spent the past few years programming in Delphi and not really had to deal with threads, this is all taking me back to my days at university... the brain has slowed down a bit since then, doesnt like new concepts anymore.
Thanks for your help
|
|
|
|
|
Threading can be a right pain i nthe arse :P
But since it's used so much in the UI, MS have made it a bit easier up at the front end
|
|
|
|
|
Hi,
I have been playing around with the backgroundworker again, I get the following errors when I try using InvokeRequired and Invoke in the backgroundworker DoWork code
Error 1 The best overloaded method match for 'System.Windows.Forms.Control.Invoke(System.Delegate)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'int' to 'System.Delegate'
if (cboComments.InvokeRequired)
cboComments.Invoke(cboComments.Items.Add(tempString));
else
cboComments.Items.Add(tempString));
What am i doing wrong? I need a brain upgrade!
Thanks for you time
|
|
|
|
|
ahhhh dang it. In this case the method we're calling doesn't conform to the System.Delegate, which wants a void Method();.
Add this code to your class:
<br />
private delegate void AddItemDelegate(object item);<br />
<br />
private void AddComboBoxItem(object item)<br />
{<br />
cboComments.Items.Add(item);<br />
}<br />
see how the delegate defines the method signature of the method?
private delegate "void AddItemDelegate(object item)"
private "void AddComboBoxItem(object item)"
And change your Invoke method call to:
<br />
cboComments.Invoke(new AddItemDelegate(AddComboBoxItem), item);<br />
this creates a new delegate of our specific definition, tells it to point at our 'AddComboBoxItem' method and passes item as a parameter.
The reason you have to use delegates and Invoke is that now you have said ... I want to use this method please, and the control will execute that method on the appropriate thread (i.e. the UI one), which prevents the cross threading issues.
|
|
|
|
|
Karlos_V wrote: I have looked at background workers but that leads to cross threading.
Control.CheckForIllegalCrossThreadCalls = false;
Although not healthy, it will do the trick.
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
yeah might was well switch to VB, turn off option strict and use goto error :P
The check is there for a reason, as you say it's not healthy and so theres no point even metioning it. It's also much harder to debug threading bugs than to explain how to do things correctly in the first place!
|
|
|
|
|
originSH wrote: well switch to VB
That should do it
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
Why would you tell a newbie that information? My team leader's head would explode if I tried anything like that.
|
|
|
|
|
Oops, sorry
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
in case of normal listbox it is possible with the usage of "IndexFromPoint" method but for checked list box there is no such method. So pls can any one tell me how to proceed
string strTip = "";
//Get the item
int nIdx = lblRecords.IndexFromPoint(e.Location);
if ((nIdx >= 0) && (nIdx < lblRecords.Items.Count))
strTip = lblRecords.Items[nIdx].ToString();
toolTip1.SetToolTip(lblRecords, strTip);
Thanks in advance.
|
|
|
|
|
I have a webapp where users can search for PDF-documents in database. They enter the searchcritera in a textbox and presses search. All that works fine but I have problem with clearing the textbox after pressing the searchbutton. The code for the button is:
protected void btnSearchNumber_Click(object sender, EventArgs e)
{
try
{
//readPDF gets the document from database
readPdf(TextBox1.Text);
TextBox1.Text = "";
}
catch
{
lblNoPDF.Text = "NO PDF-DOCUMENT FOR GIVEN MRB WAS FOUND";
TextBox1.Text = "";
}
}
If no document was found the catchblock successfully clears the textbox. But if document successfully opens, the textbox is not cleared. I have also tried to clear the textbox in a finally block with no success. There is probably a simpel solution to this but I havn't found it.
Anyone who can help?
Gitsu
|
|
|
|
|
Your code is obviously not getting past the readPdf so I would guess that the method isn't returning an expected value if successful.
If the readPdf method isn't something you can change, you could copy the TextBox1.Text value to a string variable at the top of the try block, clear the text box then call the readPdf method passing the string.
I'd alter the readPdf method if possible though as things are not going to be disposing properly otherwise.
Dave
|
|
|
|
|
I have tried copying the text to a string, but that doesn´t work either. The readPDF method is:
void readPdf(string mrb)
{
try
{
DataRow row = handler.getReport(mrb).Tables[0].Rows[0];
Byte[] pdfData = (byte[])row["reportFile"];
int mrbID = (int)row["mrbID"];
Response.ContentType = "application/PDF";
Response.AddHeader("content-disposition", "attachment; filename = " + mrbID + ".pdf");
Response.BinaryWrite(pdfData);
}
catch
{
throw;
}
}
Can you see if there is something wrong here? The method returns the PDFfile successfully if it exists.
|
|
|
|
|
It looks fine to me
This should work to get you out of trouble:
protected void btnSearchNumber_Click(object sender, EventArgs e)
{
try
{
string thisPDF = TextBox1.Text;
TextBox1.Text = "";
System.Windows.Forms.Application.DoEvents();
//readPDF gets the document from database
readPdf(thisPDF);
}
catch
{
lblNoPDF.Text = "NO PDF-DOCUMENT FOR GIVEN MRB WAS FOUND";
}
}
but I would like to know why it doesn't work in your original code!
Maybe, as an experiment, try returning a bool value from the readPdf
bool readPdf(string mrb)
{
try
{
DataRow row = handler.getReport(mrb).Tables[0].Rows[0];
Byte[] pdfData = (byte[])row["reportFile"];
int mrbID = (int)row["mrbID"];
Response.ContentType = "application/PDF";
Response.AddHeader("content-disposition", "attachment; filename = " + mrbID + ".pdf");
Response.BinaryWrite(pdfData);
return true;
}
catch
{
throw;
return false;
}
}
and show the result in a message box
protected void btnSearchNumber_Click(object sender, EventArgs e)
{
bool result;
try
{
//readPDF gets the document from database
result = readPdf(TextBox1.Text);
System.Windows.Forms.MessageBox.Show(result.ToString());
TextBox1.Text = "";
}
catch
{
lblNoPDF.Text = "NO PDF-DOCUMENT FOR GIVEN MRB WAS FOUND";
TextBox1.Text = "";
}
}
If you don't get a message box when the readPdf is successful then there's something seriously screwy going on somewhere.
|
|
|
|