|
AFAIK you cannot mix standard format elements (f) in custom format so you'll have to create the whole format string. For example
(0.40m).ToString("###,##0.00###;-###,##0.00###;#")
|
|
|
|
|
For 1.1m your format shows 2 places after comma, but I want only 1 place as F.
Which custome format is exactly equivalent to F format ?
Andrus.
Andrus
|
|
|
|
|
If you want just one zero after comma, change the 0 to #:
(1.1m).ToString("###,##0.00###;-###,##0.00###;#") to
(1.1m).ToString("###,##0.0####;-###,##0.0####;#")
I'm not sure about F's format since I believe that it is affected by number settings in control panel. For more info Standard Numeric Format Strings[^] and Custom Numeric Format Strings[^]
Mika
|
|
|
|
|
F format behaviour on decimal is not affected by any settings:
0m returns 0
1.10m returns 1.10
1.1m returns 1.1
1.123m returns 1.123
How to obtain same behaviour with format string except zero is converted to blank string ?
Andrus
|
|
|
|
|
From the manual:
If format is null or an empty string, the return value of this instance is formatted with the general numeric format specifier ("G").
Simple test case:
System.Diagnostics.Debug.WriteLine("-------");
System.Diagnostics.Debug.WriteLine("Using F");
System.Diagnostics.Debug.WriteLine("-------");
System.Diagnostics.Debug.WriteLine("(0m).ToString(\"F\") => " + (0m).ToString("F"));
System.Diagnostics.Debug.WriteLine("(1.10m).ToString(\"F\") => " + (1.10m).ToString("F"));
System.Diagnostics.Debug.WriteLine("(1.1m).ToString(\"F\") => " + (1.1m).ToString("F"));
System.Diagnostics.Debug.WriteLine("(1.123m).ToString(\"F\") => " + (1.123m).ToString("F"));
System.Diagnostics.Debug.WriteLine("------------------");
System.Diagnostics.Debug.WriteLine("Using empty string");
System.Diagnostics.Debug.WriteLine("------------------");
System.Diagnostics.Debug.WriteLine("(0m).ToString() => " + (0m).ToString(""));
System.Diagnostics.Debug.WriteLine("(1.10m).ToString() => " + (1.10m).ToString(""));
System.Diagnostics.Debug.WriteLine("(1.1m).ToString() => " + (1.1m).ToString(""));
System.Diagnostics.Debug.WriteLine("(1.123m).ToString() => " + (1.123m).ToString(""));
System.Diagnostics.Debug.WriteLine("-------");
System.Diagnostics.Debug.WriteLine("Using G");
System.Diagnostics.Debug.WriteLine("-------");
System.Diagnostics.Debug.WriteLine("(0m).ToString(\"G\") => " + (0m).ToString("G"));
System.Diagnostics.Debug.WriteLine("(1.10m).ToString(\"G\") => " + (1.10m).ToString("G"));
System.Diagnostics.Debug.WriteLine("(1.1m).ToString(\"G\") => " + (1.1m).ToString("G"));
System.Diagnostics.Debug.WriteLine("(1.123m).ToString(\"G\") => " + (1.123m).ToString("G"));
results:
-------
Using F
-------
(0m).ToString("F") => 0,00
(1.10m).ToString("F") => 1,10
(1.1m).ToString("F") => 1,10
(1.123m).ToString("F") => 1,12
------------------
Using empty string
------------------
(0m).ToString() => 0
(1.10m).ToString() => 1,10
(1.1m).ToString() => 1,1
(1.123m).ToString() => 1,123
-------
Using G
-------
(0m).ToString("G") => 0
(1.10m).ToString("G") => 1,10
(1.1m).ToString("G") => 1,1
(1.123m).ToString("G") => 1,123
So are yuo actually looking for "G" like behaviour? AFAIK you must implement your own converter which has the knowledge of the actual input string.
|
|
|
|
|
Yes. I'm sorry my previous letter was wrong, f format cannot used.
I want that result string contains as may decimal places after comma as
decimal value actually contains
*except* zero must be converted to empty string.
ToString() without argument, with empty format string or with G format as you demostrated produces this output
except it outputs 0m as 0 :
0.400m is converted to 0.400
123.40m is converted to 123.40
123.4m is converted to 123.4
0m is converted to 0
How to create format string which works as above except 0m, 0.00m etc is converted to empty string ?
I tried ";;#" format string but this does not output any digits.
I'm using FYI RDL report engine which allows to use only .NET format strings, no code.
Andrus
|
|
|
|
|
Sorry to say, but don't know any solution by using only format strings.
If you are using this Project RDL - Open Source Report Definition Language implementation in C#[^] I noticed that expressions and user written functions are supported (for example iif). Perhaps using these would help you.
Another option could be to preformat the numbers before they are given to report engine (converting them to strings) in which case you can control the output in your code.
|
|
|
|
|
Hi
I want to start a console app from a windows form(VS 2005) and show messages sent by the console app in a textbox, so I've used CommandLineProcess.
I declared an instance like this:
CommandLineProcess cmdLP = new CommandLineProcess();
then, I attached an event handler to the outputline event in the constuctor like this:
cmdLP.OutputLine += new CommandLineProcess.OutputLineEventHandler(OnOutputLine)
The OnOutputLine method looks like this:
private void OnOutputLine(object sender, CommandLineProcess.OutputLineEventArgs e)
{
try
{
if ((e.Text == null) || (e.Text.Length == 0))
{
return;
}
Form1.txtDepDetails.Text = e.Text;
}
catch (Exception ex)
{
throw ex;
}
}
The error i get when i try to run this line
Form1.txtDepDetails.Text = e.Text;
"Source = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
I have searched the web but found solutions for web apps, not for win forms.
And i have already made sure that the app is starting in debug mode and that Supress JIT optimization is checked.
Could you give me some suggestions?
Thank you very much.
|
|
|
|
|
netDeveloper wrote: "Source = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
That just means that the debugger could not evaluate something because a native frame (in this case, the set_Text call) is on top of the stack.
The error is something different: Windows Forms is not thread-safe, but asynchronous events like this run on a separate thread. You need to use Form.Invoke / Form.BeginInvoke to access Windows Forms controls from another thread:
form1.BeginInvoke(new MethodInvoker(delegate { form1.txtDepDetails.Text = e.Text; }));
Note that I've made form1 a reference that you'll have to pass to the class containing OnOutputLine. Don't make text boxes "static"!
|
|
|
|
|
The weird thing is that it doesn't even see form1.
This fails
if (Program.form1.InvokeRequired)
|
|
|
|
|
I'm trying to set my program in a way that before the program or I mean the form is to be closed, the program checks whether a text box has a value. So what I did is to make use of the closing event of the form... ok so when the form is closing, the program checks if there is a value on the text box and if empty the program asks the user through a dialog box like "You left that #)($*#$ blank.". When i run the program, the message box pops out so now the next thing to do is to stop the closing of the form if the textbox doesn't have any value.. is there a way to do that? well of course there are other alternatives for validation but on the FORM i'm dependent on the CANCEL (the x button on the upper right) for the closing of the form and nothing else.
|
|
|
|
|
In the closing event if you set e.Cancel = true; then the form will not close.
My current favourite word is: Nipple!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Just Do This !!!
private void Form1_FormClosing(object sender, FormClosingEventArgs e)<br />
{<br />
if (textBox1.Text == "")<br />
{<br />
MessageBox.Show("Please Fill Details");<br />
e.Cancel = true;<br />
}<br />
else<br />
{<br />
MessageBox.Show("Thanks You !!!");<br />
}<br />
}
|
|
|
|
|
Check out the ErrorProvider[^] class/component.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Hi,
I am trying to change the background color of the listview items depending on their text.
The problem is, the items don't keep their background color after every repaint. I tried the refresh() and update() function to make the items keep their color, but that didn't work.
I don't see any other function or property which might fix this problem i'm having.
I have heard about 'onwerdraw' but i have no idea how to use that, googeling this up hasn't showed a few interesting examples or if it fixes this problem for that matter.
If anyone has a success story about 'onwerdraw' with an example code, i'd really appreciate it. Or a suggestion about how to fix this issue.
Thanks in advance!
|
|
|
|
|
|
Hi,
Is there no alternative than to use ownerdraw?
It would be nice to catch the repaint event and put there the coloring stuff.
|
|
|
|
|
Setting BackColor for each item is another way (which you have used). I made a little test and forcing refresh didn't make the items to loose their color. In what situation you face the problem?
Test was (two buttons on a form and a listview):
private void button1_Click(object sender, EventArgs e) {
ListViewItem lvItem;
for (int i = 0; i < 10; i++) {
lvItem = new ListViewItem();
lvItem.Text = lvItem.GetHashCode().ToString();
lvItem.BackColor = Color.LightBlue;
listView1.Items.Add(lvItem);
}
lvItem = new ListViewItem();
lvItem.Text = lvItem.GetHashCode().ToString();
lvItem.BackColor = Color.Red;
listView1.Items.Add(lvItem);
}
private void button2_Click(object sender, EventArgs e) {
this.Refresh();
}
|
|
|
|
|
Hi,
I'll try to describe the situation.
The items in the listview control can have several states. The states are defined in the 3rd column.
Depending on that status text, i am giving the items a color. They do get the color but as soon as i select an item, the color is gone. And any other event on the listview control is making this happen too.
I am looping through a switch statement and assigning there the back color for the items. After that, i refresh the listview control.
|
|
|
|
|
I tested clicking and refreshing with the code I posted and no problems. Test case was:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
foreach (ListViewItem lvItem in listView1.Items) {
lvItem.BackColor = Color.Purple;
}
listView1.Refresh();
}
If I understood correctly, this would be similar to your case
So I suspect that the problem is somehow in the loop where you define (and possibly reset) the colors for each item.
|
|
|
|
|
Hi,
I forgot to mention that i have to use delegates to access the listview control. I only assign the color at one place.
That shouldn't be the problem right?
|
|
|
|
|
Delegates themselves are not the problem. However if you receive events in a wrong order, that could cause the problem (event to set colors before event to reset colors etc).
Use debugger to confirm that your code is run in desired order. You could also use conditional breakpoints so that whenever a listviewitems background color changes, the debugger breaks. That could give you idea what's going wrong.
|
|
|
|
|
Hi,
The coloring of the items happens in another class. The listview control is passed by ref to that other class.
It shouldn't be a problem if i don't color the items in the Form class right?
The delegates run in the right order.
|
|
|
|
|
No, that isn't a problem as long as you work with the original ListViewItems (i.e. don't make copies).
Did you try to break on refresh or use conditional breakpoints. I believe they would clarify this greatly.
|
|
|
|
|
Hi,
I did both, the colors really get assigned. But they are just not visible. If i minimize the window and bringing it back to front, i can see the colors but disappear really fast again.
[EDIT 12:33h]
Hi,
It seems that i managed to fix this a bit. I created a paint event from the Form.
I do all the coloring stuff in there now. It seems that it keeps the colors, but if i select an item, it loses its color.
If i use the selectedIndexChanged event for the listview control, and i select an item, i lose all the colors on every item.
I need a way to call the Paint event inside the selectedIndexChanged event of the listview control. This might do the trick.
I tried this: this.frmMain_Paint(this.listviewcontrol, null);
But that didn't work...
[/Edit]
modified on Sunday, August 31, 2008 6:33 AM
|
|
|
|