3

I am trying to create a packet analyzer for an online game using C# and I am new to c#.

I have 2 RichTextBoxes, 1 shows the packet in bytes and the other one shows the packet in ANSI.

Here is what I want to achieve:

  1. When I select(highlight) data in the byte text box, I want the corresponding data in the ANSI text box to also be highlighted. (and vice-versa)

  2. When I change data in the 1 of the textboxes, I want the corresponding data in the other textbox to also be changed.

How do I do these?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0x64
  • 124
  • 8

1 Answers1

1

You would normally do this kind of thing based on an event like onHighlightedTextChanged but because such an event doesn't exist its a much easier solution to have the following code in a timer:

textBox2.Focus();
textBox2.SelectionStart = textBox1.SelectionStart;
textBox2.SelectionLength = textBox1.SelectionLength;

With this code updating every 10ms (or whatever you set it too) it will appear to be highlighting the text dynamically.

For the change of data in one textbox to another you can use the event TextChanged with the following code:

textbox2.Text = ByteToAscii(textbox1.Text)

Where ByteToAscii is your own function

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
Shane
  • 119
  • 1
  • 10
  • Interesting solution.. But would it not be possible to achieve the same thing using some other event(s?) like OnFocus or/and OnFocusLost? @user1907736: Is it vital that the change happens imediately? – Kjartan Dec 17 '12 at 20:00
  • You could probably do it with a combination of `OnMouseUp` (for selections via mouse) and `OnKeyUp` (for selections via keyboard) – ean5533 Dec 18 '12 at 04:59