|
Hi,
Im trying to set the font in my Richtextbox by some buttons.
If I set some Text to bold, it works. then i want to reset the style to regular (for the whole Control). The first time it works, but any next time it fails. nothing changes.
background:
The words are set to bold by selecting text and then set the font for selected text in code.
Then at some point i want to reset it. But this just works once.
I tried it with substracting the bold style, by just overriding it. nothing works.
is there any way to reset the fontstyles to regular for ALL text, without selecting all and then set the font.
I really hope you can help me with this!
thank you!!
moon44
|
|
|
|
|
You can try with RichTextBox.ResetFont(), perhaps it works. I don't know any other method.
I think the problem cames from the RTF format: all the 'styles' for the text are set by tags similar (regarding the concept, not the syntax) to the HTML's tags. when you select a word, maybe the control DO NOT select correctly the tags... but this is only a supposition.
Try to print the RichTextBox.Rtf property, that reports all the RTF code.
|
|
|
|
|
hi,
thank you for your answer. this could explain the behavior. the *global* font tag just wraps around and does not override the *inner* tags.
the resetfont seems to do something, but i can't tell what exactly. WHAT is the default font value? it seems like thats not defined.
thank you for any additional hints!
|
|
|
|
|
The default font value is the one you have set in design-mode (or the automatically set value, MS Reference Sans Serif or so)
Sorry but I don't know more...
|
|
|
|
|
Hi all,
here is the real code I am using for with improved code and conditions but I am running also into the bracket.
but the last contition is false BAKER is not Meyers and so the I should never run into the bracket (MessageBox ..)
string a = "MR";
string b = "MRS";
string c = "BAKER";
string d = "Meyers";
if
(
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))) ||
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR"))) &&
(string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper()))
)
{
MessageBox.Show("ERROR");
}
where is the fault ?!?!?
please help
Thanks Frank
|
|
|
|
|
|
Let me break your statement into three parts:
bool a = (string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR"))
&& (string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"));
bool b = (string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))
&& (string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR"));
bool c = string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper());
Combined it would look like this:
if (a || b && c) {
MessageBox.Show("ERROR");
}
What the compiler makes out of this statement is this:
if (a || (b && c)) {
MessageBox.Show("ERROR");
}
Thats because the logical AND has a higher priority than the logical OR. Its like this simple numeric example:
3 + 2 * 4 = 3 + (2 * 4)
In conclusion the statement will be true if a is true no matter what the other subparts are.
|
|
|
|
|
Thank you for your answer,
but I think I did:
if ( ( a || b) && c ) {
MessagBox.Show("ERROR")
}
Please look at the (many) brackets - therefor I placed the brackets around the or-expression to force the compiler to check the or-term fist and this result with the &&-expression !
And that's what's going wrong
Frank
|
|
|
|
|
Yes, you thought that you did, but you failed.
Instead of ((a || b) && c) you created ((a) || (b) && c).
string a = "MR";<br />
string b = "MRS";<br />
string c = "BAKER";<br />
string d = "Meyers"; <br />
<br />
a = a.Trim().ToUpper();<br />
b = b.Trim().ToUpper();<br />
c = c.Trim().ToUpper();<br />
d = d.Trim().ToUpper();<br />
<br />
if (((a == "MR" && b == "MRS") || (a == "MRS" && b == "MR")) && c == d) {<br />
MessageBox.Show("ERROR");<br />
}
---
b { font-weight: normal; }
|
|
|
|
|
no you did not. It will result in
if
(
(1) || ( 1 ) && (0)
)
{
MessageBox.Show("ERROR");
}
Must be this:
if
(
(((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))) ||
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR")))) &&
(string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper()))
)
{
MessageBox.Show("ERROR");
}
will result in
if
(
((1) || ( 1 )) && (0)
)
{
MessageBox.Show("ERROR");
} and will not display message box.
But still, it's a mess.
David
|
|
|
|
|
May I suggest you little simplification?
a.TrimEnd().TrimStart().ToString().ToUpper() -> a.Trim().ToUpper()
David
|
|
|
|
|
Thank's but that doesn't help in my problem !
regards
Frank
|
|
|
|
|
fracalifa wrote:
that doesn't help in my problem !
Pardon, but it does. Low readability (daily-wtf calibre!) of your code is what caused problem here.
ok, it is prevence but not solution, that's why I say it is OT (off topic).
David
|
|
|
|
|
|
Hi all,
coming from "C" I'm a little bit confused on the result of the following boolean term.
I think this can never become TRUE but it does - why ?
if( ((("A"=="A") && ("B"=="B")) || (("A"=="B") && ("B"=="A"))) && (true==false) )
{
MessageBox.Show("Error"); // this code is not reachable ?????
}
A and B is really a comparison of strings
reduced to my understanding is that the results shoud be
if( (((TRUE)&&(TRUE)) ||((FALSE && FALSE))) && (FALSE) ) // should ever be FALSE
........\---------/........\----------/.........|
........... TRUE......||.......FALSE.......&& FALSE
.............\------------------/...............|
.....................TRUE..................&& FALSE = FALSE //!!
the second problem is that the code in the brackets is not reachable ????
the count of backets is correct !
that's leave my understanding of C#
where is my error in thinking ?
Thanks for your help
Frank
|
|
|
|
|
In c# the == operator has been redefined for strings, so when you compare two strings written like "abc" the compiler is able to compare them. Using variables, you have to use the string.Equals(...) or the string.CompareTo(...) methods to have reliable results. In VB.NET you can use the = operator also for string variables.
|
|
|
|
|
Have you actually tried this code? The fact that the compiler tells you thats not reachable definitely means that it will never get executed. Ive tried the code and the messagebox is not showing.
|
|
|
|
|
That's one of the two problems,
I reduced two brackets - that it runs
but I am trying to implement the first suggestion and than .... ?
|
|
|
|
|
The first term (("A"=="A") && ("B"=="B")) is true, thus the whole OR-expression || will become true.
Because the first term is always true, there is no need for the compiler to evaluate the second term.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
... yes but ..
I put the OR-expression into brackets followed by an AND-expression so the compiler has to check for
( 1 || 0 ) && ( 0 ) so there is a need to do this - because the last term is not true !
regards
|
|
|
|
|
Oh sorry, I misread the brackets.
Well, because the last term is always false, the compiler detects that the whole clause never can become true. The or-clause becomes always true, bug then the and-clause will become false anyway.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi Greeeg,
please look to following code I have implemented:
Please try this code:
string a = "MR";
string b = "MRS";
string c = "BAKER";
string d = "Meyers";
if
(
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))) ||
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR"))) &&
(string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper()))
)
{
MessageBox.Show("ERROR");
}
The result of the first 4 lines of the if-expression becomes TRUE -- o.k.
but the comparison of c.Trim and d.Trim is FALSE so the whole expression (due to the &&) has to become false but the code is running into the MessageBox bracket
why
thank you for your help
Frank
|
|
|
|
|
this is strange indeed, looks like the && operator is evaluated BEFORE the || operator - I thought they are handled equally.
If you want the || clause to be evaluated before, better put brackets around the first two expressions.
This is a test I made:
string a = "MR";
string b = "MRS";
string c = "BAKER";
string d = "Meyers";
bool b1,b2,b3,b4,b5;
b1 = string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR");
b2 = string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS");
b3 = string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS");
b4 = string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR");
b5 = string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(), d.TrimEnd().TrimStart().ToString().ToUpper());
bool b6, b7;
b6 = (((b1 && b2) || (b3 && b4)) && b5);
b7 = ((b1 && b2) || ((b3 && b4) && b5));
if(b6)
{
MessageBox.Show("ERROR");
}
regards
/edit:
This supports my theory:
Operator precedence and associativity
modified 12-Sep-18 21:01pm.
|
|
|
|
|
They have different precendence.
A lot of things have in C#...
The PROgrammer Niklas Ulvinge aka IDK
|
|
|
|
|
Hi,
I need to find the relation between the windows user(username with which he logged ) ,his session id and the process that were started by him.All these informations are available in the task manager but how do i get them through the code.
Does c# directly have any procedures to find this or if there are any other API's.Please let me know.
Thanks
|
|
|
|