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. Deleting characters from a list<char>

Deleting characters from a list<char>

Scheduled Pinned Locked Moved C#
data-structureshelptutorialquestion
9 Posts 4 Posters 3 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.
  • M Offline
    M Offline
    mjcs100
    wrote on last edited by
    #1

    I have a program I am working on. First I will explain what I have done and then get to the question. I have created a undo and redo functions that when undo, it deletes one character at a time from the string and puts it in a 'List'. Then when you click redo, it goes to the 'List' and adds each character back to the string and deletes it from the list, one character at a time. You can see in the pictures the undo and redo menu button click events for them in the 1st pic. They get their functions from the 2nd pic., the undo redo class. I also have a keydown and text change function shown in pic. 3. All functions work good but the clearing of the 'list' that remains if they don't 'redo' all commands. I can undo and redo and have no problems, but I want it to delete the list contents after the user starts to type again after undoing and redoing. I have tried to put it in the text change but it keeps firing every time the list is over '0' and after the menu redo is enabled. I have tried putting in the buttons function, and I even tried to do the stack process, but I could not get my head wrapped around that option. I know that 'richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);' deletes 1 character, but how do you say if the 'text.length +1' from where you stop to undo and redo, how to add a character. I hope I am explaining this fine for you to understand and that someone can possibly help me with this part, even if it is not adding 1 to the text.length. Thank you.

    private void menuUndoEdit_Click(object sender, EventArgs e)
    {
    menuUndoEditClicked = true;

            //accesses the undo function to undo last command
            UndoRedo.undo();
    
            if (richTxtDirections.Text.Length > 0)
            {
                //removes the last character in the text in the textbox
                richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);
    
                //positions the cursor at the end of the text
                richTxtDirections.Select(richTxtDirections.Text.Length, 0);
            }
    
            //condition for menu redo edit button is enabled
            if (menuRedoEdit.Enabled == false)
            {
                //if the keys (control and Z) are pressed enables the redo button...
                menuRedoEdit.Enabled = true;
                //sets the font to specified font and style to regular....
                menuRedoEdit.Font = new Font(menu
    
    L L J 4 Replies Last reply
    0
    • M mjcs100

      I have a program I am working on. First I will explain what I have done and then get to the question. I have created a undo and redo functions that when undo, it deletes one character at a time from the string and puts it in a 'List'. Then when you click redo, it goes to the 'List' and adds each character back to the string and deletes it from the list, one character at a time. You can see in the pictures the undo and redo menu button click events for them in the 1st pic. They get their functions from the 2nd pic., the undo redo class. I also have a keydown and text change function shown in pic. 3. All functions work good but the clearing of the 'list' that remains if they don't 'redo' all commands. I can undo and redo and have no problems, but I want it to delete the list contents after the user starts to type again after undoing and redoing. I have tried to put it in the text change but it keeps firing every time the list is over '0' and after the menu redo is enabled. I have tried putting in the buttons function, and I even tried to do the stack process, but I could not get my head wrapped around that option. I know that 'richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);' deletes 1 character, but how do you say if the 'text.length +1' from where you stop to undo and redo, how to add a character. I hope I am explaining this fine for you to understand and that someone can possibly help me with this part, even if it is not adding 1 to the text.length. Thank you.

      private void menuUndoEdit_Click(object sender, EventArgs e)
      {
      menuUndoEditClicked = true;

              //accesses the undo function to undo last command
              UndoRedo.undo();
      
              if (richTxtDirections.Text.Length > 0)
              {
                  //removes the last character in the text in the textbox
                  richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);
      
                  //positions the cursor at the end of the text
                  richTxtDirections.Select(richTxtDirections.Text.Length, 0);
              }
      
              //condition for menu redo edit button is enabled
              if (menuRedoEdit.Enabled == false)
              {
                  //if the keys (control and Z) are pressed enables the redo button...
                  menuRedoEdit.Enabled = true;
                  //sets the font to specified font and style to regular....
                  menuRedoEdit.Font = new Font(menu
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you want to clear the list when someone "types", why do you have extra conditions in the text changed event?

      if (UndoRedo.charList.Count > 0 && menuRedoEditClicked == false)

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      M 1 Reply Last reply
      0
      • L Lost User

        If you want to clear the list when someone "types", why do you have extra conditions in the text changed event?

        if (UndoRedo.charList.Count > 0 && menuRedoEditClicked == false)

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        M Offline
        M Offline
        mjcs100
        wrote on last edited by
        #3

        Because I don't want it to clear it every time the text changes or keydown event fires but just after the user starts to type and quit. So I tried to put an extra condition to help stop it but its not. I hope I am explaining myself right.

        L 1 Reply Last reply
        0
        • M mjcs100

          Because I don't want it to clear it every time the text changes or keydown event fires but just after the user starts to type and quit. So I tried to put an extra condition to help stop it but its not. I hope I am explaining myself right.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think you need to better explain what "type and quit" means. And you're talking as if "text changes" and "key down" are somehow independent. Maybe you should be looking at individual key presses in the preview key down event.

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          M 1 Reply Last reply
          0
          • L Lost User

            I think you need to better explain what "type and quit" means. And you're talking as if "text changes" and "key down" are somehow independent. Maybe you should be looking at individual key presses in the preview key down event.

            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

            M Offline
            M Offline
            mjcs100
            wrote on last edited by
            #5

            I'm sorry Gerry. I wondered if I explained it wrong. Let me try this again. I was looking for help on how to delete the list after the user is finished 'undo' commands or 'redo' commands, and the user starts typing on the keyboard, in the rich textbox, the rest of the directions for a recipe. I have used the the 'keydown' function to add the command that if a key was pressed, to delete the list if its greater than 0, but it deletes the list every time the user clicks the 'ctrl' and 'z' shortcut. I don't want it to empty the new characters added to the list by the 'undo' function, because there would be nothing left if he needs to 'redo' one or two. I was thinking if there was a text change, something like typing then stop to 'undo' and start typing again, that it may work, but it does not. Still deletes each character. I'm not even sure if that would be considered text change also. So now I am at a point I am asking if someone knows what how. I'm am fairly new at programming and have learned everything about it myself and doing it all myself. I know I explain things wrong or have the wrong terms for whatever, and I'm sorry if I did not explain better. My hubby always tells me I cant explain things the way they should be.

            L 1 Reply Last reply
            0
            • M mjcs100

              I have a program I am working on. First I will explain what I have done and then get to the question. I have created a undo and redo functions that when undo, it deletes one character at a time from the string and puts it in a 'List'. Then when you click redo, it goes to the 'List' and adds each character back to the string and deletes it from the list, one character at a time. You can see in the pictures the undo and redo menu button click events for them in the 1st pic. They get their functions from the 2nd pic., the undo redo class. I also have a keydown and text change function shown in pic. 3. All functions work good but the clearing of the 'list' that remains if they don't 'redo' all commands. I can undo and redo and have no problems, but I want it to delete the list contents after the user starts to type again after undoing and redoing. I have tried to put it in the text change but it keeps firing every time the list is over '0' and after the menu redo is enabled. I have tried putting in the buttons function, and I even tried to do the stack process, but I could not get my head wrapped around that option. I know that 'richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);' deletes 1 character, but how do you say if the 'text.length +1' from where you stop to undo and redo, how to add a character. I hope I am explaining this fine for you to understand and that someone can possibly help me with this part, even if it is not adding 1 to the text.length. Thank you.

              private void menuUndoEdit_Click(object sender, EventArgs e)
              {
              menuUndoEditClicked = true;

                      //accesses the undo function to undo last command
                      UndoRedo.undo();
              
                      if (richTxtDirections.Text.Length > 0)
                      {
                          //removes the last character in the text in the textbox
                          richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);
              
                          //positions the cursor at the end of the text
                          richTxtDirections.Select(richTxtDirections.Text.Length, 0);
                      }
              
                      //condition for menu redo edit button is enabled
                      if (menuRedoEdit.Enabled == false)
                      {
                          //if the keys (control and Z) are pressed enables the redo button...
                          menuRedoEdit.Enabled = true;
                          //sets the font to specified font and style to regular....
                          menuRedoEdit.Font = new Font(menu
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              mjcs100 wrote:

              I have created a undo and redo functions that when undo, it deletes one character at a time from the string and puts it in a 'List<char>'

              Yah, that changes a char, but doesn't do/undo the last action. You want a memento-pattern. It's easier, cleaner, and better, because it also can do/undo any font-changes.

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              1 Reply Last reply
              0
              • M mjcs100

                I'm sorry Gerry. I wondered if I explained it wrong. Let me try this again. I was looking for help on how to delete the list after the user is finished 'undo' commands or 'redo' commands, and the user starts typing on the keyboard, in the rich textbox, the rest of the directions for a recipe. I have used the the 'keydown' function to add the command that if a key was pressed, to delete the list if its greater than 0, but it deletes the list every time the user clicks the 'ctrl' and 'z' shortcut. I don't want it to empty the new characters added to the list by the 'undo' function, because there would be nothing left if he needs to 'redo' one or two. I was thinking if there was a text change, something like typing then stop to 'undo' and start typing again, that it may work, but it does not. Still deletes each character. I'm not even sure if that would be considered text change also. So now I am at a point I am asking if someone knows what how. I'm am fairly new at programming and have learned everything about it myself and doing it all myself. I know I explain things wrong or have the wrong terms for whatever, and I'm sorry if I did not explain better. My hubby always tells me I cant explain things the way they should be.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Your "key down" goes off and does something in "PerformClick()". I'd look there.

                menuUndoEdit.PerformClick();

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                1 Reply Last reply
                0
                • M mjcs100

                  I have a program I am working on. First I will explain what I have done and then get to the question. I have created a undo and redo functions that when undo, it deletes one character at a time from the string and puts it in a 'List'. Then when you click redo, it goes to the 'List' and adds each character back to the string and deletes it from the list, one character at a time. You can see in the pictures the undo and redo menu button click events for them in the 1st pic. They get their functions from the 2nd pic., the undo redo class. I also have a keydown and text change function shown in pic. 3. All functions work good but the clearing of the 'list' that remains if they don't 'redo' all commands. I can undo and redo and have no problems, but I want it to delete the list contents after the user starts to type again after undoing and redoing. I have tried to put it in the text change but it keeps firing every time the list is over '0' and after the menu redo is enabled. I have tried putting in the buttons function, and I even tried to do the stack process, but I could not get my head wrapped around that option. I know that 'richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);' deletes 1 character, but how do you say if the 'text.length +1' from where you stop to undo and redo, how to add a character. I hope I am explaining this fine for you to understand and that someone can possibly help me with this part, even if it is not adding 1 to the text.length. Thank you.

                  private void menuUndoEdit_Click(object sender, EventArgs e)
                  {
                  menuUndoEditClicked = true;

                          //accesses the undo function to undo last command
                          UndoRedo.undo();
                  
                          if (richTxtDirections.Text.Length > 0)
                          {
                              //removes the last character in the text in the textbox
                              richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);
                  
                              //positions the cursor at the end of the text
                              richTxtDirections.Select(richTxtDirections.Text.Length, 0);
                          }
                  
                          //condition for menu redo edit button is enabled
                          if (menuRedoEdit.Enabled == false)
                          {
                              //if the keys (control and Z) are pressed enables the redo button...
                              menuRedoEdit.Enabled = true;
                              //sets the font to specified font and style to regular....
                              menuRedoEdit.Font = new Font(menu
                  
                  L Offline
                  L Offline
                  lmoelleb
                  wrote on last edited by
                  #8

                  I am not sure what controls you are using, but have you checked undo/redo is not already supported by the control - I would expect it is. For example: Windows Forms Redo[^] WPF Redo[^] Your implementation is very limited. As already mentioned you do not handle formatting - but you also assume the last character is the last one entered.

                  1 Reply Last reply
                  0
                  • M mjcs100

                    I have a program I am working on. First I will explain what I have done and then get to the question. I have created a undo and redo functions that when undo, it deletes one character at a time from the string and puts it in a 'List'. Then when you click redo, it goes to the 'List' and adds each character back to the string and deletes it from the list, one character at a time. You can see in the pictures the undo and redo menu button click events for them in the 1st pic. They get their functions from the 2nd pic., the undo redo class. I also have a keydown and text change function shown in pic. 3. All functions work good but the clearing of the 'list' that remains if they don't 'redo' all commands. I can undo and redo and have no problems, but I want it to delete the list contents after the user starts to type again after undoing and redoing. I have tried to put it in the text change but it keeps firing every time the list is over '0' and after the menu redo is enabled. I have tried putting in the buttons function, and I even tried to do the stack process, but I could not get my head wrapped around that option. I know that 'richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);' deletes 1 character, but how do you say if the 'text.length +1' from where you stop to undo and redo, how to add a character. I hope I am explaining this fine for you to understand and that someone can possibly help me with this part, even if it is not adding 1 to the text.length. Thank you.

                    private void menuUndoEdit_Click(object sender, EventArgs e)
                    {
                    menuUndoEditClicked = true;

                            //accesses the undo function to undo last command
                            UndoRedo.undo();
                    
                            if (richTxtDirections.Text.Length > 0)
                            {
                                //removes the last character in the text in the textbox
                                richTxtDirections.Text = richTxtDirections.Text.Remove(richTxtDirections.Text.Length - 1);
                    
                                //positions the cursor at the end of the text
                                richTxtDirections.Select(richTxtDirections.Text.Length, 0);
                            }
                    
                            //condition for menu redo edit button is enabled
                            if (menuRedoEdit.Enabled == false)
                            {
                                //if the keys (control and Z) are pressed enables the redo button...
                                menuRedoEdit.Enabled = true;
                                //sets the font to specified font and style to regular....
                                menuRedoEdit.Font = new Font(menu
                    
                    J Offline
                    J Offline
                    jochance
                    wrote on last edited by
                    #9

                    You might consider using Stack versus List for charList. It has Push() (add to list), Peek() (look at the "topmost" list member), and Pop() (retrieve value and remove the topmost list member). It is just the sort of LIFO collection it sounds like you may want.

                    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