How to check If method is executed? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to prevent some methods execution when an other method is executed that's why i think to check if that last did. The problem i didn't find a helpful solution, any ideas please.
private void LoadScenarioNumber()
{
//some code
}
private void NumberOfScenariosChanged(Object sender, EventArgs e)
{
//if LoadScenarioNumber() is excuted
return;
else
UpdateScenarioDataGrid();
}

You could simply set a global boolean value in your method like this:
private bool loadScenarioNumberExecuted = false;
private void LoadScenarioNumber()
{
//some code
loadScenarioNumberExecuted = true;
}
private void NumberOfScenariosChanged(Object sender, EventArgs e)
{
if (loadScenarioNumberExecuted)
return;
UpdateScenarioDataGrid();
}
As an alternative you could use a state driven pattern where you have an enumeration of possible states and assign the current state to a global variable.
For more information on state driven programming you can read the following article:
http://www.codeproject.com/Articles/509234/The-State-Design-Pattern-vs-State-Machine

private bool executed;
private void LoadScenarioNumber()
{
//some code
executed = true;
}
private void NumberOfScenariosChanged(Object sender, EventArgs e)
{
if (executed)
return;
else
UpdateScenarioDataGrid();
}

boolean isExecuted = false;
private void LoadScenarioNumber()
{
// your code
isExecuted = true;
}
private void NumberOfScenariosChanged(Object sender, EventArgs e)
{
if (isExecuted)
return;
else
UpdateScenarioDataGrid();
}

It sounds like your first function updates the scenario grid based on the numbers (when loading from, say, disk), and the event handler is responding to user-input and updates the grid in the same fashion, and you are trying to avoid calling that update-function twice.
A far cleaner solution imho would be to have the LoadScenariosNumber populate the control that contains this, and then theoretically the event will be raised to notify listeners that it has and therefore the scenario grid will be updated as if the user had changed the control themselves.
This is a much more fluid, less error-prone method then setting global variables in an attempt to track what has been updated, and when.

Normally you use a, for example, bool _scenarioNumberLoaded = false, to check if a certain piece of code is executed.

Related

I am trying to get no of button clink in Label its work only 2 click [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
int count = 1;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Label1.Text = Convert.ToInt32(count).ToString();
}
}
protected void btncount_Click(object sender, EventArgs e)
{
count++;
Label1.Text = Convert.ToInt32(count).ToString();
}
It works only 1 click after that it's not working.
What is the issue I am not getting.
From the Page_Load method, this looks like a web application. Web applications are stateless. What this means for you is that every request back to the server will result in an entirely new instance of your class. Which means this happens on every request:
int count = 1;
So by design in the code shown count can never be more than 2.
You'll need to persist that value somewhere. A database, a file, even session state could possibly work for your needs. For example:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
Session["count"] = 1;
Label1.Text = Session["count"].ToString();
}
}
protected void btncount_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(Session["count"]);
count++;
Session["count"] = count;
Label1.Text = count.ToString();
}
In this case the page creates an initial value for the first page load (in the if (IsPostBack) block) and both displays this value as well as persists the value in sessions tate. Then the button click handler gets the value from session state, increments it, saves it back to session state as well as displays it.
Session state will temporarily follow that one user, but will not be permanent or visible to other users. It's just a quick illustration of persisting a value outside the scope of just the page class.
If you want the value to be more permanent and global to all users, consider storing it in a database instead. The overall structure would be the same... Create an initial value somewhere. Then in the button click you'd fetch the persisted value, update it, and save it back.

How to check if a click event is already in process C# Winforms

I have a method like this:
private async void BtnGivePermit_Click(object sender, EventArgs e)
{
//some code here
}
And I'm trying to add a button to Cancel and in there I wanted to check if any other click event is not in progress. How can I do that in C#?
As Ash suggested, you may want to have a boolean like so:
bool isGivePermitProcessing = false;
private void BtnGivePermit_Click(object sender, EventArgs e)
{
isGivePermitProcessing = true;
try {
...
} finally {
isGivePermitProcessing = false;
}
}
This will give you a definite understanding of whether the function is currently processing or not.
The try-finally block ensures that no exception will cause the isGivePermitProcessing boolean to become indefinitely true.
Extending the above concept into Jimi's suggestion, you could also simply disable the button like so:
private void BtnGivePermit_Click(object sender, EventArgs e)
{
BtnGivePermit.Enabled = false;
try {
...
} finally {
BtnGivePermit.Enabled = true;
}
}
This will also give you a definite understanding of whether the function is currently processing or not while additionally preventing the button from being clicked while already processing a previous click.
If you're intending to make a cancel button for asynchronous operations, you might want to look into the CancellationToken type.

Action is not performed when clicking ComboBox and Button [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm totally new into forms and I have an problem.
I would like to choose one of items in comboBox then hit button and my action regarding to chosen item is performed.
I'm creating list with options, boolean to check if button was hit and integer index.
List<string> options = new List<string> {"Dodaj studenta", "Wyƛwietl studenta", "Edytuj studenta" };
private bool button1WasClicked = false;
int index;
I'm trying to read index from comboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
index = comboBox1.SelectedIndex;
}
Setting handle to button to change the value of boolean to true when user hits button:
private void button1_Click(object sender, EventArgs e)
{
button1WasClicked = true;
}
And setting the comboBox:
private void comboBoxSetup()
{
this.comboBox1.DataSource = options;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; //read only
if(index == 0 && button1WasClicked == true)
{
System.Windows.Forms.MessageBox.Show("My message here");
}
}
PS: In constructor I have comboBoxSetup(); :)
When I'm checking only index in condition - popup is visible. Thanks for any help in advance!
Thanks to #Plutonix the solution that worked:
Insted of calling comboBoxSetup() in constructor I moved whole code from this method to
button1_Click(object sender, EventArgs e)

Simple Condition For Void [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have 2 controls for highlighting, and separated void control like:
public void yee()
{
rtb.ForeColor = Color.Red;
}
public void yoo()
{
rtb.ForeColor = Color.Blue;
}
then I have a combobox which have an item of:
yee and yoo .
when I select yee the void yee should be selected but when I select yoo the void yoo should be selected.
I know its kinda easy but i need the condition to be "case" instead of "if" since I want to have break, and if it's possible make the void = false if one got selected .
Thanks a lot! and sorry for this newbie question .xD
It is still unclear what you are trying to do, but if you are wanting to add a switch statement to determine which method that you need to run, assign it to your ComboBox's SelectedIndexChanged or SelectedValueChanged EventHandlers.
See if something like this is what you are wanting.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
switch ((sender as ComboBox).SelectedItem.ToString().ToLower()) //Have to get the string representation
{ //since Selected Item is an object
case "yee":
yee();
break;
case "yoo":
yoo();
break;
}
}
It's not totally clear what you're trying to do, but this is my best stab at it. Note DropDownList1 is the ID of your DDL.
if(DropDownList1.SelectedItem.Text == "yee"){
yee();
}
if(DropDownList1.SelectedItem.Text == "yoo"){
yoo();
}
Well, you can just do a bool function
such as
public boolean yeeOrYooPressed() {
{
if(//yee is selected) {
return true;
}
else {
return false;
break;
}
}

Navigation and memorising text box data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The program has a panel which holds a text box and the panel has two buttons on each side.
Each button acts as a 'next' (>>) and 'previous' (<<) navigation. I want to be able to navigate to the next panel by clicking '>>' this will clear the text box. Then when I click '<<' I want to go back to the previous panel with the text box containing the data previously added. However I want to do this without having to create two panels on top of each other and setting the visibility to true or false (which I am able to do). I want to achieve this by using only the one panel so the process can be done an infinite number of times. I hope this is clear to understand if you require more information please let me know.
Here is an image of my interface to clarify things:
since you have the page number, why not just create a list (or use a dictionary with the page number as a key), then in the button handler for >> and << collect the text for the current page (and put it in the list or dictionary) and replace it with the text for the previous page (from the list or dictionary).
code could look something like this:
public partial class Form1 : Form
{
Dictionary<Decimal, String> TextInfo;
public Form1()
{
InitializeComponent();
TextInfo= new Dictionary<Decimal, String>();
}
private void Form1_Load(object sender, EventArgs e)
{
numPage.Value = 1;
}
private void bnForward_Click(object sender, EventArgs e)
{
if (TextInfo.ContainsKey(numPage.Value))
{
TextInfo[numPage.Value] = textBox1.Text;
}
else
{
TextInfo.Add(numPage.Value, textBox1.Text);
}
numPage.Value++;
if (TextInfo.ContainsKey(numPage.Value))
{
textBox1.Text = TextInfo[numPage.Value];
}
else
{
textBox1.Text = "";
}
}
private void bnBack_Click(object sender, EventArgs e)
{
if (numPage.Value == 1)
return;
if (TextInfo.ContainsKey(numPage.Value))
{
TextInfo[numPage.Value] = textBox1.Text;
}
else
{
TextInfo.Add(numPage.Value, textBox1.Text);
}
numPage.Value--;
if (TextInfo.ContainsKey(numPage.Value))
{
textBox1.Text = TextInfo[numPage.Value];
}
else
{
textBox1.Text = "";
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
}

Categories