Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Lock a textbox and richtextbox together

Lock a textbox and richtextbox together

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simplicitylabs
    wrote on last edited by
    #1

    If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?

    C A M 3 Replies Last reply
    0
    • S simplicitylabs

      If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      What have you already done ? It seems to me that there's no 'lock' property, you need to write the code to do it, so you need to post the code you've written so we can see what's wrong with it

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      S 1 Reply Last reply
      0
      • C Christian Graus

        What have you already done ? It seems to me that there's no 'lock' property, you need to write the code to do it, so you need to post the code you've written so we can see what's wrong with it

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        S Offline
        S Offline
        simplicitylabs
        wrote on last edited by
        #3

        I haven't written any code for "locking" the two together. I have searched for relevant code snippets and ideas to do this, but haven't found any. I'm not asking anyone to write it for me -- I can't learn this stuff if every time I need help someone just does it for me. I just need some pointers. Thanks...

        C 1 Reply Last reply
        0
        • S simplicitylabs

          I haven't written any code for "locking" the two together. I have searched for relevant code snippets and ideas to do this, but haven't found any. I'm not asking anyone to write it for me -- I can't learn this stuff if every time I need help someone just does it for me. I just need some pointers. Thanks...

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          "If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak." How is this working then ? Or did I misunderstand and this is a description of what you want, not what you have ?

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          S 1 Reply Last reply
          0
          • C Christian Graus

            "If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak." How is this working then ? Or did I misunderstand and this is a description of what you want, not what you have ?

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            S Offline
            S Offline
            simplicitylabs
            wrote on last edited by
            #5

            Ah, ok...I misunderstood what you were asking. See the code below that creates the line numbers in textbox1: Here's the code that does the work:

                private void updateNumberList()
                {
                    Point pos = new Point(0, 0);
                    int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
                    int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);
            
                    pos.X = ClientRectangle.Width;
                    pos.Y = ClientRectangle.Height;
                    int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
                    int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);
            
                    pos = textBox1.GetPositionFromCharIndex(lastIndex);
            
                    textBox1.Text = "";
                    for (int i = firstLine; i <= lastLine; i++)
                    {
                        textBox1.Text += i + 1 + " \\r\\n";
                    }
                }
            

            Here's the code that calls it:

                private void richTextBox1\_TextChanged(object sender, EventArgs e)
                {
                    updateNumberList();
                }
            
            C 1 Reply Last reply
            0
            • S simplicitylabs

              Ah, ok...I misunderstood what you were asking. See the code below that creates the line numbers in textbox1: Here's the code that does the work:

                  private void updateNumberList()
                  {
                      Point pos = new Point(0, 0);
                      int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
                      int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);
              
                      pos.X = ClientRectangle.Width;
                      pos.Y = ClientRectangle.Height;
                      int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
                      int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);
              
                      pos = textBox1.GetPositionFromCharIndex(lastIndex);
              
                      textBox1.Text = "";
                      for (int i = firstLine; i <= lastLine; i++)
                      {
                          textBox1.Text += i + 1 + " \\r\\n";
                      }
                  }
              

              Here's the code that calls it:

                  private void richTextBox1\_TextChanged(object sender, EventArgs e)
                  {
                      updateNumberList();
                  }
              
              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              OK - the problem is probably to do with GetCharIndexFromPosition. I don't see why you're doing that. You should be able to get the index from the cursor position, which is retrieved from the selection start property. If you step through the code when you step back, you should see which line is not giving the value you'd hope, but I do think that's the way to get the correct index, all the time.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              1 Reply Last reply
              0
              • S simplicitylabs

                If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?

                A Offline
                A Offline
                Arun Immanuel
                wrote on last edited by
                #7

                Instead of scrolling the textbox, find the number of the first visible line in the richtextbox.Say if it is 15 and the text box can show only 'n' numbers, textBox1.text=""; for i=15 to 15+n textBox1.Text+=i+"\n";

                Regards, Arun Kumar.A

                1 Reply Last reply
                0
                • S simplicitylabs

                  If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?

                  M Offline
                  M Offline
                  mav northwind
                  wrote on last edited by
                  #8

                  Hi! I'm not sure if I understand correctly what you try to achieve, but there a quite useful article about line numbers for the RichTextBox[^] here on CP.

                  Regards, mav -- Black holes are the places where God divided by 0...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups