Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone!

I have a RichTextBox wich can be used for calculations in my program.
Now I want to Add a Listbox which should display shortcuts from the shortcut[](sin, cos, tan,log,ln.....) array.
Actually the same as Visual Studio, it helps you to find variable names/methods/funktions etc

I have a solution with an KeyEvent, but i think its a bad(confusing) solution..
I read something about DataBindings, could be this the right way?
If it is, i need a good explanation or an example, please!

Thanks
Posted
Comments
Jason Gleim 22-Jul-13 14:18pm    
Do you want the user to pick something from the listbox and have that function appear in the richtextbox? If so, data binding is not a good solution unless you know that the user will only ever pick a single function and it will always appear at the same place in the rich text box.

Data bindings allow you to have a backing variable in the data context of the control that is automatically updated on the UI. Meaning if you bind the class property MyCheckMark to the Checked property of a check box control, then when you set MyCheckMark = true; in code, the checkbox will change to be checked. (assuming the property and the data context class implement INotifyPropertyChanged)

It would seem to me that you want flexibility... the user should be able to select multiple functions and have them inserted at the current cursor position. There would be value binding the combobox to a property on a ViewModel but I'm guessing if you are asking about databinding, you aren't doing MVVM anyway. In that case I would say, no... data binding is not a good solution for you.
SubbZer 22-Jul-13 14:59pm    
1st: fexibility sounds good!
2nd: thats exactly what i want - "the user should be able to select multiple functions and have them inserted at the current cursor position"

thats what my private void Calc_PreviewKeyDown(object sender, KeyEventArgs)-event does, the user can choose the function and the code insered them at the current position
however, it makes the program much slower.. (many declarations of keys what should'nt be pressed..)

ONE of the next problems is that i also can write normal text in the textbox wich also should be observed because the text can contain the shortcut names, then it should'nt be selectable
Jason Gleim 22-Jul-13 15:17pm    
You should only insert the function when the user picks it from the combobox... right? So hook the SelectionChanging and SelectionCompleted events. When the user drops the box down, the selection changing event will fire. Set a flag so you know the user dropped the box down. In the selection completed event, check the flag and if set, insert the function into the text runs collection of the rich text box at the current cursor position.

Also, you don't need to process all the keypresses that can't be pressed... why would you do that? Just check for the ones you are interested in intercepting. Make sure you set the Handled property to true so the keypress doesn't bubble up the event chain. For keys you aren't interested in, just do nothing. They will bubble up and be handled by something else.
CHill60 22-Jul-13 18:08pm    
OP likes this - paraphrase your comments and post as the solution!
SubbZer 22-Jul-13 16:12pm    
Thanks! This was the answer what i was searching for.
I think this would work. Thank you

1 solution

You should only insert the function when the user picks it from the combobox... right? So hook the SelectionChanging and SelectionCompleted events. When the user drops the box down, the selection changing event will fire. Set a flag so you know the user dropped the box down. In the selection completed event, check the flag and if set, insert the function into the text runs collection of the rich text box at the current cursor position.

Also, you don't need to process all the keypresses that can't be pressed... why would you do that? Just check for the ones you are interested in intercepting. Make sure you set the Handled property to true so the keypress doesn't bubble up the event chain. For keys you aren't interested in, just do nothing. They will bubble up and be handled by something else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900