Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 599727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:31:45+00:00 2026-05-13T16:31:45+00:00

I tried: private void textBox1_KeyDown(object sender, KeyEventArgs e) { streamWriter.Write(e.Key.ToString()); } But I don’t

  • 0

I tried:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    streamWriter.Write(e.Key.ToString());
}

But I don’t know how to convert a Key to string correctly. I also tried:

private void textBox1_TextInput(object sender, TextCompositionEventArgs e)
{
    streamWriter.Write(e.Text);
}

But this event is not called. The farthest I went was:

private string previous = string.Empty;
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    var text = textBox1.Text.Substring(previous.Length);
    streamWriter.Write(text);
    previous = textBox1.Text;
}

But this has problems with character deletion, and many other cases.
What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:31:46+00:00Added an answer on May 13, 2026 at 4:31 pm

    The easiest option is to use KeyPress instead of KeyDown.

    KeyPressEventArgs.KeyChar provides the actual char, already translated appropriately. Just write this char to your stream directly.


    Edit:

    After seeing in the comments that this is WPF, not Windows Forms (as tagged), the above will not exist.

    That being said, I question your goal here – using a textbox to write to a stream, in a character by character fashion, is always going to be problematic. This is due to what you wrote in your last sentence:

    But this has problems with character deletion, and many other cases.

    Personally, I would take one of two different approaches:

    1. Use a TextBox as a “buffer”, and only send the entire contents to the stream when the focus is lost, etc. This way, the user can delete, etc, as needed, and you always have a complete “string” to work with. This would probably be my first choice, if you want the behavior to be similar to what you’re after now.

    2. Don’t use a TextBox for this. Use some other control which allows text entry, but not deletion or selection. If you must use a TextBox, and must handle key-by-key streaming, then you should restrict the input (via a behavior or attached property) to only the character you wish, which means disabling deletion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 322k
  • Answers 322k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer We are using the built-in HttpRuntime Caching in our application,… May 14, 2026 at 12:51 am
  • Editorial Team
    Editorial Team added an answer Ah, can it be the query plan sucks? SP's get… May 14, 2026 at 12:51 am
  • Editorial Team
    Editorial Team added an answer I'm not aware of this, but the code window has… May 14, 2026 at 12:51 am

Related Questions

I need TextBox which will reflect changes in databound string. I tried following code:
And I'm doing some exercises about switch. I just did it from console application
I'm trying to design a code where one guess a number. I defined the
I need help with my code. I would like to write only numbers/integers in
Hi I am creating Spell Checker for my c# application. I am using using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.