Random in C#
-
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
-
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
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?
-
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?
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.
-
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.
Create a variable, e.g.
userClicks
. Initialize it to 0. In your button'sClick
handler, increment the value ofuserClicks
, and if it is 10, set the button'sEnabled
property tofalse
. -- I've killed again, haven't I? -
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
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
-
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
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.
-
Create a variable, e.g.
userClicks
. Initialize it to 0. In your button'sClick
handler, increment the value ofuserClicks
, and if it is 10, set the button'sEnabled
property tofalse
. -- I've killed again, haven't I?Thank you, Thank you I've done it :-D
-
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.
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!
-
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.
just use an INVISIBLE TextBox control to store the track and count of the clicks.:zzz: