Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
I am trying to make a simple text-based spammer, but I seem to be running into an issue with some of the code under Keydown event not letting me start a timer.
I am relatively new to c# and c++ though I have made console applications before that was quite massive and this is my first time running into an issue like this.
I have already tried pretty much everything. I always used stack overflow for most of my questions but it doesn't seem to have an answer to my problem.
private void Button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "M") ;
{
timer1.Enabled == true; //right here is my issue
}
}
I am trying to start my timer with the short cut key, in this case, Ctrl + M and it will then start a timer which will continue until stopped by another shortcut key (not implemented yet) or the stop button. It gives the error
CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Would love some help thank you in advance!
Change
timer1.Enabled == true;
to
timer1.Enabled = true;
In addition, remove the semi colon at the end of your if statement. This is causing your timer to start as soon as any key is pressed.
if (e.Control && e.KeyCode.ToString() == "M") ;
to
if (e.Control && e.KeyCode.ToString() == "M")
Related
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)
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.
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 am developing a chatting system. And I have a Button to send the text in the text box to the chat log. How am I going to stop the user from press Enter key and to disable the Enter key. I know there are many posts out there like this, but the solutions haven't worked for me.
You can try something like this:-
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
I think you do not need to stop the user from pressing enter but instead send the chat to the other person on press of enter.
Also if you have any other shortcuts to be allowed then you can have a look at this C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as "Ctrl+A" to get through?
Using the same you can also block
private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress=true;
}
}
Your question is a little ambiguous to say the least; however, the textbox control has an event called KeyDown : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
This way, you can capture whenever Enter is pressed and modify and behavior as needed, here is an example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.Enter == e.KeyCode)
{
MessageBox.Show("Enter Was Pressed");
textBox1.Text = new String(textBox1.Text.Where((ch, i) => i < textBox1.Text.Length - 2).ToArray());
textBox1.SelectionStart = textBox1.Text.Length;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have 2 forms .. form1 and form2 .. I have button1 on form1 that loads form2 but every time I click button1 it loads new instance of form2
I want button1 to bring to front form2 if it was open and restore it if it was minimized
This should work (haven't tested it though)
public static bool _Invoked;
Form2 f2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
if (!_Invoked)
{
_Invoked = true;
f2.Show();
}
else if (_Invoked)
{
f2.BringToFront();
_Invoked = false;
}
}
Add a comment for further clarification
EDIT:
Just tested this and its working
Form2 f2 = new Form2();
bool _Clickone = false;
private void button1_Click(object sender, EventArgs e)
{
if (!_Clickone)
{
_Clickone = true;
f2.Show();
}
else
{
f2.WindowState = FormWindowState.Normal;
f2.ShowInTaskbar = true;
f2.BringToFront();
}
}
Then handle the Form Closing event of the second for m
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
You can do this by creating an onClose event in your form2, where you cancel the close, and set the visibility to hidden.
Then instead of creating a new instance, set the visibility to visible again.
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 8 years ago.
Improve this question
I'm working on a windows form application and I have a button and a textbox in it.
When the button is pressed, it should make the textbox visible and hidden.
myTextbox.Visible = !myTextbox.Visible;
Did you try Google?
textBox1.Visible = false;
You can toggle the visibility by doing:
if(textBox1.Visible == true)
textBox1.Visible = false;
else
textBox1.Visible = true;
WinForm:
private void button1_Click(object sender, System.EventArgs e)
{
textBox.Visible = !textBox.Visible;
}
WPF:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox.Visibility != System.Windows.Visibility.Hidden)
textBox.Visibility = System.Windows.Visibility.Hidden;
else
textBox.Visibility = System.Windows.Visibility.Visible;
}
You can find an example here
private void button1_Click(object sender, System.EventArgs e)
{
/* If the CTRL key is pressed when the
* control is clicked, hide the control. */
if(Control.ModifierKeys == Keys.Control)
{
((Control)sender).Hide();
}
}
textbox.visible=true;
you should try this on buttonClick event