I have a problem its been bugging me for 2 days now.
Now the problem is that Uncertainty checkbox is not responding to events when I click search.
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 THE_HELP
{
public partial class MainPanel : Form
{
public MainPanel()
{
InitializeComponent();
}
private void buttonSelectionSearch_Click(object sender, EventArgs e)
{
{
foreach (CheckBox chk in groupBox4.Controls)
{
chk.Checked = false;
{
if (checkBoxCCF.Checked == true)
{
tabControl1.SelectedTab = tabPage1;
}
else if (checkBoxReliabilty.Checked == true)
{
tabControl1.SelectedTab = tabPage2;
}
else if (checkBoxRisk.Checked == true)
{
tabControl1.SelectedTab = tabPage3;
}
else if (checkBoxSaftey.Checked == true)
{
tabControl1.SelectedTab = tabPage4;
}
else if (checkBoxSensitivity.Checked == true)
{
tabControl1.SelectedTab = tabPage5;
}
else if (checkBoxThroughput.Checked == true)
{
tabControl1.SelectedTab = tabPage6;
}
else if (checkBoxUncertainity.Checked == true)
{
tabControl1.SelectedTab = tabPage7;
}
}
}
}
}
private void checkBoxReliabilty_CheckedChanged(object sender, EventArgs e)
{
}
private void groupBoxReliability_Enter(object sender, EventArgs e)
{
}
}
}
CheckBox Uncertanity is not responding to events.
You could make it like this
var checkbox = new CheckBox[] {checkBoxReliability, checkBoxRisks, ... };
var tab = new TabPage[] {tabPageReliability, tabPageRisks, ... };
for(int i = 0; i < checkbox.Length; i++)
if(checkbox[i].Checked)
{
tabControl1.SelectedTab = tab[i];
break;
}
Related
In Form1 :
private void recordStripMenuItem_Click(object sender, EventArgs e)
{
recordToggle = !recordToggle;
if (recordToggle)
{
recordStripMenuItem.Text = "Stop";
Icon = iconRed;
TextInfo("Recording");
record.Start();
}
else
{
recordStripMenuItem.Text = "Record";
Icon = iconGreen;
TextInfo("Waiting");
record.Stop();
}
}
And i want to enabled false/true the button in form1 depending on the flag state in this form :
using Capture_Screen.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class SettingsForm : Form
{
public static bool isSettingsEmpty = false;
private FFmpeg_Capture ffmpegCapture;
public SettingsForm()
{
InitializeComponent();
ffmpegCapture = new FFmpeg_Capture();
}
private void SettingsForm_Load(object sender, EventArgs e)
{
}
private void btnWorkingFolder_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
folderBrowserDialog1.Description = "Browse for working folder";
if (result == DialogResult.OK)
{
textBoxWorkingFolder.Text = folderBrowserDialog1.SelectedPath;
ffmpegCapture.workingDirectory = folderBrowserDialog1.SelectedPath;
}
}
private void btnFfmpegFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = #"C:\",
Title = "Browse For Ffmpeg exe File",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "exe",
Filter = "ffmpeg exe file (ffmpeg.exe)|ffmpeg.exe",
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBoxFfmpegFile.Text = openFileDialog1.FileName;
ffmpegCapture.outputDirectory = openFileDialog1.FileName;
}
}
private void btnConrimArguments_Click(object sender, EventArgs e)
{
ffmpegCapture.arguments = textBoxArguments.Text;
btnConrimArguments.Enabled = false;
}
private void textBoxArguments_TextChanged(object sender, EventArgs e)
{
if(textBoxArguments.Text == "")
{
isSettingsEmpty = true;
}
if(textBoxArguments.Text != "" && textBoxWorkingFolder.Text != ""
&& textBoxFfmpegFile.Text != "")
{
isSettingsEmpty = false;
}
btnConrimArguments.Enabled = true;
}
private void textBoxWorkingFolder_TextChanged(object sender, EventArgs e)
{
if(textBoxWorkingFolder.Text == "")
{
isSettingsEmpty = true;
}
if (textBoxArguments.Text != "" && textBoxWorkingFolder.Text != ""
&& textBoxFfmpegFile.Text != "")
{
isSettingsEmpty = false;
}
}
private void textBoxFfmpegFile_TextChanged(object sender, EventArgs e)
{
if(textBoxFfmpegFile.Text == "")
{
isSettingsEmpty= true;
}
if (textBoxArguments.Text != "" && textBoxWorkingFolder.Text != ""
&& textBoxFfmpegFile.Text != "")
{
isSettingsEmpty = false;
}
}
}
}
I'm using a public static variable flag isSettingsEmpty and check if one of the textBoxes is empty make the flag true and if all the textBoxes filled make the flag flase.
but how do i apply the flag isSettingsEmpty state changes to the form1 and change the record button state enabled false/true in real time ?
I want that if one of the textBoxes is empty enabled false the record button.
There are many ways of doing that, one of them is using events.
create custom EventArgs class:
public class RecordingEventArgs : EventArgs
{
public bool Enabled {get; set;}
}
Form1:
public partial class Form1 : Form
{
// you need to subscribe your Form1 to the SettingsForm event
// supposing you are creating and calling it from Form1
public void SettingsShowButton_Click(object sender, EventArgs e)
{
var settingsForm = new SettingsForm();
settingsForm.Notify += NotifiactionHandler;
settingsForm.Show();
}
// handling notification event from SettingsForm
public void NotifiactionHandler(object sender, RecordingEventArgs e)
{
recordButton.Enabled = e.Enabled;
}
}
SettingsForm:
public event EventHandler Notify;
private void textBoxArguments_TextChanged(object sender, EventArgs e)
{
var eventArgs = new RecordingEventArgs();
if(textBoxArguments.Text == "")
{
isSettingsEmpty = true;
eventArgs.Enabled = true;
Notify?.Invoke(this, eventArgs);
}
// ...
}
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 DENEME1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
label1.Text += "1 ";
}
else if (radioButton2.Checked)
{
label1.Text += "2 ";
}
else if (radioButton3.Checked)
{
label1.Text += "3 ";
}
else if (radioButton4.Checked)
{
label1.Text += "4 ";
}
.....
.....
.....
.....
}
}
}
If i have more than 10 radio buttons how can i control which one are selected. I tried it like above but i think that it isn't the correct way. How can i make it better? For example if i have 100 radio buttons how can i get the selected one?
Here is an option
RadioButtons are in panels (border is to show this)
Originally each RadioButton text was radioButtonn
There is no logic here to decide which RadioButton may or may not be checked, only provides a list to work with.
String extension needed
public static class StringExtensions
{
public static string Numbers(this string sender) =>
Regex.Replace(sender, "[^0-9 _]", "");
}
Control extensions
public static class ControlExtensions
{
public static IEnumerable<T> Descendants<T>(this Control control) where T : class
{
foreach (Control child in control.Controls)
{
T thisControl = child as T;
if (thisControl != null)
{
yield return (T)thisControl;
}
if (child.HasChildren)
{
foreach (T descendant in Descendants<T>(child))
{
yield return descendant;
}
}
}
}
public static List<RadioButton> RadioButtonList(this Control control) =>
control.Descendants<RadioButton>().ToList();
public static List<RadioButton> RadioButtonListChecked(this Control control) =>
control.RadioButtonList().Where(rb => rb.Checked).ToList();
}
Form code
public partial class Form2 : Form
{
private List<RadioButton> _checkedRadioButtons = new List<RadioButton>();
public Form2()
{
InitializeComponent();
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
this.RadioButtonList().ForEach(rb =>
{
rb.CheckedChanged += OnCheckedChanged;
rb.Checked = false;
rb.Text = rb.Text.Numbers(); // original names are radioButton1, radioButton2 etc
});
label1.Text = "";
}
private void OnCheckedChanged(object sender, EventArgs e)
{
ProcessChecked(sender);
}
private void ProcessChecked(object sender)
{
if (!(sender is RadioButton radioButton) || !radioButton.Checked) return;
_checkedRadioButtons = this.RadioButtonListChecked();
if (_checkedRadioButtons.Count > 0)
{
label1.Text = string.Join(" ",
_checkedRadioButtons.Select(rb => rb.Text));
}
}
private void RadioButtonsCheckedButton_Click(object sender, EventArgs e)
{
if (_checkedRadioButtons.Any())
{
var checkedList = _checkedRadioButtons.Select(rb => rb).ToArray();
var names = string.Join("\n", checkedList.Select(rb => rb.Name));
MessageBox.Show(names);
}
}
}
The following code dynamically checks whether the control is RadioButton and then fetches the RadioButton name number and displays it on the label.
foreach (Control ctr in this.Controls)
{
if (ctr is RadioButton)
{
RadioButton radioButton = (RadioButton)ctr;
if (radioButton.Checked)
{
//get number in radioButton
string num = "";
foreach(Char c in radioButton.Name)
{
if (Char.IsDigit(c))
num += c;
}
label1.Text += num + " ";
}
}
}
label1.Text += Controls.OfType<RadioButton>().FirstOrDefault(_ => _.Checked)?.Text;
I want to save radio button state and select the radio button again on reloading the page. I have written the following code but it is not working:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Question_1 : System.Web.UI.Page
{
public int index;
public bool flag = false;
protected void Page_Load(object sender, EventArgs e)
{
if(flag)
{
index = (int)Session["index"];
if (index == 5)
{
totallyagree.Checked = true;
}
else if (index == 4)
{
agree.Checked = true;
}
}
}
protected void next_Click(object sender, EventArgs e)
{
flag=true;
if (totallyagree.Checked)
{
Session["index"] = 5;
}
else if (agree.Checked)
{
Session["index"] = 4;
}
Response.Redirect("Question 2.aspx");
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Session["index"] = RadioButtonList1.SelectedIndex;
}
}
Please help me with this issue.
did you try IsPostBack ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
index = (int)Session["index"];
if (index == 5)
{
totallyagree.Checked = true;
}
else if (index == 4)
{
agree.Checked = true;
}
}
}
I wish to know how to store a VALUE to an item in a listbox. I'm currently working out a worksheet for my studies, and I've been wondering how to do this. Basically I need to store values to the Food (such as broccoli, bread. For example: Broccoli has a value of 20 calories).
The user must not see the value, only the program can store it.
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 MCAST_Calorie_Counter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lbAvailable.Items.Clear();
if (comboBox1.Text == "Vegetables")
{
lbAvailable.Items.Add("Broccoli");
lbAvailable.Items.Add("Carrots");
lbAvailable.Items.Add("Lettuce");
lbAvailable.Items.Add("Onions");
lbAvailable.Items.Add("Potatoes");
}
if (comboBox1.Text == "Meat")
{
lbAvailable.Items.Add("Chicken");
lbAvailable.Items.Add("Veal");
lbAvailable.Items.Add("Beef");
lbAvailable.Items.Add("Fish");
}
if (comboBox1.Text == "Legumes")
{
lbAvailable.Items.Add("Bread");
lbAvailable.Items.Add("Peanuts");
lbAvailable.Items.Add("Green Peas");
lbAvailable.Items.Add("Lentils");
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection highlightedItems = lbAvailable.SelectedItems;
foreach (var item in highlightedItems)
{
lbChosen.Items.Add(item);
}
if (lbAvailable.SelectedItems.Count > 0)
{
lbAvailable.Items.Remove(lbAvailable.SelectedItems[0]);
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection highlightedItems = lbChosen.SelectedItems;
foreach (var item in highlightedItems)
{
lbAvailable.Items.Add(item);
}
if (lbChosen.SelectedItems.Count > 0)
{
lbChosen.Items.Remove(lbChosen.SelectedItems[0]);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
lbAvailable.Items.Clear();
lbChosen.Items.Clear();
if (comboBox1.Text == "Vegetables")
{
lbAvailable.Items.Add("Broccoli");
lbAvailable.Items.Add("Carrots");
lbAvailable.Items.Add("Lettuce");
lbAvailable.Items.Add("Onions");
lbAvailable.Items.Add("Potatoes");
}
if (comboBox1.Text == "Meat")
{
lbAvailable.Items.Add("Chicken");
lbAvailable.Items.Add("Veal");
lbAvailable.Items.Add("Beef");
lbAvailable.Items.Add("Fish");
}
if (comboBox1.Text == "Legumes")
{
lbAvailable.Items.Add("Bread");
lbAvailable.Items.Add("Peanuts");
lbAvailable.Items.Add("Green Peas");
lbAvailable.Items.Add("Lentils");
}
}
}
}
Thanks alot for your help!
You can always add custom objects that support ToString() to the ListBox.Items collection:
struct Record
{
int value;
string label;
public override string ToString()
{
return label;
}
}
Adding a custom object:
Record record = new Record();
record.value = 1;
record.label = "This text will appear in the ListBox";
listBox.Items.Add(record);
Retrieving it:
Record selectedRecord = (Record)listBox.SelectedItem;
Console.WriteLine(selectedRecord.value); // => 1
I'm making a quick quiz game in c# and I'm struggling at making the label (questionLabel) and the buttons (ans1 - ans4) display the specified text, when the play button has been clicked, thank you in advance.
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;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int pointCounter = 0;
private SoundPlayer _soundPlayer;
int questionNr = 1;
public Form1()
{
InitializeComponent();
_soundPlayer = new SoundPlayer("song.wav");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.amazon.com/Chuck-Seasons-One-Five-Blu-ray/dp/B007AFS0N2");
}
private void Form1_Load(object sender, EventArgs e)
{
_soundPlayer.PlayLooping();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void muteButton_Click(object sender, EventArgs e)
{
if (muteButton.Text == "Mute")
{
muteButton.Text = "Unmute";
_soundPlayer.Stop();
}
else
{
muteButton.Text = "Mute";
_soundPlayer.PlayLooping();
}
}
private void button1_Click(object sender, EventArgs e)
{
ans1.Visible = true;
ans2.Visible = true;
ans3.Visible = true;
ans4.Visible = true;
playButton.Visible = false;
while (questionNr >= 2)
{
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "fushfus";
ans2.Text = "bhfsfs";
ans3.Text = "Chuck";
ans4.Text = "sfhus";
}
}
}
private void ans3_Click(object sender, EventArgs e)
{
if (questionLabel.Text == "What is Chuck's full name?")
{
pointCounter++;
}
else
{
}
}
public void PointCounter(){
pointsLabel.Text = pointCounter.ToString();
}
}
}
If button1 is the play button then I guess you got it wrong with the while() loop.
It will never enter the while(questionNr >= 2) since questionNr is 1 from when you create the instance.
public partial class Form1 : Form
{
int pointCounter = 0;
private SoundPlayer _soundPlayer;
int questionNr = 1; <--
public Form1() { ... }
}
Change:
while (questionNr >= 2)
{
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "fushfus";
ans2.Text = "bhfsfs";
ans3.Text = "Chuck";
ans4.Text = "sfhus";
}
}
To:
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "fushfus";
ans2.Text = "bhfsfs";
ans3.Text = "Chuck";
ans4.Text = "sfhus";
}