|
Hi Guys,
only a short question. Is there a way to scan a Text in a Textbox and Compare it with a text in a file, or even parts of a other textbox text? Would be great if anybody has a solution in source or hints.
Thanks all...
Eolus
If you want realize your dreams, you should not sleep!
|
|
|
|
|
if (textBox1.Text == textBox2.Text)
MessageBox.Show("They match!"); If you want to compare it to a file, use a TextReader to read each line and use line.IndexOf(textBox1.Text); to determine if the line contains the text in textBox1 .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
sorry but i must ask you again, the first time i wrote it not clear enough.
I must compare text in a Textbox with a textfile and if there is a match, it should copy the code lying behind.
The first thing is something like that: <FHGO> (but it's not HTML-tags),
the second thing are normal letters: A B C and so on
Hope it's little clearer to you
Eolus
Sorry for my bad english...
If you want realize your dreams, you shouldn't sleep!
|
|
|
|
|
From what I can see, I think Heath's suggestions that you read the file line by line using the TextReader.ReadLine method and then calling the IndexOf method on the returned string is the way to go.
It will get you the location of the begining of the text in your text box on the current line of the file. Then you just need to break up the rest of the string to get what you need.
I'd need to know more about the file structure and what you'll be searching for to make a more concrete suggestion.
Bill
|
|
|
|
|
That's what I was going to suggest. Thanks!
Regarding this last statement - and directed to the original poster - if your text spans multiple lines, then you must take that into account either by removing line breaks when reading strings into your application from the file, or use a Regex class/statement that spans multiple lines (regular expressions - very powerful; get to know them). Regex's are an advanced string parsing algorithm.
See the Regex class in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Okay i give you an example:
Text in a Textbox:
<FHGO><BIG>Hello World!
Textfile:
begin raw_codes
name <FHGO>
8561 4107 609 440 601 439
600 438 604 439 600 437
600 440 597 436 602 435
600 4120 617 435 603 439
598 1478 613 1478 608 1478
615 435 600 436 603 436
600
name <BIG>
8562 4107 608 443 594 443
600 441 602 444 593 443
596 443 601 445 594 443
601 4120 609 1480 608 1478
611 1479 609 1479 609 1478
608 442 598 441 600 443
598
name A
8528 4136 579 448 579 450
580 449 580 448 578 450
579 449 579 448 578 447
579 4149 579 447 581 1503
581 448 578 1505 580 448
580 449 578 450 579 450
578
...
So i need to scan the textbox und compare it with the textfile and get the code behind back. I think a Problem would be probably that "A" is also in Name. I hope there is a solution to solve my Problems
Eolus
If you want realize your dreams, you shouldn't sleep!
|
|
|
|
|
Yeah, and we gave it to you. Scan lines like we told you until you find a string match. Then keep reading lines in using TextReader.ReadLine until you encounter (apparently) an empty line. This is really a simple problem.
If you think your TextBox content is too ambiquous, then use "name " + textBox1.Text . Keep reading lines from the text file until you find it, then follow what I said above to read the rest of the lines until you encounter a blank line.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
So the textbox can contain an arbitrary number of <name>'s? For example: one time I could enter '<FHGO>SomeText', next time '<FGHO><A>SomeOtherText', third try '<FGHO><A><BIG>SomeThingElse' and you want to get 1, 2 or 3 (or N) code blocks?
If so, you need to two two things:
1) parse up your textbox string into individual <name>'s. Here the Regex class Heath refered to is an IDEAL tool.
2) for each <name> you get, search the file. You'll want to do write a method something like:
<code>
private string strFindCode(strName){
TextReader trCodeFile;
//open the text file here....
bool bFoundIt=false;
string strCurrentLine;
string strCodeBlock="";
while(trCodeFile.Peek()>-1 && !bFoundIt){
strCurrentLine = trCodeFile.ReadLine();
if (strCurrentLine.IndexOf(strName)>-1){
bFoundIt=true;
strCurrentLine = trCodeFile.ReadLine();
while (strCurrentLine != ""){
strCodeBlock+=strCurrentLine +"\n";
strCurrentLine = trCodeFile.ReadLine();
}
}
}
trCodeFile.Close();
if (bFoundIt){
return(strCodeBlock);
}else{
return("Name Not Found");
}
</code>
I have not compiled, much less run, this code. So I make no promises...but hopefully it points you in the right direction algorithmicly.
Bill
ps: please don't everyone jump on me for not using the StringBuilder...
|
|
|
|
|
Hi all,
I have a TextBox control that only takes in numbers, the final *bug* i have to solve is when the user right clicks on the control and the popup menu appears with undo, cut, copy, paste, etc on it.
What event can i use to deal with when they select paste?
I need to solve this to stop the user pasting text into the control that isnt numbers.
This isnt a context menu i have attached to the control so the ContextMenuChanged event doesnt fire, which is the event i thought i should be using.
Anyone got any ideas where i'm going wrong?
Thanks
|
|
|
|
|
If you've extended TextBox with your own, override OnValidating ; otherwise, handle the Validating event. This allows you to cancel the operation if anything but numbers are pasted and will restore the previous text (since Validating fires before the Text is set). See the documentation for the Validating event to see an order of events. The only problem with this method is that the control must loose focus first.
If that's not sufficient, then extend TextBox (if you haven't already) and override WndProc . Handle the WM_PASTE notification message (0x0302) and throw it out (i.e., don't call base.WndProc ) if the Clipboard text doesn't contain numbers. You could handle this a few other ways, but hopefully this will get you started.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks!!!
The override of WndProc is just what i needed, i'll get on with it that way.
I really must remember WndProc when i get stuck.
|
|
|
|
|
Hi,
When we generally click the cross on the caption bar, our application (or our Form) will close and it is automatic action.
I'd like close my application when I'll click the cross on the caption bar and I'll confirm this action. When I'll not confirm it, application shouldn't be closing.
I try use the Closing event, but I don't know how interrupt closing app.
Regards
Kamil
|
|
|
|
|
Hi,
If you override the forms "Closing" event then have some code like below this should help you out.
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Do you wish to close?",
"AppName",
MessageBoxButtons.YesNo) == DialogResult.No)
e.Cancel = true;
}
By changing e.Cancel to true the event will not complete and your form will not close.
Hope this helps
Simon Wren
simon.wren@nesltd.co.uk
Senior Software Architect
National Energy Services Ltd
Visit Us: www.nesltd.co.uk or: www.nher.co.uk
|
|
|
|
|
Except this isn't overriding, this is handling the Closing event. Since it's typical to extend the Form class with your own (VS.NET does this for new Windows Forms application proejcts), you override the event handler, OnClosing , which is better (faster, more control) than handling the event in a derived class:
protected override void OnClosing(CancelEventArgs e)
{
DialogResult result = MessageBox.Show("Do you want to close?", Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
e.Cancel = true;
base.OnClosing(e);
} That's overriding. In this case, you'll still want to call the base class's handler, but in some cases (like when you want to throw messages out in WndProc ), you don't want to call the base class's method.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, i did get my use of the word override wrong in my first response
Out of interest you mention that using OnClosing is faster, why is it?
|
|
|
|
|
When you handle an event, you actually add a reference - a managed pointer - to a list of handlers. That reference must be added to a list - which may have to be resized, which takes time. Even adding the referenec to that list is an O(n) operation, where n is the number of list items currently in the list.
When the event is fired, the list of references to delegates is enumerated and each delegate is invoked. The EventArgs derivative may need to be "queried" in between each invocation to keep track of whether or not something needs cancelled (in this case).
That's all quite a bit more than simply a virtual call (callvirt IL instruction), which is polymorphic.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Look in the EventArgs object.
You have to do something like
e.Cancel = true;
However I do not remember the EXACT property name so search for it.
______________________________
The Tao gave birth to machine language. Machine language gave birth to the assembler.
The assembler gave birth to the compiler. Now there are ten thousand languages.
Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao.
But do not program in COBOL if you can avoid it.
|
|
|
|
|
Your Closing event will have a parameter of type CancelEventArgs which has a property called Cancel . Set the property Cancel to true and the application won't exit.
This gives you a chance to pop up a message box asking "Are you sure?" and if the user selects "no" then you can cancel the event. If the user selectes "yes" then you need do nothing more - the application will be closed.
Does this help?
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
Thanks!!!
e.Cancel = true;
It is exactly what I need.
Thank you for fast answer.
Regards
Kamil
|
|
|
|
|
I'm not sure how to do it!
help pls
Cata
|
|
|
|
|
See the StringFormat class. OR StringFormatFlags.NoWrap with StringFormat.FormatFlags and pass the StringFormat instance to your Graphics.DrawString call.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi ,
i have a mesh model and i put it in many places in my app. using the world transformation and i want to detect which mesh is the pointed one but when i point to mesh it only detects the last mesh has been transformed .
|
|
|
|
|
What do you mean by "the pointed one"? This is a little too non-technical to answer.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i mean by it the one which the mouse points to it
|
|
|
|
|
You can get the mouse position at any time using the static Control.MousePosition property. You could transform these coordinates to your world space and get the mesh you need, or if you click on it you can get the client coordinates and transform these.
When you say you are getting the last mesh that was rendered, do you mean that you're destroying and re-creating your mesh with each drawing phase, and that you get the last one rendered? You probably don't need to destroy your mesh each time, though you would have to get the materials and textures from it to draw.
I'm sorry if I don't understand your problem.
Microsoft MVP, Visual C#
My Articles
|
|
|
|