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.
Related
I have a question!
I'm currently making a notepad app and everything was going well until now.
The way it's set up:
There's a panel with the menu buttons (File, Edit, Themes, & Format) and a panel at the bottom with all of the options under each tab. Both the menu button and sub-buttons are labels.
When you click a menu button, the bottom panel will show the buttons categorized under that section by making all labels there invisible, and then making the appropriate ones visible.
Everything seems to work fine except for the 2 buttons under Format (Font & Font Color). They show up, but do not do anything when clicked. Below is the code for it.
private void FontButton_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
if(fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fd.Font;
}
}
private void FontColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if(cd.ShowDialog() == DialogResult.OK)
{
richTextBox1.ForeColor = cd.Color;
}
}
private void Label4_Click(object sender, EventArgs e)
{
Config();
FontButton.Visible = true;
FontColor.Visible = true;
}
private void Config()
{
New.Visible = false;
Open.Visible = false;
Save.Visible = false;
Light.Visible = false;
Dark.Visible = false;
FontButton.Visible = false;
FontColor.Visible = false;
Undo.Visible = false;
Cut.Visible = false;
Copy.Visible = false;
Paste.Visible = false;
}
Is the code wrong? Is it because the labels are stacked?
If someone could lend a helping hand, that'd be great! Thanks in advance.
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;
}
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 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.