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

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)

Related

Windows Form: Display unique panel based on list selection [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 2 years ago.
Improve this question
Using a Windows Form I am trying to replicate the functionality of a "Tab Control" but instead of the panel selection being controlled by a tab selection it would be controlled by a list. Is there a built in way to produce this using neatly like the tab control or should I just check if a list value is equal to some value and if yes display panel else don't?
This is probably a dumb question so sorry for wasting anyone's time and thanks in advance!
Besides, you can also use tabpages in tabcontrol as "panel". Just hide the header via the code
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;
foreach (TabPage tab in tabControl1.TabPages)
{
tab.Text = "";
}
}
Then select the tabpage like,
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(((ListBox)sender).SelectedItem.ToString() == "tabPage2")
{
tabControl1.SelectedTab = tabPage2;
}
//...
}

KeyDown event not letting timer start [closed]

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")

How to check If method is executed? [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 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.

Hashtable application [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
I have a massive problem with a project im working on. I am trying to make a WFA which will take input from user and then the user will choose to either add what they typed using a hashtable or delete something out of that hashtable by using the add and remove buttons...
Im really struggling on how to add the user input to the hashtable??
someone please help!!!
namespace Lab6_Library2 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
Hashtable books = new Hashtable();
books = textBoxInput.Text;
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void buttonView_Click(object sender, EventArgs e)
{
MessageBox.Show("All The Books Added are: \n" + textBoxInput);//+ );
}
If you'd like to keep the collection content, you have to move it to class level:
public partial class Form1 : Form
{
HashSet<string> books = new HashSet<string>();
// (...)
}
I used generic HashSet<string> here, because it's the right one to use in your case.
To add item to HashSet instance, use Add method:
private void buttonAdd_Click(object sender, EventArgs e)
{
books.Add(textBoxInput.Text);
}
To remove items, use Remove method:
books.Remove(textBoxInput.Text);

Dissapear text in textbox in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I made a textbox of name tbFirstNumber
When i enter any value in that textbox , i want that Value to dissapear
I am using C# window form application in visual studio 2008
private void tbFirstNumber_TextChanged(object sender, EventArgs e)
{
}
tbFirstNumber.Text = "";
Just set it to empty on text changed event.
If you want that user is not allowed to enter any text, you can make the textbox as read only.
Set the TextBox control's ReadOnly property to true.
tbFirstNumber.ReadOnly = true;
Another way would be to hook in to the KeyPress event.
private void tbFirstNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Stop the character from being entered into the control
e.Handled = true;
}
private void tbFirstNumber_TextChanged(object sender, EventArgs e)
{
tbFirstNumber.Text = "";
}
Clear the value of the text box
this.tbFirstNumber.Text = ""

Categories