I have a button and a comboBox. The comboBox has 2 value, 'yes and no' I want to disable the button if the selected value is no while i want to enabled if the value selected is yes what would I do, I dont know where will I put the code and also my code seems wrong.
if (ComboBoxCustType.SelectedIndex = 0)
{
Button1.Enabled = false;
}
else
Button1.Enabled = true;
Since you didn't specify WinForms or WPF, this is for WinForms.
You have to create an event on the ComboBox.SelectedIndexChanged and inside the event handler, you write your code to handle the selecteditem text.
public Form1()
{
comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
InitializeComponent();
CheckSelction();
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckSelction();
}
void CheckSelction()
{
if (comboBox1.SelectedItem != null)
{
var item = comboBox1.SelectedItem.ToString();
button1.Enabled = item == "yes";
}
else
button1.Enabled = false;
}
Related
I would like to show a product balance in a TextBox when a product is selected from DataGridView.
When product is selected and the tab button is pressed then the product balance should be shown in the TextBox.
All the data is loaded from an Microsoft Access database. How can i achieve this.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(dataGridView1_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 1) //Desired Column
{
//getSum() function gives sum of a db table column
textBox1.Text = getSum();
}
}
I would add a KeyDown and a KeyUp event Handler to the DataGridView Or your Form or whatever can has focus on the time ;).
When the Key, which is pressed down is Tab i would set a member variable to true or false.
Here a short example of what i mean.
public partial class Form1 : Form
{
bool isTabPressed = false;
public Form1()
{
InitializeComponent();
button1.KeyDown += button1_KeyDown;
button1.KeyUp += button1_KeyUp;
}
void button1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
isTabPressed = true;
}
}
void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
isTabPressed = false;
}
}
}
Everything else you can do in your method, that you are currently writing and just check if isTabPressed is true.
Hope this helps.
If not leave a comment ;)
EDIT:
So something like this ?
Currently what it does is: you choose something and when you press tab it writes it to the textbox:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
editControlWasOpened = true;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1 != null && dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null && editControlWasOpened )
{
textBox1.Text = dataGridView1.CurrentCell.Value.ToString();
}
valueischanged = false;
}
From what i understand, based on the value you select in you ComboBox, getSum() wiill go and retrieve the total value from the database. If that's the case then add a SelectedIndexChanged event but casting the DataGridViewComboBoxCell into a ComboBox like the code below.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
var combobox = e.Control as ComboBox;
if (combobox != null)
{
combobox.SelectedIndexChanged -= ComboBox_SelectedIndexChanged;
combobox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var combobox = sender as ComboBox;
if (combobox != null)
{
var item = combobox.SelectedItem.ToString();
if (!string.IsNullOrEmtpy(item))
{
textBox1.Text = getSum();
// Adjust the line above as per your requirement.
// I don't fully understand what getSum() does as you haven't posted it
}
}
}
After a long time this was done .......and i am posting this for other user too!
My Question was
"**I would like to show a product balance in a TextBox when a product is selected from DataGridView. When product is selected and the tab button is pressed then the product balance should be shown in the TextBox. All the data is loaded from an Microsoft Access database. How can i achieve this."
I think the subject is "how to show textbox value when datagridview combobox value is selected"
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
{
myCon.Open();
OleDbCommand cmd1 = new OleDbCommand("SELECT sum_stock_purchase_quantity, sum_stock_purchase_quantity_thaan from productPurchaseQuery where stock_product_id=" + this.dataGridView1.CurrentCell.Value.ToString() + "");
cmd1.Connection = myCon;
OleDbDataReader reader = null;
reader = cmd1.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
textBox9.Text = Convert.ToString(reader["sum_stock_purchase_quantity"]);
textBox2.Text = Convert.ToString(reader["sum_stock_purchase_quantity_thaan"]);
}
}
else
{
textBox9.Text = "0.0";
textBox2.Text = "0.0";
}
myCon.Close();
Guys i did this. But this may be good or bad. But my objective is achieved.
I tried this but it doesn't work. They're still greyed out even when I select stuff.
btnVoirFiche.Enabled = false;
btnEchangerJoueur.Enabled = false;
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
}
You'll want to handle the ListBox.SelectedIndexChanged event, and within your handler you're going to check if the specific value is the selected one, and then set you button's enable property accordingly.
Something like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
//whatever you need to test for
}
}
Cheers
EDIT: I'm not too sure what your logic for button's enabled property is, so my answer is pretty generic. If you add details to you question, I'll adapt accordingly.
Hook into SelectedIndexChanged event and put your code inside of it
private void lstJoueurs_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
}
As an alternative, and using mrlucmorin's answer, you could use the listbox's SelectedItem which will return null if nothing is selected.
I just started learning C#.
Here's my code:
private void button1_Click(object sender, EventArgs e)
{
object Nappi1 = ("Nice button");
MessageBox.Show(Nappi1.ToString());
}
I got a textbox, that should disable the button1 if empty or whitespace.
I already got it working in some level, but it checks the state of the textbox on button1_Click.
private void button1_Click(object sender, EventArgs e)
{
if (textBox1 = "")
{
button1.enabled = false;
}
else
{
button1.enabled = true;
object Nappi1 = ("Nice button");
MessageBox.Show(Nappi1.ToString());
}
}
Fictional example:
if (textBox1 = "" or textBox1 = whitespace[s])
How could I make it check the state of the textbox onLoad (as soon as the program starts)?
How could I make it check if (multiple) whitespace, and can I write it to the same if -statement?
Please keep it simple.
To answer exactly the question title, Shorter, clearer:
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
Replace your if-else with this, if it is only a string:
if (string.IsNullOrWhiteSpace(textBox1)) {
button1.enabled = false;
}
else {
button1.enabled = true;
...
}
or use textBox1.Text if it is really a Textbox use this:
if (string.IsNullOrWhiteSpace(textBox1.Text)) {
button1.enabled = false;
}
else {
button1.enabled = true;
...
}
You want String.IsNullOrWhiteSpace:
if (String.IsNullOrWhiteSpace(textBox1.Text)) {
button1.enabled = false;
}
You originally had:
if (textBox1 = "") {
button1.enabled = false;
}
textbox is the control, you need to use the Text property which refers to the string literal inside the textbox control. Also in C# = is an assignment, you ideally would want == which is used to compare.
If you're not using .NET 4 or .NET 4.5 you can use:
String.IsNullOrEmpty
This can be done by using hooking an event handler to text-box text changed event.
Steps:
Attach an event handler for text-box (on text changed)
Inside the text changed event handler enable / disable the button.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text))
button1.Enabled = false;
else
button1.Enabled = true;
}
By default disable the button in InitializeComponent method of form
button1.Enabled = false;
In the textBox1 text changed event, right this code:
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
string.IsNullOrWhiteSpace("some text") will check if the text is none or just a wightspaces
if this is true you will set button1.Enabled to false.
I'm trying to make pictures appear, when the element in combobox is selected.
private void Form4_Load(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
pictureBox1.Visible = true;
pictureBox2.Visible = true;
pictureBox3.Visible = true;
pictureBox4.Visible = true;
}
}
I tried to do it this way, but that doesn't work. Where I’ve made the mistake?
The naming of your method makes me think you're doing that inside the form loading event.
Use the SelectedIndexChanged event of the combobox instead.
I have a Form which holds a DataGridView, this Form also loads with an invisible Form which only holds another DataGridView. The second DGV is used to display more information on the items in the first DGV.
The second DGV should only be shown when the user clicks inside the 7th Cell of any row in the first DGV. I have already managed to get it to hide when I click other cells, but I can't seem to get it to hide when I click outside the DataGridView. I have already tried the Leave, RowLeave and LostFocus events without success. I think it is because as soon as the second DataGridView is displayed, it gets the focus and this somehow messes with the event.
Here is my code:
public class Form1
{
Form schedules = new Form();
DataGridView backups = new DataGridView();
public Form1()
{
this.schedules.Visible = false;
backups.DataBind();
}
private void backups_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 7)
{
if (this.schedules.getData(Convert.ToInt32(backups.Rows[e.RowIndex].Cells[0].Value)))
{
this.schedules.Owner = this;
this.schedules.Visible = true;
this.schedules.changePosition(Cursor.Position);
}
else
{
this.schedules.Visible = false;
}
}
else
{
this.schedules.Visible = false;
}
}
}
public class Schedules : Form
{
DataGridView grdSchedules = new DataGridView();
public Schedules()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Visible = false;
this.AutoSize = true;
this.grdSchedules.RowHeadersVisible = false;
this.grdSchedules.AllowUserToAddRows = false;
this.grdSchedules.ScrollBars = ScrollBars.None;
this.grdSchedules.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.grdSchedules.AllowUserToResizeColumns = false;
this.grdSchedules.AllowUserToResizeRows = false;
this.grdSchedules.AllowUserToDeleteRows = false;
}
}
private void Form1_Click(object sender, EventArgs e)
{
this.schedules.Visible = false;
}
Users tend to click on the biggest window they see to close popups. You can also do the same with the secondary form; or even add a close button to it.
I think you would want to combine Form Click and Grid Leave event to make it work.
private void Form1_Click(object sender, EventArgs e)
{
detailForm.Visible = false;
}
private void dataGridView1_Leave(object sender, EventArgs e)
{
detailForm.Visible = false;
}
Now if a user clicks outside Grid on form or directly into a different control, then your detail form should be hidden.
Hope it helps.