Hashtable application [closed] - c#

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

Related

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)

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}$";
}

Code still runs though it has been deleted [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 6 years ago.
Improve this question
I have a an app. Form 1 has a button which was set to open an instance of form2. I got some funny behaviour with the form reopening itself. So i deleted the code from that button on form1. Still when i click on that button, its opening the form2. Any idea??
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
Try Clean, then rebuild the project.

Using an object from another method [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
My program goes like this:
I have a public "main method" of the class, which got a data table with values.
I want that when the user will press a specific button, I will calculate something in the event of the button with the data table from the "main method" of the class (public classname()).
There is anyway to do that?
Here is a quick example:
using System.Data;
...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
//load your table here
...
}
// Create an event (maybe via the designer) for button click...
private void button1_Click(object sender, EventArgs e)
{
//you can reference the datatable here, for example tell how many rows it has
MessageBox.Show(dt.Rows.Count.ToString());
...
}

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