I am trying to figure out, for an assignment, a way to display a message that basically says "combo box is empty" after I remove all items from a combo box in C#. The assignment is very simple. I write a windows form in C# that is populated with fifteen states in a ComboBox and when I select an item from that list it is removed. I have it working but once all the items are gone It just sits there and I have to manually exit. Can someone point me in the right direction to get this to work, I am thinking maybe if statement is in order? Here is my code so far...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chapter_15_Ex._15._3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Thank You
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Your combo is empty");
}
Check the number of items left, after you remove one. You can Count on the Items collection to see how many items are left in the ComboBox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
if (comboBox1.Items.Count == 0)
MessageBox.Show("All Gone!");
}
You can check for the condition below
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Empty");
}
Related
I have a a simple Form, with a panel that holds a question with four CheckButtons as answers.
The users will go through the form and select the answer for each Question.
Once they click the button to accept the answer ("buttonNewAnswer_Click" below in the code)
the answer gets consolidated in a List named "answers" and then I write that into "results" and format it so I can write one line to a .csv file.
Once all the Questions are covered, the users will click "buttonExit_Click" button
and that will write the "results" to the .csv and also exit the Application.
Unfortunatelly I cannot get the "results" list from the "buttonNewAnswer_Click" to "buttonExit_Click".
Thank you for your help/suggestions.
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SIMPLE_FORM
{
public partial class Form1 : Form
{
//public List<String> results = new List<String>();
string myCsvFileTest = #"myFile.csv"
// Button to update the answers list
private void buttonNewAnswer_Click(object sender, EventArgs e)
{
// Algorithm to update the "answers" list
var results = new StringBuilder();
foreach (var i in answers)
{
results.AppendFormat("{0},", i.ToString());
}
}
// Button to write the results to a .csv and then close the application
private void buttonExit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Press \"Yes\" to confirm closing the Application", " ", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (StreamWriter writer = new StreamWriter(myCsvFileTest, true, Encoding.UTF8))
{
writer.WriteLine(results);
}
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
I'm trying to get the "results" list from the "buttonNewAnswer_Click" and use it somewhere else in the code such as "buttonExit_Click" to write to a .csv
You need to declare the results object as a Form1 class member.
Right now, you are defining it as a local variable in buttonNewAnswer_Click function - so it's destroyed once the function ends.
Simplified code based on the code in the question:
public partial class Form1 : Form
{
// declare and allocate
StringBuilder results = new StringBuilder();
private void buttonNewAnswer_Click(object sender, EventArgs e)
{
// fill the results object
foreach (var i in answers)
{
results.AppendFormat("{0},", i.ToString());
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
// you can use the result here.
// results
}
}
I am trying to write a data input app using Visual Studio 2017 C# for my company.
We have 2 tables, SI and SIC, in a SQL DB.
I have created a form and put a bound datagridview, which is bound to the SIC table.
On form load, it shows all data nicely.
What we want to do is enter data into this datagridview and save.
However, when entering a value into column D, we want to check that it doesn`t exist in column D in the SI table.
I did have it setup with a unique constraint, however, when the record wasn`t unique, an error message appeared, and pressing OK would delete all the other data on the row. Not desirable as there is quite a lot of data to be entered.
I am very new to programming in general, so would appreciate any advice that could be given.
My form code looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Product_List
{
public partial class NewProduct_frm : Form
{
public NewProduct_frm()
{
InitializeComponent();
}
private void NewProduct_frm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bHTESTDataSet.SIC' table. You can move, or remove it, as needed.
this.TableAdapter.Fill(this.bHTESTDataSet.SIC);
}
private void Save_btn_Click(object sender, EventArgs e)
{
this.Validate();
this.BindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.bHTESTDataSet);
}
private void Reload_btn_Click(object sender, EventArgs e)
{
this.TableAdapter.Fill(this.bHTESTDataSet.SIC);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Sunday");
comboBox1.Items.Add("Monday");
comboBox1.Items.Add("Tuesday");
comboBox1.Items.Add("Wednesday");
comboBox1.Items.Add("Thursday");
comboBox1.Items.Add("Friday");
comboBox1.Items.Add("Saturday");
comboBox1.SelectedIndex = comboBox1.FindStringExact("Sunday");
}
private void button1_Click(object sender, EventArgs e)
{
string var;
var = comboBox1.Text;
MessageBox.Show(var);
}
}
}
This is the code but when i try to run it, its fine but my drop down list did not show any values. I still new in this C# language, forgive me if i did not realized any mistake in the code. Somebody, please help and explain to me what is the error and the solution for it. I really need it. Much appreciate.
First off, you don't need to load the items in the "form load" event, you can do so in Form1() itself after InitializeComponent(), and secondly you could try using List and ItemsSource:
public Form1()
{
InitializeComponent();
List<string> items = new List<string> { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
comboBox1.ItemsSource = items;
comboBox1.SelectedIndex = 0; //since you know "Sunday" is at index 0
}
EDIT: Since as John comments, the code you provided already works, my answer will remain a suggestion to shorten & simplify your code.
I created a form with just 2 textboxes and a button. In the first one I type a temperature in Fahrenheit and when I press the button "Convert", the program calculates and puts the temperature in Celsius in the other TextBox. It's working fine.
Now I want the program to clear the second TextBox when I start typing in the first TextBox. Below I show just a part of the code, which didn't work. Can anybody help me?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Conv_Temp
{
public partial class Frm_Principal : Form
{
public Frm_Principal()
{
InitializeComponent();
}
public event EventHandler Leave;
private void Tb_Temp_Leave(object sender, EventArgs e)
{
MessageBox.Show("Leaving TB Tb_Temp");
Tb_Result.Text="";
}
}
}
I think you are almost there.
Try adding this under InitializeComponent();
this.Tb_Temp.TextChanged += new System.EventHandler(this.Tb_Temp_Leave);
Add new Event handler in your form designer code.
this.textBox1.TextChanged += new System.EventHandler(this.ModifyTextBox1);
and implement this event in above form.cs file(Form_Principal )
private void ModifyTextBox1(object sender, EventArgs e)
{
textBox2.Text = String.Empty;
}
Please follow good convention for writing codes this is just a demo.
I'm currently trying to make a simple paint program in C# using a Windows Forms Application. When converting my list of Points to an Array using the ToArray function, I'm getting a generic "ArgumentException was unhandled: Parameter is invalid" error. I know I've done this before and it's worked fine, is there something special about the DrawLines function that I'm not aware of? Below is the code, with the line in question being the last line in the panel1_Paint event. Thanks in advance for any help you can provide.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GetSig
{
public partial class Form1 : Form
{
bool paint = false;
List<Point> myPointList = new List<Point>();
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
myPointList.Add(e.Location);
panel1.Invalidate();
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLines(Pens.Black, myPointList.ToArray());
}
}
}
According to the MSDN:
The first two points in the array specify the first line.
You need minimum two points in your Array.
Well, you can't draw lines without at least two points :)
if (myPointList.Count >= 2)
{
e.Graphics.DrawLines(Pens.Black, myPointList.ToArray());
}
Most likely that exception is not coming from the call to ToArray but from e.Graphics.DrawLines.