none
Is a field but used like a type RRS feed

  • Question

  • Hi guys,

    In my 'safe' application, I am trying to make it so once the conditions of the form have been changed the function of this button changes. Once the background color has been changed etc I want it to display a message box to the user saying that they can only enter a new combination once the safe has been unlocked. I have highlighted the bit I am having trouble with

    Cheers guys,

    Joe

    private void btn_setcombo_Click(object sender, EventArgs e)
            {
    
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
                       
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
    
                }
                else
    			{
    				btn_test.Click = MessageBox.Show("The Safe must be unlocked before you can change the combination");
                }

    Thursday, December 20, 2012 2:32 PM

Answers

  • I think your condition is wrong. Seeing your message box text, I think you have to use below condition,

    private void btn_setcombo_Click(object sender, EventArgs e)
    {
         if(safestatus.Text != "Locked")
         {
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
    
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
          else
          {
               btn_test.Click += (s, ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");
          }
    }
    
    

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Jason Dot Wang Monday, December 24, 2012 6:11 AM
    • Marked as answer by Jason Dot Wang Monday, December 31, 2012 5:10 AM
    Friday, December 21, 2012 5:28 AM

All replies

  • Change that piece of code as below,

    btn_test.Click += (s,ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");
    

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, December 20, 2012 2:36 PM
  • Change that piece of code as below,

    btn_test.Click += (s,ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!


    Sorry that didn't work, it said something about invalid tokens
    Thursday, December 20, 2012 2:51 PM
  •  BackColor = Color.Red; //Changes the background colour of the form to red
    

    This is wrong. You are assigning value to a type itself not to any object.

    Remove this line.

    Hope this helps!


    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 2:56 PM
  • Are you using .NET framework older than 3.0? If yes, then use the below code, otherwise the above code should have worked.

    button1.Click += delegate(object s, EventArgs ev) { MessageBox.Show("Test"); };

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, December 20, 2012 3:03 PM
  •  BackColor = Color.Red; //Changes the background colour of the form to red

    This is wrong. You are assigning value to a type itself not to any object.

    Remove this line.

    Hope this helps!


    One good question is equivalent to ten best answers.


    I wanted the background color of the form to change to red
    Thursday, December 20, 2012 3:03 PM
  • Are you using .NET framework older than 3.0? If yes, then use the below code, otherwise the above code should have worked.

    button1.Click += delegate(object s, EventArgs ev) { MessageBox.Show("Test"); };

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!


    I am using .Net Framework 4.0
    Thursday, December 20, 2012 3:05 PM
  •  BackColor = Color.Red; //Changes the background colour of the form to red

    This is wrong. You are assigning value to a type itself not to any object.

    Remove this line.


    Actually that is correct line. That is equivalent to this.BackColor = Color.Red.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, December 20, 2012 3:05 PM
  •  BackColor = Color.Red; //Changes the background colour of the form to red

    This is wrong. You are assigning value to a type itself not to any object.

    Remove this line.


    Actually that is correct line. That is equivalent to this.BackColor = Color.Red.

    Please mark this post as answer if it solved your problem. Happy Programming!


    Yes my mistake.

    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 3:11 PM
  • Change that piece of code as below,

    btn_test.Click += (s,ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    These are build errors it lists

    Error 1 Invalid token 'else' in class, struct, or interface member declaration Form1.cs 48 13 The Safe Assignment
    Error 2 Invalid token '+=' in class, struct, or interface member declaration Form1.cs 50 20 The Safe Assignment
    Error 3 Invalid token '(' in class, struct, or interface member declaration Form1.cs 50 39 The Safe Assignment
    Error 4 Expected class, delegate, enum, interface, or struct Form1.cs 53 17 The Safe Assignment

    Thursday, December 20, 2012 3:21 PM
  • Change that piece of code as below,

    btn_test.Click += (s,ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!


    Sorry that didn't work, it said something about invalid tokens

    If you are using .NET 4.0 , this code should not give any error. Can you provide us you method code again.

    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 3:21 PM
  • Change that piece of code as below,

    btn_test.Click += (s,ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    These are build errors it lists

    Error 1 Invalid token 'else' in class, struct, or interface member declaration Form1.cs 48 13 The Safe Assignment
    Error 2 Invalid token '+=' in class, struct, or interface member declaration Form1.cs 50 20 The Safe Assignment
    Error 3 Invalid token '(' in class, struct, or interface member declaration Form1.cs 50 39 The Safe Assignment
    Error 4 Expected class, delegate, enum, interface, or struct Form1.cs 53 17 The Safe Assignment


    Your whole method block need little attention to correct syntax. There is no problem with this code, other codes are causing problems.

    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 3:23 PM
  • private void btn_setcombo_Click(object sender, EventArgs e)
            {
    
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
                       
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
    
                }
                else
    			{
    				btn_test.Click += (s,ev) =>   MessageBox.Show("The Safe must be unlocked before you can change the combination");
                }


    • Edited by jhubbert Thursday, December 20, 2012 3:24 PM
    Thursday, December 20, 2012 3:23 PM
  • Change that piece of code as below,

    btn_test.Click += (s,ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    These are build errors it lists

    Error 1 Invalid token 'else' in class, struct, or interface member declaration Form1.cs 48 13 The Safe Assignment
    Error 2 Invalid token '+=' in class, struct, or interface member declaration Form1.cs 50 20 The Safe Assignment
    Error 3 Invalid token '(' in class, struct, or interface member declaration Form1.cs 50 39 The Safe Assignment
    Error 4 Expected class, delegate, enum, interface, or struct Form1.cs 53 17 The Safe Assignment


    Your whole method block need little attention to correct syntax. There is no problem with this code, other codes are causing problems.

    One good question is equivalent to ten best answers.


    What would I need to change?
    Thursday, December 20, 2012 3:29 PM
  •  private void btn_setcombo_Click(object sender, EventArgs e)
            {
    
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
                       
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
    
                
                else
    			{
    				btn_test.Click += (s,ev) =>   MessageBox.Show("The Safe must be unlocked before you can change the combination");
                }
            
            }
    Just copy paste the whole method carefully. Its all done :)

    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 3:38 PM
  • Hi,

    wait , where is your if block ?

    you have written else , but where is if block ?


    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 3:40 PM
  •  private void btn_setcombo_Click(object sender, EventArgs e)
            {
    
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
                       
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
    
                if(//your condition)
                {
                }
                else
    			{
    				btn_test.Click += (s,ev) =>   MessageBox.Show("The Safe must be unlocked before you can change the combination");
                }
            
            }
    Provide condition in if block. This should work.

    One good question is equivalent to ten best answers.

    • Proposed as answer by JMCF125 Thursday, December 20, 2012 4:10 PM
    Thursday, December 20, 2012 3:42 PM
  •  private void btn_setcombo_Click(object sender, EventArgs e)
            {
    
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
                       
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
    
                if(//your condition)
                {
                }
                else
    			{
    				btn_test.Click += (s,ev) =>   MessageBox.Show("The Safe must be unlocked before you can change the combination");
                }
            
            }
    Provide condition in if block. This should work.

    One good question is equivalent to ten best answers.


    It does now but the InputBox from earlier in my code appears instead
    Thursday, December 20, 2012 4:07 PM
  • >> It does now but the InputBox from earlier in my code appears instead

    Not getting exactly what you need.


    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 4:28 PM
  • >> It does now but the InputBox from earlier in my code appears instead

    Not getting exactly what you need.


    One good question is equivalent to ten best answers.

    I meant that I want it to display the Message Box that we have just created rather than the InputBox appearing. This is my entire code

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MyDialogs; //Uses the MyDialogs class
    
    namespace The_Safe_Assignment
    {
    
        public partial class Form1 : Form
        {
            string str_safe_combo; //The string for the safe combination 
    
    
    
    
            public Form1()
            {
                InitializeComponent();
    
    
    
            }
    
            private void btn_setcombo_Click(object sender, EventArgs e)
            {
    
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
    
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
    
                if (BackColor == Color.Red)
                {
                }
                else
                {
                    btn_test.Click += (s, ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");
                }
    
            }
    
            private void btn_test_Click(object sender, EventArgs e)
            {
                if (keyentry.Text == str_safe_combo)  //If the combination entered into the textbox matches the combination entered by the user
                {
                    BackColor = Color.Green; //Changes the background colour of the form to green
                    safestatus.BackColor = Color.LawnGreen; //Changes the background colour of the safe status text box to light green
                    safestatus.Text = ("Unlocked"); //Changes the text of the safe status text box so it says 'unlocked'
                    keyentry.BackColor = Color.LawnGreen; //Changes the background colour of the text box where the user tests the combination to light green
                    btn_test.Text = ("Lock"); //Changes the text of the test key button so it says 'Lock'
                    btn_test.BackColor = Color.Pink; //Changes the background colour of the text key button to pink
                    keyentry.Text = (""); //Makes the key entry text box empty for security reasons
    
    
                }
    
                else
                {
                    if (keyentry.Text != str_safe_combo)  //If the combination entered into the textbox does not match the combination previously entered by the user
                    {
                        MessageBox.Show("Combination is incorrect!"); //Displays a message to the user saying that the entered combination is incorrect
                        BackColor = Color.Red; //Changes the background colour of the form to red
                        safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                        safestatus.Text = ("Locked"); //Changes the text of the safe status text box to 'Locked'
                        keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                    }
                }
            }
        }
    }
    
    
            
    
    
    

    Thursday, December 20, 2012 4:29 PM
  • I think your condition is wrong. Seeing your message box text, I think you have to use below condition,

    private void btn_setcombo_Click(object sender, EventArgs e)
    {
         if(safestatus.Text != "Locked")
         {
                str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
    
                while (str_safe_combo.Length <= 5) //Makes sure that the user has to enter a valid combination
                {
                    MessageBox.Show("Combination must be greater than 5 characters!");
                    str_safe_combo = My_Dialogs.InputBox("Please enter a safe combination containing at least 5 numbers"); //Displays an Input Box to the user so they can enter a combination for the safe 
                }
    
                BackColor = Color.Red; //Changes the background colour of the form to red
                safestatus.BackColor = Color.Pink; //Changes the background colour of the safe status text box to pink
                safestatus.Text = ("Locked"); //Changes the text in the safe status textbox to 'Locked'
                keyentry.BackColor = Color.Yellow; //Changes the background colour of the key entry text box to yellow
                btn_test.Enabled = true; //Enables the test key button
                btn_test.Text = ("Test Access Code"); //Changes the text on the button from nothing to 'Test Access Code'
          else
          {
               btn_test.Click += (s, ev) => MessageBox.Show("The Safe must be unlocked before you can change the combination");
          }
    }
    
    

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Jason Dot Wang Monday, December 24, 2012 6:11 AM
    • Marked as answer by Jason Dot Wang Monday, December 31, 2012 5:10 AM
    Friday, December 21, 2012 5:28 AM