Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Random in C#

Random in C#

Scheduled Pinned Locked Moved C#
questioncsharpdatabasehelplounge
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alee15 10 88
    wrote on last edited by
    #1

    Hi everyone, I've got a problem and I can't solve it with anything! I have a button that when one clicks on it a groupbox is enabled showing a label with the value of a row which was random from a database. Then when one clicks the next button another question is random and displayed in the same label.Now I would like that after the user clicks the next button for ten times, it is disabled. How can I do this? :confused: Thanks

    O J 2 Replies Last reply
    0
    • A alee15 10 88

      Hi everyone, I've got a problem and I can't solve it with anything! I have a button that when one clicks on it a groupbox is enabled showing a label with the value of a row which was random from a database. Then when one clicks the next button another question is random and displayed in the same label.Now I would like that after the user clicks the next button for ten times, it is disabled. How can I do this? :confused: Thanks

      O Offline
      O Offline
      Office Lineman
      wrote on last edited by
      #2

      Can you elaborate on the problem? Is it disabling the button? Is it selecting random questions from a database? Is it keeping track of ten clicks? Help us help you. -- I've killed again, haven't I?

      A 1 Reply Last reply
      0
      • O Office Lineman

        Can you elaborate on the problem? Is it disabling the button? Is it selecting random questions from a database? Is it keeping track of ten clicks? Help us help you. -- I've killed again, haven't I?

        A Offline
        A Offline
        alee15 10 88
        wrote on last edited by
        #3

        Yes a new question is random everytime I click the Next button. But it's not keeping track of the ten clicks that the user makes since I don't know how and at the moment the button is not disabled.

        O J 2 Replies Last reply
        0
        • A alee15 10 88

          Yes a new question is random everytime I click the Next button. But it's not keeping track of the ten clicks that the user makes since I don't know how and at the moment the button is not disabled.

          O Offline
          O Offline
          Office Lineman
          wrote on last edited by
          #4

          Create a variable, e.g. userClicks. Initialize it to 0. In your button's Click handler, increment the value of userClicks, and if it is 10, set the button's Enabled property to false. -- I've killed again, haven't I?

          A 1 Reply Last reply
          0
          • A alee15 10 88

            Hi everyone, I've got a problem and I can't solve it with anything! I have a button that when one clicks on it a groupbox is enabled showing a label with the value of a row which was random from a database. Then when one clicks the next button another question is random and displayed in the same label.Now I would like that after the user clicks the next button for ten times, it is disabled. How can I do this? :confused: Thanks

            J Offline
            J Offline
            Jakob Farian Krarup
            wrote on last edited by
            #5

            Hi Alee :-D It would be helpful if you told us whether this is a windows- or web application. But I guess you're talking about web. You could store the number of times in ViewState (a hidden form field on the webpage) Something along the lines of: http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/[^] Here is the code below, also available at: http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/limitedclicks.zip[^] --------------------------------------------------------------------------

            public class WebForm1 : System.Web.UI.Page
            {
            protected System.Web.UI.WebControls.Label lblQuote;
            protected System.Web.UI.WebControls.Label lblNumberOfClicksLabel;
            protected System.Web.UI.WebControls.Label lblNumberOfClicksValue;
            protected System.Web.UI.WebControls.Button btnShowQuote;

            //makes it easier to store an int in ViewState by wrapping it in a property
            protected int NumberOfClicks
            {
            	get 
            	{
            		//if there is no value in ViewState, return 0 (zero)
            		if(this.ViewState\["clicks"\] == null)
            			return 0;
            		else
            			return (int)ViewState\["clicks"\];
            	}
            	set{this.ViewState\["clicks"\] = value;}
            }
            
            private void btnShowQuote\_Click(object sender, System.EventArgs e)
            {
            	//if there are still clicks left
            	if(this.NumberOfClicks < 10)
            	{
            		//show quote
            		lblQuote.Text = GetQuote();
            		//increment number of clicks
            		NumberOfClicks++;
            	}
            	else
            	{
            		//let them know that the show is over
            		lblQuote.Text = "\[no more quotes for you!\]";
            		lblQuote.ForeColor = Color.Red;
            	}
            }
            
            public string GetQuote()
            {
            	//implement your own code to get random quote
            	if(this.NumberOfClicks % 2 == 0
            		return "Even is pretty good";
            	else
            		return "Odd is good too ; )";
            }
            
            private void WebForm1\_PreRender(object sender, System.EventArgs e)
            {
            	lblNumberOfClicksValue.Text = this.NumberOfClicks.ToString();
            }
            

            }

            Three kinds of people in the world: - Those who can count.. - Those who can't! -- modified at 14:47 Thursday 6th April, 2006

            A 1 Reply Last reply
            0
            • J Jakob Farian Krarup

              Hi Alee :-D It would be helpful if you told us whether this is a windows- or web application. But I guess you're talking about web. You could store the number of times in ViewState (a hidden form field on the webpage) Something along the lines of: http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/[^] Here is the code below, also available at: http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/limitedclicks.zip[^] --------------------------------------------------------------------------

              public class WebForm1 : System.Web.UI.Page
              {
              protected System.Web.UI.WebControls.Label lblQuote;
              protected System.Web.UI.WebControls.Label lblNumberOfClicksLabel;
              protected System.Web.UI.WebControls.Label lblNumberOfClicksValue;
              protected System.Web.UI.WebControls.Button btnShowQuote;

              //makes it easier to store an int in ViewState by wrapping it in a property
              protected int NumberOfClicks
              {
              	get 
              	{
              		//if there is no value in ViewState, return 0 (zero)
              		if(this.ViewState\["clicks"\] == null)
              			return 0;
              		else
              			return (int)ViewState\["clicks"\];
              	}
              	set{this.ViewState\["clicks"\] = value;}
              }
              
              private void btnShowQuote\_Click(object sender, System.EventArgs e)
              {
              	//if there are still clicks left
              	if(this.NumberOfClicks < 10)
              	{
              		//show quote
              		lblQuote.Text = GetQuote();
              		//increment number of clicks
              		NumberOfClicks++;
              	}
              	else
              	{
              		//let them know that the show is over
              		lblQuote.Text = "\[no more quotes for you!\]";
              		lblQuote.ForeColor = Color.Red;
              	}
              }
              
              public string GetQuote()
              {
              	//implement your own code to get random quote
              	if(this.NumberOfClicks % 2 == 0
              		return "Even is pretty good";
              	else
              		return "Odd is good too ; )";
              }
              
              private void WebForm1\_PreRender(object sender, System.EventArgs e)
              {
              	lblNumberOfClicksValue.Text = this.NumberOfClicks.ToString();
              }
              

              }

              Three kinds of people in the world: - Those who can count.. - Those who can't! -- modified at 14:47 Thursday 6th April, 2006

              A Offline
              A Offline
              alee15 10 88
              wrote on last edited by
              #6

              Actually at the moment I'm working with windows and when I'll be ready I'm going to do it in a web application.

              J 1 Reply Last reply
              0
              • O Office Lineman

                Create a variable, e.g. userClicks. Initialize it to 0. In your button's Click handler, increment the value of userClicks, and if it is 10, set the button's Enabled property to false. -- I've killed again, haven't I?

                A Offline
                A Offline
                alee15 10 88
                wrote on last edited by
                #7

                Thank you, Thank you I've done it :-D

                1 Reply Last reply
                0
                • A alee15 10 88

                  Actually at the moment I'm working with windows and when I'll be ready I'm going to do it in a web application.

                  J Offline
                  J Offline
                  Jakob Farian Krarup
                  wrote on last edited by
                  #8

                  Well then... here is a Windows version just for you :-D http://www.sweetsilence.dk/codeprojectprojects/limitedclicks/LimitedQuotes_Winsolution.zip[^] Kind regards - Jakob Three kinds of people in the world: - Those who can count.. - Those who can't!

                  1 Reply Last reply
                  0
                  • A alee15 10 88

                    Yes a new question is random everytime I click the Next button. But it's not keeping track of the ten clicks that the user makes since I don't know how and at the moment the button is not disabled.

                    J Offline
                    J Offline
                    junglerover77
                    wrote on last edited by
                    #9

                    just use an INVISIBLE TextBox control to store the track and count of the clicks.:zzz:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups