|
i need to check that the content in a textbox is a 6-digit hexadecimal number (or rather, an HTML color code).
How do I do this?
|
|
|
|
|
You could use a regular expression.
There's a bunch of regular expression classes in .net (Start here[^]
If you don't know about regular expressions, you might want to read up a bit on them. There's a site here[^] that's worth looking at.
It's also worth having a tool to test them with before you start coding them. A popular one is Expresso[^].
Simon
|
|
|
|
|
use this function...
bool ValidateColor(string s)
{
if(s.Length != 6)
return false;
for(int i = 0; i < 6; i++)
{
int c = (int)s[i];
if(!(c > 47 && c < 58) || !(c > 64 && c < 71))
return false;
}
return true;
}
..should do the job
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
That is terrible. I see magic constants, a major bug (|| instead of &&) and no acceptance of lower-case hex.
I would suggest:
public static bool ValidateColor(string s, out int value) {
return s!=null && s.Length==6 && int.TryParse(s, out value, NumberStyles.HexNumber);
}
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Yeah, my bad... I wanted to use the parse method but couldn't remember the param for the the NumberStyles bit...
Plus I knew you be around to set me straight
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I want to do the program in c# which is form based,
This is what my program have to do,
It should monitor for the internet explorer, when it is opened it should show a message box. and whenever the url changes in it should be displayed in message box. The url here changed by user.
I dont know how to add beforenaviagte2 event to each ie window,
Where there is a will there is a way
-MS
|
|
|
|
|
No replies...??
Where there is a will there is a way
-MS
|
|
|
|
|
|
Article
Internet Explorer Activity Monitor[^]
The demo project worked fine which contains an exe.
WHen I am opening source code project in my visual studio 2008..After Conversion process, I run it..But it not worked for me.
Please help me to fix this. I am new to c#.
Where there is a will there is a way
-MS
|
|
|
|
|
I have a number of functions that, for one reason or another, return data type object.
If I try and use the + operand (or indeed any arithmetic operator) on types object the compiler throws a wobbly.
How do I tell if a given object supports the "+" operator and, if it does, invoke that "+" operator?
|
|
|
|
|
Hi,
I don't think returning a general object is a good choice.
Have you considered returning a small class or struct holding both the value and the type of the result?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Unfortunately it is for evaluating an expression tree so we don't know until we are running what an expression will evaluate to.
I have found that the compiuler turns operator + to a method op_Addition so i guess I need to do a GetMethod("op_Addition")?
|
|
|
|
|
I can't answer that, I don't know enough of your problem domain.
I would refrain from relying on undocumented but observed compiler behavior whenever possible.
I do know when I create an interpreter/parser-like system, I use small data structures to describe what I am dealing with, i.e. something holding name, value, type, etc.
Then when some operator needs executed, its code can check its operands' types and act accordingly.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Nope, that would require an interface. MS could probably write generic interfaces to do that, but I don't think they'd apply them to the built-in types retroactively.
P.S. You might want to see what I did here[^].
|
|
|
|
|
Hi,
I have a problem to get single numeric values from an Excel worksheet (that I need to process as I receive it). I use the Microsoft Excel 9.0 Library because I need to process EXcel 2000/2003 files.
This is the range:
Excel.Range CellRange = (Excel.Range) WS.Cells[RowNum, ColNum];
I use this statement to check format and cell.
MessageBox.Show(CellRange.NumberFormat.ToString() + "\n" + RowNum.ToString() + "\n" + ColNum.ToString());
Only in case of format 0.0 I can get a value (e.g. 234.9) like this:
Result = Convert.ToDouble(CellRange.Value);
MessageBox.Show(Result.ToString());
But the format can also be General with values e.g. 234.9 (with point), 234,9 (with comma) or 2349 (without separator).
It must be simple, but I can't find the solution.
Who can help me?
Thanks,
Gijs
|
|
|
|
|
Convert.ToDouble("34.13", CultureInfo.InvariantCulture);
There's also a CurrentCulture
I are Troll
|
|
|
|
|
Yes, the CultureInfo helps in certain cases.
But I don't manage to get a value (string or numeric) from a cell with type 'General'.
The single cell range 'Value' is null, although the Excel cell contains a few characters.
Thanks,
Gijs
|
|
|
|
|
Hello,
I am writing a server/client application and I have encountered a weird problem. The server handles several TCP connections and sometimes sends messages to the clients. Now, if I send these messages too "fast" they are not sent at all. However, if a Thread.Sleep(1000); command is introduced before sending a message everything works fine (except it is very slow). Maybe it is relevant that the Send() method is called by different threads , triggered by events (locking the communication object does not help). Here is the code:
public void Send(int id, string message)
{
StreamWriter writer = new StreamWriter(connections_[id].GetStream());
writer.WriteLine(message);
writer.Flush();
}
|
|
|
|
|
What is your receive code, perhaps it is incorrect.
Personally I always use the sockets code instead of writing to the streams directly and it always seems to work.
|
|
|
|
|
This is my receive code. It works fine until the calls are getting crowded:
public string Receive(int id)
{
StreamReader reader = new StreamReader(connections_[id].GetStream());
string result = reader.ReadLine();
return ProcessQuery(result);
}
|
|
|
|
|
I see your problem. Eliminate your List of connections from both the client and the server, pass the id in the stream, and use one connection for all of the communications. It will work just fine.
ie:
RecieveThread(){
BinaryReader br = new BinaryReader(stream);
while(go){
int messageId = br .ReadInt32();
sring message = br .ReadString();
}
}
This is untested psuedo code just to give you a starting point.
|
|
|
|
|
|
Hi,
IMO Flush() is the wrong method to call. You need Close() and Dispose(), not Flush().
A "using" statement would fit well.
And Ennis is right, why not use Socket class?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I cannot close my conenction because I will need it later...
Perhaps I try the Socket class.
|
|
|
|
|
I didn't say anything about a connection, I was yalking about your writer, the one you create a new of every time, and never Close nor Dispose. And don't tell me you have shown fake code, I hate that, one should show real code to get real solutions.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|