I'm trying to display / hide a 2 combo boxes depending on the chosen event type.
What the program should do is: If exhibition is selected, the groupbox of exhibition will be visible, while having the clubbing groupbox hidden.(Vice versa if clubbing is selected)
Code:
namespace Promoter.Forms
public partial class eventCreate : Form
{
public eventCreate()
{
InitializeComponent();
//import enum value to combo box
cmbEventType.DataSource = Enum.GetValues(typeof(EventType));
}
private void eventCreate_Load(object sender, EventArgs e)
{
if (cmbEventType.Text == "Exhibition")
{
grpClubbing.Visible = false;
grpExhibition.Visible = true;
//import enum values to combo box
cmbExhibitionVenue.DataSource = Enum.GetValues(typeof(ExhibitionVenue));
}
else if (cmbEventType.Text == "Clubbing")
{
grpExhibition.Visible = false;
grpClubbing.Visible = true;
//import enum values to combo box
cmbClubbingVenue.DataSource = Enum.GetValues(typeof(ClubbingVenue));
}
}
Try using the SelectedIndexChanged event.
private void cmdEventType_SelectedIndexChanged( object sender, EventArgs e ) {
ComboBox cb = (ComboBox)sender;
grpClubbing.Visible=false;
grpExibition=false;
switch ( cb.SelectedText ) {
case "Exhibition":
grpExhibition.Visible = true;
//import enum values to combo box
cmbExhibitionVenue.DataSource = Enum.GetValues(typeof(ExhibitionVenue));
break;
case "Clubbing":
grpClubbing.Visible = true;
//import enum values to combo box
cmbClubbingVenue.DataSource = Enum.GetValues(typeof(ClubbingVenue));
break;
default:
break;
}
}
Fixed it, just had a logic missing from my mind.
Instead of
private void eventCreate_Load(object sender, EventArgs e)
I had to reason out and do it at:
private void cmbEventType_SelectedIndexChanged(object sender, EventArgs e)
Related
I'm trying to create a design form in visual studio with 4 checkboxes and I really want to make the user to check only one of them, and if he's not checked one, when he will press a button, he should receive a notification with the obligation to select a box, and the program should not starting.
RadioGroup is a control very similar in appearance to CheckBox. It's used to select only one RadioGroup in each group. You can define groups of radio buttons puttin inside a container (a Form, a Panel, a GroupBox). Add 4 radio buttons to your form, set the Text property.
You can check if a radio button is selected:
var isChecked = radioButton1.Checked;
Or make a method like this:
private int GetSelectedRadioIndex()
{
var buttons = new[]
{
this.radioButton1,
this.radioButton2,
this.radioButton3,
this.radioButton4
};
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i].Checked)
{
return i;
}
}
return -1;
}
If you get a <0 index, there aren't a radio selected. In other case you have a 0 index of the radio that is selected.
As indicated, use a container like a Panel or GroupBox while with a GroupBox you can set a caption to indicate what the RadioButtons are for.
Create a private list in the form
private List<RadioButton> _radioButtons;
Subscribe to the Form's OnShown event, add the following code where OptionsGroupBox is a GroupBox with four Radio Buttons. This ensures no default selection which is optional.
private void OnShown(object sender, EventArgs e)
{
_radioButtons = OptionsGroupBox.Controls.OfType<RadioButton>().ToList();
_radioButtons.ForEach(rb => rb.Checked = false);
}
Add a button to assert/get their selection.
private void CheckSelectionButton_Click(object sender, EventArgs e)
{
var selection = _radioButtons.FirstOrDefault(x => x.Checked);
if (selection == null)
{
MessageBox.Show("Make a selection");
}
else
{
MessageBox.Show($"You selected {selection.Text}");
}
}
Edit: Working with both Panel and GroupBox
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace RadioButtonApp
{
public partial class Form1 : Form
{
private List<RadioButton> _radioButtonsGroupBox;
private List<RadioButton> _radioButtonsPanel;
public Form1()
{
InitializeComponent();
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
_radioButtonsGroupBox = OptionsGroupBox.Controls.OfType<RadioButton>().ToList();
_radioButtonsGroupBox.ForEach(rb => rb.Checked = false);
_radioButtonsPanel = OptionsPanel.Controls.OfType<RadioButton>().ToList();
_radioButtonsPanel.ForEach(rb => rb.Checked = false);
}
private void CheckSelectionInGroupBoxButton_Click(object sender, EventArgs e)
{
var selection = _radioButtonsGroupBox.FirstOrDefault(x => x.Checked);
if (selection == null)
{
MessageBox.Show("Make a selection");
}
else
{
MessageBox.Show($"You selected {selection.Text}");
}
}
private void CheckSelectionInPanelButton_Click(object sender, EventArgs e)
{
var selection = _radioButtonsPanel.FirstOrDefault(x => x.Checked);
if (selection == null)
{
MessageBox.Show("Make a selection");
}
else
{
MessageBox.Show($"You selected {selection.Text}");
}
}
}
}
How can I make button property set to enabled=true after all my textboxes are not empty?
I'm learning programming and my apps are simple.
I know how to enable this property when one of my textboxes have text but this is not the case.
Use case is that user need to put data in both textboxes and after that will be able to click btn.
How in most simple way can I validate all form and then enable button?
There are just 2 tb:
https://i.imgur.com/JUslNWE.png
You need to create a TextBox_TextChanged event and subscribe to all text boxes.
private void TextBox_TextChanged(object sender, EventArgs e)
{
int notEmptyTextBoxCount = 0;
int textBoxCount = 0;
foreach (var item in Controls)
{
if (item is TextBox txtb)
{
textBoxCount++;
if (txtb.Text != String.Empty)
notEmptyTextBoxCount++;
}
}
if (textBoxCount == notEmptyTextBoxCount)
button.Enabled = true;
else
button.Enabled = false;
}
Thanks guys for all feedback.
I have managed to do this this way:
private void ValidateTextBoxes()
{
if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
{
generateHashBtn.Enabled = true;
}
else
{
generateHashBtn.Enabled = false;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
I want to be able to select multiple rows in a datatable and then if they are selected, change the value in the cells.
The code I have is:
private void btnSetToReceived_Click(object sender, EventArgs e)
{
SetToReceived();
}
private void SetToReceived()
{
this.dgvPod.CurrentCell.Value = "Yes";
}
private void dgvPod_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
dgvPod.BeginEdit(true);
SetToReceived();
}
What I get from your question is, you want to select multiple cells and then start typing and you expect all the selected cell values change:
To do so, you can handle EditingControlShowing event and get the TextBox editing control and handle its TextChanged event and update text of selected cells. For example:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 4;
dataGridView1.RowCount = 4;
TextBox txt = null;
dataGridView1.EditingControlShowing += (s1, e1) =>
{
if (dataGridView1.EditingControl is TextBox)
{
if (txt == null)
{
txt = (TextBox)dataGridView1.EditingControl;
txt.TextChanged += (s2, e2) =>
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
cell.Value = txt.Text;
};
}
}
};
}
The code doesn't check if the selected cells are all in the same column, however it shows in an example how to get the text at the same time of typing and also how to set value of other selected cells.
I would like to make a tool on my form that would allow the user to add or remove textboxes using a [+] and [-] button. This should only be possible if the items "*.doc" or "*.docx" are selected in a ComboBox.
I have tried this for the .doc thingy:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbExtension.Text)
{
case "Other...":
string extensionName = Interaction.InputBox("Enter the new extension's name (for example *.txt): ", "New extension!");
File.AppendAllText(strPath, "\n" + extensionName);
// string extensionFunction = Interaction.InputBox("Enter the type of file it's supposed to be (for example Microsoft Word 2016): ", "Give us an idea.");
cmbExtension.Items.Clear();
LoadLines();
break;
case "*.doc":
btnPlus.Show();
break;
case "*.docx":
btnPlus.Show();
break;
default:
btnPlus.Hide();
break;
}
// As well as using similar code in these things, now empty:
if (cmbExtension.Text == "Other...")
{
}
if (cmbExtension.Text == "*.doc" || cmbExtension.Text == "*.docx")
{
}
}
You can use a FlowLayoutPanel in which you add and remove the textboxes.
To add a TextBox to a FlowLayoutPanel (or any container control) use:
TextBox textBox = new TextBox();
this.flowLayoutPanel1.Controls.Add(textBox);
To remove the last added TextBox from FlowLayoutPanel (or any container control) use:
int count = this.flowLayoutPanel1.Controls.Count;
if (count > 0)
{
this.flowLayoutPanel1.Controls[count - 1].Dispose();
}
Hear is an simple example with:
A FlowLayoutPanel with FlowDirection set to TopDown
Two buttons named btnPlus and btnMinus
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmbExtension.Items.Add("*.doc");
cmbExtension.Items.Add("*.docx");
cmbExtension.Items.Add("Other...");
btnPlus.Hide();
btnMinus.Hide();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbExtension.Text)
{
case "Other...":
// ...
btnPlus.Hide();
btnMinus.Hide();
break;
case "*.doc":
btnPlus.Show();
btnMinus.Show();
break;
case "*.docx":
btnPlus.Show();
btnMinus.Show();
break;
default:
btnPlus.Hide();
btnMinus.Hide();
break;
}
}
private void btnPlus_Click(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
this.flowLayoutPanel1.Controls.Add(textBox);
}
private void btnMinus_Click(object sender, EventArgs e)
{
int count = this.flowLayoutPanel1.Controls.Count;
if (count > 0)
{
this.flowLayoutPanel1.Controls[count - 1].Dispose();
}
}
}
Snapshots:
I'm new to C# and I need this function for a program im working on for school. I need to make a new window pop up when i click a button, not a message box though like a forms window, one that i can design with text boxes and buttons. What is on the new pop up window depends on the previous window but i can figure that out.
Also I need a way to close the previous window once the new one appears
Here's my code:`
// This makes sure only one box is checked
private void MulCB_CheckedChanged(object sender, EventArgs e)
{
if( MulCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void DivCB_CheckedChanged(object sender, EventArgs e)
{
if (DivCB.Checked == true)
{
MulCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void AddCB_CheckedChanged(object sender, EventArgs e)
{
if (AddCB.Checked == true)
{
DivCB.Checked = false;
SubCB.Checked = false;
MulCB.Checked = false;
}
}
private void SubCB_CheckedChanged(object sender, EventArgs e)
{
if (SubCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
MulCB.Checked = false;
}
}
private void oneDCB_CheckedChanged(object sender, EventArgs e)
{
if(oneDCB.Checked == true)
{
twoDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void twoDCB_CheckedChanged(object sender, EventArgs e)
{
if ( twoDCB.Checked == true)
{
oneDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void threeDCB_CheckedChanged(object sender, EventArgs e)
{
if (threeDCB.Checked == true)
{
oneDCB.Checked = false;
twoDCB.Checked = false;
}
}
// ends here
// Button operation
private void button8_Click(object sender, EventArgs e)
{
var form = new Form();
}
}
}
`
Thanks a lot!
Sal
The project is im supposed to make a quizzing program for kids. They should be able to choose 1 operation and the amount of digits the numbers will have. It then has to out put 10 random questions according to the selection made by the kid, then once they have completed the quiz, it should display their results and which questions they got wrong.
Assuming that the design of the window doesn't have to be completely dynamic, you can design it in Visual Studio (I'm assuming you did so with the first one). Then you can pass the results to the window. Like:
// Note: Form2 ist the name of your designed From
Form2 myform = new Form2();
this.Hide();
//You could pass the question settings like this
// 1 is for multiplication, 2 for division,3 for addition, 4 for substraction
myform.operation=1;
myform.digits=2
myform.Show();
And in the code of Form2:
namespace Yournamespace {
public partial class Form2: Form {
//Add these two lines about here
public static int operation;
public static int digits;
public Form2() {
InitializeComponent();
}
}
}
Then you can use the variables in Form2 and fill in the textbox or other elements you might design.
Also: You cloud use radio buttons instead of checkboxes as you then won't have you worry about unchecking the other checkboxes.