Click here to Skip to main content
16,016,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I was wondering if i could get a little advice...
I'm almost at the end of a hench project but i'm having some problems getting the bugs out

basically I need to pass, ammend and return a string input from a textbox.

Using stringbuilder the character 'x' is included every other letter of the string (ie abc turns into axbxcx). The new string is then returned to sendtext where it is written to a serial port.

using textboxes to test, the new string is printed correctly when tried in the "txt_ammend" method, but only prints the last character in the string within "sendtext"

Any suggestions would be great
Cheers,
M


C#
//***************sends string trom txt box to serial
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13)
    {
        string ErrorMessage = Sendtext(textBox1.Text);
    }
}

public string Sendtext(string ToSend)
{
    txt_ammend();   //calls method                               
    try
    {
        foreach (char sb in ToSend)
        {
            serialPort1.WriteLine(sb.ToString());
            Thread.Sleep(950);
            textBox2.Text = sb.ToString();  //this only displays last 
                                            //char of string
        } 
    }
    finally
    {
        serialPort1.Write("x"); 
    }
    return "OK";
}

//**************************stringbulider method
string txt_ammend()            
{
    StringBuilder sb = new StringBuilder();
    foreach (char c in textBox1.Text)
    {
        sb.Append(c);
        textBox3.Text = sb.ToString();   //this shows correctly
                                         //ammended string
        if (!char.IsWhiteSpace(c))       //ie axbxcxdx etc
        {
            sb.Append("x");
            //Thread.Sleep(500);
        }
    }
    return sb.ToString();
}
Posted
Updated 20-Jan-11 8:03am
v2

Well, You're calling txt_ammend, but you're not receiving the returned value into a variable. You should probably do this:

string myText = txt_ammend();


And then work with the myText.

There are other inconsistencies in your code, but this should get you to where you want to do.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Jan-11 14:28pm    
This is the only correct answer so far - my 5.
Sergey Alexandrovich Kryukov 20-Jan-11 14:41pm    
Propose as a final answer. OP, I suggest you accept this one.
Sergey Alexandrovich Kryukov 20-Jan-11 14:57pm    
OP, you should also reconsider serial port communication part: it will only work properly if you dedicate a separate thread for it. See also discussion with Nishant.
That's because you block the UI thread when you run that code. So your changes to the textbox won't render on screen until the loop is done with by which time it will only show the last character you set it to.

Use a background thread for the serialport write, or append the text to the textbox instead of overwriting it during each loop iteration.

[Edit]
------

Since this answer has been challenged, I'll add more text so the OP understands what I am talking about. You have this loop here:

C#
foreach (char sb in ToSend)
{
    serialPort1.WriteLine(sb.ToString());
    Thread.Sleep(950);
    textBox2.Text = sb.ToString();
}


I assume from the Sleep call you have there, that you expect to see character by character appearing in your textbox. This wont happen because Sleep will block the current thread, which in your case is the UI thread. So unless that is in a background thread you will not see the characters flashing by one after the other.

So the alternative is to change this line:

C++
textBox2.Text = sb.ToString();


to:

C++
textBox2.Text += sb.ToString();



Now at the end of the loop, you will see the entire set of characters.

BTW read John's reply too, he addresses a different bug in your code.
 
Share this answer
 
v2
Comments
Nish Nishant 20-Jan-11 14:32pm    
Ok, whoever voted this a 1 did not go through the OP's code.
Sergey Alexandrovich Kryukov 20-Jan-11 14:34pm    
Nishant, unfortunately, your answer is wrong this time.

My original comment is incorrect in this part:
"txt_amend if in fact very fast, could not cause any noticeable delay. Also, the delay would not cause the problem described. If one implemented threaded solution it would add unwanted complexity and possibly more problems (considering OP's skills level at the moment)."

You correctly point out a need in threading, even though it has nothing to do with the reported problem. But without multi-threading this code will not properly.
However, I'm not sure background worker is appropriate. Whole design should be modified -- all serial port IO should probably run in a separate thread -- permanently.

Correct answer is John's: the result of this method is calculated but not assigned.

Respectfully,
--SA
Nish Nishant 20-Jan-11 14:39pm    
I don't think the OP's code is fully correct or optimized anyway. But my answer correctly addressed why he only sees the last character. It does not matter that he does not check the return value from txt_ammend. All it returns is the content of textBox1.Text (with some space filtering).

But the reason he only sees the last character is that his code is:

textBox2.Text = sb.ToString();

when it should really be:

textBox2.Text += sb.ToString();

I am sorry you disagree, and I hope your hasty disagreement will not confuse the OP.

OP, if you are reading this, do read my answer and if you have any questions, feel free to ask me about it.
Sergey Alexandrovich Kryukov 20-Jan-11 14:51pm    
I agree about OP's code.

I'm not criticize everything you say, only two things:

1) You did not notice that result of txt_amend is not assigned; it's OK, but you should agree this is a more of a root of the problem and admit: if this is not fixed, everything else makes no sense;
2) You should also agree that your idea to use threading is not related to this problem.

From the other hand, I admit I was not right about threading. I failed to understand your recommendation is about the rest of "Sendtext", not related to "txt_amend".

I apologize for messing things up and misleading comment.

Therefore I think your answer deserves "4".

Thank you for your notes.
Respect,
--SA
Nish Nishant 20-Jan-11 14:58pm    
Please note, I don't mind you or anyone down voting my answer at all. I only took issue with you saying that the answer was incorrect. I understand what you are saying, and I do agree that John's reply does actually address the bigger problem. I think it's best to close this discussion here lest the OP wonders what we are doing on his thread. Thank you for explaining to me what you did though, it makes it seem a lot better than initially when I did get a little annoyed! :-)

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