Member Avatar for Ajantis

Hello there!

I have questions about two things:
1.) How do I create a completely new type of files that can only be opened by my programe? What knowledge do I have to have to create something like this?

2.) I am trying to create a search function in a program for training purposes - viewing from MSDN's page. However, I can't make it work, and I've been trying for two days. When a button is clicked, the text in a textBox will searched depending on the text in my comboBox. When a text is found, it should me selected programmatically.

This is how I did:

String cb = comboBox.Text.ToString();


            if (textBox.Text.Contains(cb))
            {
                textBox.SelectionStart = textBox.Text.IndexOf(cb);
                textBox.SelectionLength = textBox.Text.Length;
            }

1. You can register a file extension in the registry, and as long as your program knows how to open it from a command line and you put that in the registry too. It will work (for examples see the plethera already in your registry)

2. At first glace that code looks like it should select from the beginning of the stuff found to the end of the text, so, what isnt it doing?

Member Avatar for Ajantis

1. You can register a file extension in the registry, and as long as your program knows how to open it from a command line and you put that in the registry too. It will work (for examples see the plethera already in your registry)

2. At first glace that code looks like it should select from the beginning of the stuff found to the end of the text, so, what isnt it doing?

Allright, I am not so knowledgeable about registries and how to edit them. Do you know any document I could read about doing this so I can try creating a new file type? :)

That code I post is supposed to search all occurences of a text in a textBox, that is written in the comboBox, and select it. But it does neither. :/

OK for your file association: http://letmegooglethatforyou.com/?q=c%23+set+file+association

As for the code, well the .ToString() is kinda pointless as well .Text returns as string anyway, so, perhaps this means your cb doesnt contain what you thought .. did you look?

Also, the code you show as I said earlier looks like it would find a first occurance of something and highlight to the end? is that what you see? Also remember it would want to be exactly the same so "test" doesnt equal "Test"

Member Avatar for Ajantis

OK for your file association: http://letmegooglethatforyou.com/?q=c%23+set+file+association

As for the code, well the .ToString() is kinda pointless as well .Text returns as string anyway, so, perhaps this means your cb doesnt contain what you thought .. did you look?

Also, the code you show as I said earlier looks like it would find a first occurance of something and highlight to the end? is that what you see? Also remember it would want to be exactly the same so "test" doesnt equal "Test"

Thanks for the link! :) I will be checking it in a moment.

Oh ok! Now I see. Yes it was pretty pointless having .toString().
I added comboBox.Text instead. :)

Well, I was learning this from MSDN, so I tried to do like them. According from what I read, and as far as I understood it, the SelectionStart at indexOf() should start selecting the word I am seeking where it is first found in the textBox. Later, the selection starts from that index, and selects depending on the length of the word.

But - I couldn't make it work. Besides, now that I take a closer look at the code - the SelectionLength would select the whole Text box... due to the:

textBox.SelectionLength = textBox.Text.Length;

If I am not misstaken. But still - I am no closer to the solution. :/
I can't get the selection to start properly...
and I need to know how long the word that I search for is, so that that amount can be selected. I think... what do you say?

Sounds reasonable to me, and finding the length of your search string is the easiest bit.

textBox.SelectionLength = textBox.Text.Length

You are using the total length of the text in the textbox here, try using cb.Length. You can also use the Select method instead : Select(start,length) which is equivalent in what you are trying to do.

Member Avatar for Ajantis

You are using the total length of the text in the textbox here, try using cb.Length. You can also use the Select method instead : Select(start,length) which is equivalent in what you are trying to do.

Riiiiiight! Ahh man - that I missed it! :D Thanks a lot! I can't believe I didn't see that! Haha. Still - everything is learning. :) Each situation.

But I still have one problem - it won't select. Nothing shows up.
Am I missing something out?

if (textBox.Text.Contains(comboBox.Text))
            {
                
textBox.Select(textBox.Text.IndexOf(comboBox.Text),comboBox.Text.Length);
            
            }
commented: we learn all of our life +3

Weird. Maybe a textbox.Focus() might help (I have not tested it)
You said it well :

Still - everything is learning

Member Avatar for Ajantis

Weird. Maybe a textbox.Focus() might help (I have not tested it)
You said it well :

Thanks friend. :)
Hey! What do you know!? :D It actually works! Woohoo! Niiiice!
I wonder what that .Focus() thing does... I'll google it up later on.

Now to the next step... I want the Search function to find all the occurences of the comboBox's text in the textBox, and select them.
The way it looks like now only selects the first occurence....
But perhaps if I use a loop...

I'll go try it out now.

EDIT: Tried it out again. But no change has been made. I should have figured. If I add a loop - the only thing that WILL be repeated is the actual function - so no other text will be selected. :) I wonder how I search through the whole text box and select all the occurences of the text written in the combobox...

First issue, you can only have 1 selection in a text box
Second issue, you need to think about how you would do it as a person..

Member Avatar for Ajantis

First issue, you can only have 1 selection in a text box
Second issue, you need to think about how you would do it as a person..

Hmmm. :/ Allright. It's a pity though.

But maybe I could re-color the background color of the text I want "selected"? :D
If I use some sort of loop - that uses the value of the index of the text occurence I am searching in the textBox and increases it, so that it doesn't keep recoloring the same word all over again... maybe it will work?

But then again, I would need to know how to recolor the background of a certain Text. :/ Or at least color the text at a certain length... the same length as the text in my comboBox. Tricky....

Maybe you could use a RichTextBox instead.

Yep, or, you implement like many things do a "find next" :)

Member Avatar for Ajantis

Niiiiice. Thanks! I tried it out - and it worked similar to the .Select() one I did before. :)

Also - I ALMOSTE made it work! :D
The only thing is - it loops through all the text - and selects only one of the text occurences at a time. It doesn't select all of them. But it loops through them, and I can clearly see it selecting each of the occurences! So far so good! This is how I made now:

for (int index = 0; index < richTextBox.Text.Length; index++ )
                {
                    if (richTextBox.Text.Contains(comboBox.Text))
                    {
richTextBox.Find(comboBox.Text, index, richTextBox.Text.Length, RichTextBoxFinds.None);
richTextBox.Focus();
                    }
                }

I've been thinking this way: the index variable will be the index where the first occurence of the text I am searching for is in the richTextBox. Then, after finding a text, the index variable increases - in order to prevent the loop from selecting the same index again. When the next loop is done, it searches after closest occurence of the text I am searching for. And so on and on and on... Until reaching the end of the string.

So it seems I'm getting closer to solving it. The only thing now I would need to do is figure out how to show all the occurences in the text. And not just loop through them. :)

Try this :(code is not complete, just a sketch)
public RichTextBox txtSel = new RichTextBox();
public int textEnd = txtSel.TextLength;
public int index = 0;
public int lastIndex = txtSel.Text.LastIndexOf(texttofind);

while (index < lastIndex)
{
txtSel.Find(texttofind, index, textEnd, RichTextBoxFinds.None);
txtSel.SelectionBackColor = Color.Green;
index = txtSel.Text.IndexOf(texttofind, index) + 1;
}

Member Avatar for Ajantis

Try this :(code is not complete, just a sketch)
public RichTextBox txtSel = new RichTextBox();
public int textEnd = txtSel.TextLength;
public int index = 0;
public int lastIndex = txtSel.Text.LastIndexOf(texttofind);

while (index < lastIndex)
{
txtSel.Find(texttofind, index, textEnd, RichTextBoxFinds.None);
txtSel.SelectionBackColor = Color.Green;
index = txtSel.Text.IndexOf(texttofind, index) + 1;
}

Just tried it friend. Didn't work at all. I got no selection either, even if I put in .Focus(). :(

Oh well... perhaps I need some additional things to make it work.
I am not sure. :/

Maybe I should just focus on fixing my "almost working" search function. :D
Instead to let it loop through just like that, I would like to let the user klick on the "next" button, and next loop will be executed. :)

Well it was just a suggestion, the best way is indeed to carry on with what you already have.

Member Avatar for Ajantis

Well it was just a suggestion, the best way is indeed to carry on with what you already have.

Oh no! Please don't missunderstand me. :) I really appriciate all your help and I really need it. This is the way I really feel that I learn something! Coding myself a little, posting it, getting feedback on my way of thinking and coding... really. It's top notch. I love it!

But - I've been thinking to put the search function in a little project of mine. And I do have a deadline to think of - so, I can't stand in any place for too long - even if I wanted to (and believe me, I'm really stubborn about learning as much as I can! :D As a matter of fact - teachers are commenting that, that I should lay low a little with it. LoL) So - I need to balance it with the time I have to do my little project.

Anyway - here is something new I tried:

int index = 0;

            //ANOTHER TRY ;)
            do
            {
                for (int index; index < richTextBox.Text.Length; index++)
                {
                    if (richTextBox.Text.Contains(comboBox.Text))
                    {
                        richTextBox.Find(comboBox.Text, index, richTextBox.Text.Length, RichTextBoxFinds.None);
                        richTextBox.Focus();
                        break;

                    }
                }
            } while (nextButton.MouseClick);

Here, I've been thinking that the user performs a search with the searchButton, and then "steps through" the richTextBox for occurences of that text , when clicking on the nextButton. :)
The index variable is a class variable, and will be increased in the loop, so when the first occurence of the text is found - the loop breaks - and if you press nextButton - you will continue the loop!
At least - that's my idea. How does that sound?

Also - I keep getting an error message - that MouseClick event should have a += or -= in the statement or something...
I don't really understand. All I want it to do is to redo the code when the user clicks on the nextButton.

err yes, you cant do that :P
MouseClick is an event thats triggered not a property returning a boolean.

Once a control has the focus it will always have it, you don't have to put it in a loop. Having focus means that if you have 3 textboxes T1, T2 and T3 on a form and T3 has the focus, whenever you type the keyboard, you will see the text in T3 change, not in T2 or T1.

Member Avatar for Ajantis

err yes, you cant do that :P
MouseClick is an event thats triggered not a property returning a boolean.

Ahhh - I have it nearly working now. The only thing that doesn't work now is the "Previous Search" function. I've been working on this for 2 weeks now and I can't get it right. Guys, I really need your help on this one. Seriously... it started as a nice project, but now it really peeves me off....

Here's my coding:
http://pastebin.com/m145972ec

public int textAmount = 0;
must be public const int textAmount = "somevalue";

//public int[] indexStorage = new int[textAmount];
public int[] indexStorage = null;
is very bad, use what you have in comment here, but use it in a method not on class level.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.