Simple Condition For Void [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 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;
}
}

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;
}
//...
}

How do i get my check boxes to display and add numbers into a label on C# [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 5 years ago.
Improve this question
Im trying to develop a form for an imaginary pet clinic in Visual Studio using C# and it consists of check boxes and a label. The checkboxes represent a service the clinic performs and when checked the price of the service is supposed to be displayed in the label, whenever any other boxes are checked theyre supposed to add to the price and when unchecked take away from the price. I am stuck on how i get the check boxes to have a value to display to the label
for example: the vaccinations checkbox (chkVaccinations) is supposed to cost 35 dollars
How do i get the check box to have a value of 35 and when checked to add to the label value which is zero?
private void chkVaccinations_CheckedChanged(object sender, EventArgs e) {
if (chkVaccinations.Checked)
{
}
else
{
}
private void lblTotalprice_Click(object sender, EventArgs e)
{
}
Tbh your question lacks alot of information to be able to help you, but here is an idea of a solution following your example assuming this is codebehind. If you want a full MVVM solution I can update the answer for that.
EDIT: Made some changed to adapt the answer to your latest request in the comments
private double TotalPrice { get; set; }
private void ComputeTotalPrice(double increment)
{
TotalPrice += increment;
}
private void chkVaccinations_CheckedChanged(object sender, EventArgs e)
{
if (chkVaccinations.Checked)
{
ComputeTotalPrice(priceAssociatedWithThisCheckBox);
}
else
{
ComputeTotalPrice(-priceAssociatedWithThisCheckBox);
}
UpdateLabel();
}
private void UpdateLabel()
{
If you want to show $ with 2 decimals
string formattedPrice = string.Format("{0:0.00}", TotalPrice).Replace(".00","");
lblTotalprice.Text= $"{formattedPrice}$";
}

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.

How to prevent data duplication in a datagridview [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
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
if (e.ColumnIndex==5)
{
form.ShowDialog();
}
}
I am trying to view the details of a movie when I press the view details button in the datagridview but for some reason I can't get it to work.
The place of the buttons in the datagridview is 5.
I'd show a ss but unfortunately I cant, yet.
The place of the buttons in the datagridview is 5
It means that the column is the fifth column?
If yes, don't forget that index in .Net are generally zero-based index. So it would be:
if (e.ColumnIndex==4)
Also, good remark from KyleMit, don't create an instance of MovieDetailsForm if you don't use it:
if (e.ColumnIndex==4)
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
form.ShowDialog();
}
Just to summarize what others have said and to help out your coding style...
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns["colDetailButton"].DisplayIndex == e.ColumnIndex)
{
// my guess is you also need other data, like the movie's IMDB number
string imdbValue = dataGridView1.Rows[e.RowIndex].Cells["colImdbValue"].Value.ToString();
using (var form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read))
{
form.ImdbValue = imdbValue;
form.ShowDialog();
}
}
else
{
// Remove this debugging code once you get your code working
Console.WriteLine("ColumnIndex {0} was clicked." e.ColumnIndex);
}
}
See this answer as to how to How to handle click event in Button Column in Datagridview? for a good overview of what to do. So long as you only have a single button, you actually don't have to specify the column index at all, which makes your code less fragile to change. Although, Chris is right, that indexes are zero based so you'd need a ColumnIndex of 4 to get the 5th column. You also don't have to new up your form unless you actually want to show it, so I'd move the declaration into the if statement like this:
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//make sure click not on header and column is type of ButtonColumn
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == _
typeof(DataGridViewButtonColumn))
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
form.ShowDialog();
}
}

C# ComboBox Usage [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
Google has not helped me on this one..
Say I have a combobox, with the values "X" and "Y".
What is the syntax to say..
"If the user selected X, do this, else do that."
I've tried several ways.. none work.
Thanks in advance.
I'll assume you're using WinForms, the property you're looking to use is ComboBox.Text.
Something like:
if (xyCombo.Text == "X")
// Do something
else (xyCombo.Text == "Y")
// Do something else
You have to subscribe to ComboBox's SelectedIndex changed event. Please refer to the below link.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx
Try combining the above answers, like this.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == "X")
//Action
else
//Other Action
}
or use a switch statement
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "X":
//Action
break;
case "Y":
//Another Action
break;
default:
break;
}
}

Categories