|
I've implemented owner drawing when adding items to a listbox. I won't waste time going into it but where I have hit a problem is with colours. Our application will read a column containing an ARGB value for a colour. This value is put into the Color.FromArgb method which returns a Color object which is then applied to the listbox value. If I use exclusively named colours like Color.Red, Color.LimeGreen that sort of thing the listbox is drawn 100% fine. But if the ARGB value isn't a named colour it fails to render anything. It effectively leaves me with white text on a white background.
Has anyone used other than named colours when painting listbox items using OwnerDrawVariable?
As I say, it works perfectly for named colours but the moment I use an ARGB value which isn't a named colour it doesn't actually do anything.
There is a method in the graphics object called GetNearestColor. I tried pumping my ARGB value into it but the Color object it emits is exactly the same as what I sent it. In other words, it doesn't convert a colour that's approximately "slightly like fuschia" for example into Fuschia which is what GetNearestColor is supposed to do. The colour that's in the database is selected by users with the colour picker dialog and they can select any colour or pick one of their own.
Curiously, the same ARGB value works perfectly in a Treeview control.
Any ideas?
"I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68).
"I don't need to shoot my enemies, I don't have any." - Me (2012).
|
|
|
|
|
I don't think you'll get an answer to this question unless you reveal the code employed to draw an item and example ARGB values that fail.
PHS241 wrote: This value is put into the Color.FromArgb method which returns a Color object
which is then applied to the listbox value.
That is just too vague.
Alan.
|
|
|
|
|
Well, like Alan said, without seeing your owner-drawing code and an example of a value that fails, the best anyone call tell you is "you screwed up your rendering code".
|
|
|
|
|
Guys,
I found the answer which I'll share. As I said, the treeview displayed the node in the right text colour and background colour. The same ARGB value applied to owner drawing in the listbox did not. The problem was the alpha component. The treeview seems to be more forgiving and accepts alpha values of 0 and 255 as one and the same. The listbox renders alpha values of 0 as fully transparent and 255 as fully opaque which is perhaps the correct result. For a listbox then, I need to force the alpha value to 255. Here is a sample code snippet:
const int anyArgbValueHere = 16744703;
Color argb = Color.FromArgb(anyArgbValueHere);
Color correctedArgb = Color.FromArgb(255, argb.R, argb.G, argb.B); // Same colour but with alpha forced.
When I paint the listbox it now works fine. Obviously, it would be better to have a method that does the conversion as you'd need to handle cases where the colour's IsEmpty property is true.
So, alpha was the problem and not a fault with my owner drawing.
"I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68).
"I don't need to shoot my enemies, I don't have any." - Me (2012).
|
|
|
|
|
I would have assumed your drawing code would have handled this situation.
|
|
|
|
|
You mean something like:
using System.Hindsight.Wonderful.Thing
"I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68).
"I don't need to shoot my enemies, I don't have any." - Me (2012).
|
|
|
|
|
probably something really dumb but I can not multiple floats and come up with anything but a whole number.
for example
float x = 200031010f;
float y = .86f;
float z = x * y;
I would expect z to be 1722668.6 but it is 1722668
Convert.ToSingle also truncates it to the whole number when converting a double.
what good is a float if multiple just gives whole numbers?
|
|
|
|
|
The float data-type[^] has a maximum precision of 7 digits. The result of your calculation requires 10 digits, and therefore cannot be stored accurately in a float .
I'm assuming you have a typo in your question. The actual result should be 172026668.6; the truncated result in a float is 172026700.
You should use either the double data-type[^], which has a precision of 15-16 digits, or the decimal data-type[^] which has more precision but a smaller range.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You are losing precision as the float type is not big enough for these numbers. Change to double (which you should always use) and it works.
Use the best guess
|
|
|
|
|
durn. I am converting some code in vb6 that passes a single value in a string used in password calculations. I can't use double because the message is for 4 bytes. I need to figure out how to get c# to calculate the same numbers in the same byte format.
in vb6 20031010 * .86 returns
1.722667E+07
1,722,668 is not a very large number and if you can't have more than 7 significant digits it kind of defeats the purpose of having a floating point number is the first place. microsoft says a float has a range of
-3.4 × 10^38 to +3.4 × 10^38
|
|
|
|
|
notahack wrote: microsoft says a float has a range of -3.4 × 10^38 to +3.4 × 10^38
Microsoft also says that float has a precision of seven digits.
Remember that a floating-point number can only approximate a decimal number, and that the precision of a floating-point number determines how accurately that number approximates a decimal number. By default, a Single value contains only 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. The precision of a floating-point number has several consequences:
- Two floating-point numbers that appear equal for a particular precision might not compare equal because their least significant digits are different.
- A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used because the floating-point number might not exactly approximate the decimal number.
- A value might not roundtrip if a floating-point number is involved. A value is said to roundtrip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is equal to the original floating-point number. The roundtrip might fail because one or more least significant digits are lost or changed in a conversion.
notahack wrote: in vb6 20031010 * .86 returns 1.722667E+07
And in C#, 20031010f * .86f returns 1.722667E+07. What's the problem?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
the problem is it does not return 1.722667E+07, it returns 1722668.0
I must be special.
|
|
|
|
|
I've just tried this in .NET 4.0 and 3.5, and the result is the same in both:
float x = 20031010f;
float y = .86f;
float z = x * y;
Console.WriteLine(z);
Console.WriteLine("{0:F}", z);
Output:
1.722667E+07
17226670.00
If your code is returning 1722668.0 then you're doing something wrong; that's off by a factor of 10.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
you are correct and my results are even more evil 17226668.0
I don't doubt I am doing something wrong but really the code is exactly as you demonstrated, simple simple but frustrating.
|
|
|
|
|
Try this:
float z = 20031010f * .86f;
Debug.WriteLine( z.ToString("G") );
The two values ARE equal internally. What you see on screen is just the differnt ways the two environments chose to represent the STRING version of those numbers.
|
|
|
|
|
Yes, but a type like float cannot represent all of the numbers in that range since there are an infinite number of them!
How it works is there are a finite number of bits (32 in this case) to store a floating point value, split into 3 seections. The first bit is the sign bit. The next 8 bits are the exponent value and the remaining 23 bits are the fraction value. This gives you a sum total of 7 digits of numeric representation.
What does that mean? Well, if you want to represent a very large number, the format sacrifices accuracy by rounding off the least significant digits of the fraction value and increasing the value of the exponent to scale the fractional part of the number to the appropriate range.
decimal 10 = 0000001 * 10 ^ 1 (10 ^ 1) = 10
^ ^
| |
fraction exponent
decimal 1234567 = 12D687 * 10 ^ 0 (10 ^ 0) = 1
1234567 can be represented by 21 bits: 1 0010 1101 0110 1000 0111
so it fits inside the 23 bits available for the fraction part.
decimal 12345678 = ??????
Hmmmm... This requires 24 bits to represent: 1011 1100 0110 0001 0100 1110
This won't fit inside the 23 bit fraction part!
What to do, what to do, .... ??
The answer to this little problem is simple. Sacrifice a little precision and use scientific notation! Divide the number by 10 (remember what the exponent is raising) and round it off! This gives us 1234567.8 = 1234568.0. Now, we've got a 7 digit number that fits inside 23 bits. But there's that pesky division we did. Since we divided by 10, we have to raise the value of the exponent to compensate: 10 ^ 1. Now our number looks like this: 1234568.0 * 10 ^ 1, or 1.234568 * 10 ^ 7.
That's the quick'n'grossly simplified version of what's going on. For more read this[^]
modified 26-Feb-13 15:17pm.
|
|
|
|
|
Hi all
I have an annoying problem working with EF5 and Enums, As Enums is supposed to be supported with EF5 I thought this should be an easy matter, but sadly I can't get it behave
I have an Enum
public enum Senddays
{
AllDays = 9,
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
}
And then I have this Class containing a List of said Enums
[Serializable]
public class SubscriptionFrequency
{
public Guid ID { get; set; }
public string Name { get; set; }
public List<Senddays> Senddays { get; set; }
public DeliveryMedia DeliveryMedia { get; set; }
public bool Active { get; set; }
public SubscriptionFrequency()
{
ID = Guid.NewGuid();
}
}
See straight forward.. or so it seems, but when I try to persist it by EF5 It doesn't create a colum for the Senddays List, but eveything else gets in even the DeliveryMedia, which is also an Enum ??
How do I get the List into the DB ?
Best Regards
|
|
|
|
|
You have defined two different meanings of the term 'Senddays' (it's a type, your enum, and a variable, your list). Is it possible EF5 is confused?
|
|
|
|
|
No the definition in the Class : SubscriptionFrequency
is a List of the Enum type .. that should not create any cause for confusion, and it works throughout the code ..
And the DeliveryMedia is also an Enum and that works as intended..
|
|
|
|
|
Hi,
DeliveryMedia is a enum type, so it sets and gets and enum, but List<senddays> is a list containing objects that are cast as Senddays type. so when it has been read you may have to cast it back oreven better if you convert to "List.ToArray()" which will list the values as Senddays type.
Regards
Jegan
Think! Don't write a line of code unless you absolutely need to.
|
|
|
|
|
Hello everyone,
I need C# code for generating a key from fingerprint using minutiae...If anybody have any idea share with me please....
|
|
|
|
|
Please have a look at this paper: Generate a key for MAC Algorithm using
Biometric Fingerprint[^] by Dr.R.Seshadri and T.Raghu Trivedi.
Eventhough I suggest you start reading it from the start, chapter four sounds very promising to your cause: 4. Generating key vector from finger print
After that you can break down your tasks into manageable bits and start coding it. When you run into any specific problems feel free to come back and ask more sensible questions.
Regards,
— Manfre
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Why can't I be really rude to this pillock, has not read the guidelines, asking for the code, the only thing missing is some text speak and a demand for an urgent response!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Hello
I have a requirement to write pdfdocument using html contents in an ASP.NET MVC Project. I am writing the HTML code inside a string variable and then I am giving that string for rendering it as pdf bytes using the below code. The returned bytes I am saving it as pdf on the server. However I am getting too many formatting issues in the pdf generated.
When I saved the html contents which I passed to the string, formattings are good but somehow when I am trying to create a pdf out of those contents I am getting issues. Any help will be greatly appreciated.
public byte[] Render(string htmlText)
{
byte[] renderedBuffer;
using (var outputMemoryStream = new MemoryStream())
{
using (var pdfDocument = new Document())
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
//pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
pdfDocument.Open();
using (var htmlViewReader = new StringReader(htmlText))
{
using (var htmlWorker = new HTMLWorker(pdfDocument))
{
htmlWorker.Parse(htmlViewReader);
}
}
}
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
return renderedBuffer;
}
|
|
|
|