6 Replies - 878 Views - Last Post: 06 November 2016 - 01:08 PM Rate Topic: -----

#1 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

How to know if previous insert to textbox was equal to actual insert

Posted 05 November 2016 - 09:09 PM

Hi,
Can you help me figure out, how to know if previous text inserted to the winforms textbox, and actual insert is equal to each other?

For example if I insert "word 1" to text box, then enter it, and after I insert "word 2" in this case nothing happens, but if I insert "word 1" again after "word 1", I must know about it.

Comparison of recorded and inputed lines does not make sense in this case, because I do not always save string into the text file, but I want to know if string is same with actual insert and previous insert comparison, even if line does not exist in file.

I need value which exist between two actual and previous inserts and if previous insert is equal to actual insert, display this fact, annul value, but if actual insert is different remove this temporal value.

I'm not sure how to properly get this result for actual insert with textbox, how to know about equality for next and previous insert.

I'm sure my attempt is bad and probably wrong, and my question would be rated as bad, but anyway:

I'm trying to add actual string record to the list with 0 index, then if next actual value is equal to string in this list, make field variable value equal to 1 and clear list, otherwise clear list and annul field variable value. Then if field variable is not equal to zero display "Repeat of previous insert" and annul field variable value, clear temporal list.

But seems like comparison of
actStr
and list record
rpt[0]
it is not comparison of previous and actual, but just actual with actual, so it does not works proper, ans looks like absolutely wrong way to do it:

     List<string> rpt = new List<string>();      
              
     string tempRptCheck = actStr;
     rpt.Insert(0, tempRptCheck);
    
     if (actStr == rpt[0])
     {
        rptVal = (rptVal = 1);
        rpt.Clear();
     }
     else
     {
        rptVal = (rptVal = 0);
        rpt.Clear();
     }
    
     if (rptVal > 0) 
     {
        Console.WriteLine("Repeat of previous insert");
        rptVal = (rptVal = 0);    
        rpt.Clear();
     }




To be clearer again:

I want get notification in case if actual input is equal to previous input, for example:

Insert to textbox is a string "word1", then press enter to further process. So it must be recorded in the list, and if following insert is equal to this previous insert, with same "word1", notify me about this equality, remove this string form the list and change it to actual "word1", and if next insert will be same again, do the same.

But if insert is "word1", it must be recorded to this list, and if following insert is not equal to this previous insert, remove this string form the list and change it to actual "word2" to avoid such case if insert is "word1", and following insert is "word2" not equal to this previous, and then next following insert is "word1" again equal to before last, it should not be considered as repetition.

So I want get notification of repetition only between actual and prevous, or to put it differently actual and next inserts.

To be more clear, I want exactly this logic, which works with desired result only with console application:

 static void Main(string[] args)
        {
            var words = new List<string>();

            while (true)
            {
                Console.Write("Add word: ");
                var input = Console.ReadLine();

                if (words.Contains(input))
                {
                    Console.WriteLine($"{input} is already in the list!");
                    continue;  
                }
                words.Clear();
                words.Add(input);
            }        
            Console.ReadLine();
        }


but It does not works with textbox, I got same result as with code above: each request as already exist. And
while (true)
loops in endless cycle, because same happens here, actual equals actual.

Also I've tried this way, to pass value about repeated text to field variable
rpt
but it does not works:

 string OldText = string.Empty;
    private void textBox1_GotFocus(object sender, EventArgs e)
    {
       OldText = textBox1.Text;
    }
    
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
       string newText = textBox1.Text;
                      
       if (newText == OldText)
       {
          rpt = (rpt = 1);
       }
       else
       {
          rpt = (rpt = 0);
       }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: How to know if previous insert to textbox was equal to actual insert

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How to know if previous insert to textbox was equal to actual insert

Posted 06 November 2016 - 12:34 AM

For your last code you say it does not work. In what way does it not work?

rpt = (rpt = 1); If rpt is a boolean variable then you should just set it to either true or false, or just to the expression (newText == OldText) which is already a boolean expression.

"then press enter to further process" You can write code to check for the Enter key being pressed. A simpler approach is to provide a nearby Button that the user can Click, also making their decision to "further process" very explicit.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How to know if previous insert to textbox was equal to actual insert

Posted 06 November 2016 - 12:48 AM

I will also mention that you cannot directly translate, or compare, code for a Console application to code for a WinForms (GUI) application. In particular, there is no need to consider a loop. A WinForms application already has an intrinsic, built-in, loop called the event loop. Your program is happy to sit and wait for some event to occur before doing anything.
Was This Post Helpful? 1
  • +
  • -

#4 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to know if previous insert to textbox was equal to actual insert

Posted 06 November 2016 - 06:10 AM

hi,
I'm not sure what is wrong here, I have no any updates or regex on textbox_change event, but if I use code TextBox_Leave with textbox.Tag:


private void TextBox_Leave(object sender, EventArgs e)
{
    var textBox = (TextBox)sender;
    var previousText = (string)textbox.Tag;
    if(textbox.Text.Equals(previousText))
    {
        // notify about same value
    }
    else
    {
        textbox.Tag = textbox.Text
    }
}



but I got same result. Just does not notifies me about same value, no changes. There is nothing special, between previous and actual insert, user writes text, then press enter, I even removed the cleaning of the textbox field after press enter, if it may be cause, but not, it does not works. And same with:



 List<string> a = new List<string>();

private void textBox1_Leave(object sender, EventArgs e)
    {

        if(a.Contains(textBox1.Text))
        {
            MessageBox.Show("Notify");
        }
        else
        {
            a.Clear();
            a.Add(textBox1.Text);
        }
    }



I checked it with regex and without for such possibilities, but result is same, no mater if it is absolutely raw insert or with different corrections for inserted text with textBox1_TextChanged event. Not sure, which part of code is wrong, debug step into or over does not show anything. But code which is in relation with textbox has nothing special, insert KeyDown and then (e.KeyCode == Keys.Enter). textBox1_TextChanged is empty, but if I use regex corrections, in both cases I have nothing in result
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How to know if previous insert to textbox was equal to actual insert

Posted 06 November 2016 - 11:03 AM

As you know how to step through your code then explore the values of your variables as you do so to discover what is happening and where it goes wrong:

  • Is every line executed as you expect?
  • Does it reach each part of your code?
  • What are your variables values and when do they stop being what they should?

Stepping through is not just about executing each line, but actually following the logic as you proceed and testing expectations.
Was This Post Helpful? 1
  • +
  • -

#6 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to know if previous insert to textbox was equal to actual insert

Posted 06 November 2016 - 12:45 PM

Finally I found easy solution without any textbox event with your help. Thank you
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How to know if previous insert to textbox was equal to actual insert

Posted 06 November 2016 - 01:08 PM

You're welcome.

It might be useful to others if you describe your solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1