0

I have a couple of text boxes that i have initially hidden and what I want to do is as the users type I would like the next text box along with the label that goes with it to appear to notify of the next question.

At the same time if they change their mind and delete their response to the first question the next text box and label will disappear once the text has been deleted.

Here is my current code:

private void CreditScoreBox_KeyPress(object sender, KeyPressEventArgs e)
 {
  if (char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar))
    e.Handled = false;
  else
    e.Handled = true;
  if(CreditScoreBox.Text == "")
   {
    MakeBox.Visible = false;
    MakeLabel.Visible = false;
    ModelBox.Visible = false;
    ModelLabel.Visible = false;
    CreditLevelLabel.Visible = false;
   }
  else
   {
    MakeBox.Visible = true;
    MakeBox.Enabled = true;
    MakeLabel.Visible = true;
    CreditLevelLabel.Visible = true;
   }

I have tried using the TextChanged event with the same result.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Brandonl29
  • 35
  • 5
  • possible duplicate of [How do I fill an empty textbox with default text?](http://stackoverflow.com/questions/5178247/how-do-i-fill-an-empty-textbox-with-default-text) – Adriano Repetti May 22 '14 at 15:46
  • Does this code not work? It looks like it is at lest most of the way there. What is your question? – BradleyDotNET May 22 '14 at 16:17
  • 1
    To close vote reviewers, it doesn't seem like this is a duplicate of the linked question, but I can't figure out what he is asking. (Just my 2 cents, I'm not sure how this has 3 duplicate close votes). – BradleyDotNET May 22 '14 at 16:30
  • @BradleyDotNET The code works as to when i start typing a number the the next label and box appear. The question is if the user clears his text i want the label and box to disappear again. – Brandonl29 May 22 '14 at 16:35
  • I don't understand why this question is getting down voted what did I do wrong. I mean seriously aggravates me when I have a legitimate question and if one person doesn't understand it they automatically down vote it before they get clarity. – Brandonl29 May 22 '14 at 16:38
  • 2
    Have you tried using TextChanged instead of KeyPress? That might pick up the empty string condition better. BTW, I agree about the downvotes. This question isn't *that* bad. – BradleyDotNET May 22 '14 at 16:38
  • I edited to hopefully mitigate future downvotes. Please review and make sure you agree with the changes! It could be the extra backstory was making the question "muddy" and so meets the, "This question is unclear" portion of the downvote criteria. The last paragraph is still confusing to me, so you may consider editing it as well (I would have but didn't understand it enough to try and fix it). – BradleyDotNET May 22 '14 at 16:41
  • @BradleyDotNET To be honest that was the original code. I had private void CreditScoreBox_TextChanged(object sender, EventArgs e) but I still had the same issue. The other question and textbox items would not go invisible. – Brandonl29 May 22 '14 at 16:42
  • Understood, and it may provide context if I understood what you were trying to say :) At any rate, it doesn't hurt the quality of the question *too* badly, but if you think you can make it clear please do (pretend we know nothing about what kind of array you would have, why you would have one, and why a "MaxCreditScore" is important, and you may come up with a way to explain it better). Thank you for letting me know you tried the other event (that would be good information to edit into the question!). I'll keep looking/thinking. – BradleyDotNET May 22 '14 at 16:45
  • @BradleyDotNET I don't have an array for this part of the form but the reason I need a max credit score is because the questions on the form I'm building is "What is your current credit score?" "What make are you looking for?" "What model of your make are you looking to buy?" Each of those questions have a textbox for there response. I was thinking that if they type in something like 8000 for a credit score my response would be something to let them know that a credit score is only up to 850. This is a project for me its not a live form, I'm just trying to better myself. – Brandonl29 May 22 '14 at 16:52
  • @BradleyDotNET I have a arrays for the vehicles that they can choose from and models as well but I just used a "!= MakeArray[x]" in an "if" statement to get the question that follows the make to disappear after i delete the question for model and the textbox. – Brandonl29 May 22 '14 at 16:54
  • @Adriano I have review the question you speak of before but it is way above my head and I really don't comprehend what is happening in the answers that followed, but it also is dealing with text within the textbox, I am asking about the labels and textboxes themselves to disappear. – Brandonl29 May 22 '14 at 16:59
  • @BradleyDotNET thanks for the edit. Its hard for me to translate what I'm asking in my head verses what I actually type. – Brandonl29 May 22 '14 at 17:02
  • No problem, one suggestion given on meta is to look around at other highly voted questions and try to model yours on them. Yours really wasn't that bad, it just needed a few line breaks and the removal of non-pertinent information (I've seen far, far worse on this site). It looks like the "array" information is also not pertinent to this problem, so I removed it and added that you have tried using TextChanged. – BradleyDotNET May 22 '14 at 17:14
  • Have you tried setting a breakpoint in this event and see what the states are once you delete the value in the textbox? Your logic seems to be correct, however I would use the TextChanged event instead of a KeyPress. My assumption is you are not selecting the correct controls to enable and make visible, hence the breakpoint test. If you truly need a code snippet please say so and will put one in the answers – KSdev May 22 '14 at 17:29
  • I went ahead and coded it up and put it in the answers. Works flawlessly for me. – KSdev May 22 '14 at 17:41

1 Answers1

2

I created a Form, with 2 textbox and 1 label. If text exist in textbox1 then label1 and textbox2 become available:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text == "")
    {
        textBox2.Enabled = false;
        textBox2.Visible = false;
        label1.Visible = false;
    }
    else
    {
        textBox2.Enabled = true;
        textBox2.Visible = true;
        label1.Visible = true;
    }
}
KSdev
  • 621
  • 3
  • 12