ASP.NET validation, radio button and textbox - c#

Is there a way to change the state of the radio button depending on the value entered by the user in one of the text boxes. I’ve tried this:
protected void Btn_click(object sender, EventArgs e) {
int TextBoxValue = int.Parse(TextBox.Text);
if(TextBoxValue < 18) {
RadioButton.Enabled = false;
} else {
RadioButton.Enabled = true;
}
}
but when I click a button nothing happens.

Related

Display an alert message if a checkbox is uncheck C#

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}");
}
}
}
}

Need help deleting rows/lines in richtextbox c# win forms

So essentially, I have a button, when I press the button, the button should delete the most recent row of text embedded into the rich text box. Press it again, it should delete the next line above that etc.
I tried a using .substring, which it worked, but required me to double click the button instead of a single click..
Furthermore, when I press the add text button it should replace the deleted line. Currently, the delete button button1_click just removes all lines, but all I want the it to do is remove a line of text from the richtextbox.
Edit:
Huge apologies before, I forgot to add the code as mentioned in replies, apologies.
public partial class Form1 : Form
{
string[] texts = new string[10];
int i = 0;
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (TextBox.Text == "")
{
texts[i] = texts[i];
}
else
{
texts[i] += TextBox.Text + "\r\n";
richText.Text += texts[i];
++i;
TextBox.Text = "";
}
}
private void richText_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//i dont know need to do research or im too dumb
richText.Text = "";
for (int i = 0; i < texts.Length; i++)
{
texts[i] = "";
}
i = 0;
//https://stackoverflow.com/questions/22587505/delete-the-last-line-of-rich-text-box
//works, but double click instead of one click
}
}
}
To clarify, the left button adds text from the textbox and the right button is basically the function aforementioned.
enter image description here
Thanks to anyone who helps!

How to enable button after all textboxes are not empty in c# winforms?

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();
}

Populating textBox with value depending on button click count

I am new to coding in C#. Currently I am encountering the problem of populating a textBox depending on the amount of times button1 is clicked. I have been able to populate option one but I am not sure how to get the second option after the second click and so fourth. How would I be able to do that? Also, Would I need to add a loop to start over after the fourth click?
Code
protected void Button1_Click(object sender, EventArgs e)
{
MyTextBox.Text = "Fruits"; //1st click
MyTextBox.Text = "Vegtables"; //2nd click
MyTextBox.Text = "Grains"; //3rd click
MyTextBox.Text = "Poultry"; //4th click
}
private List<string> messages= new List<string>(){"Fruits", "Vegetables", "Grains", "Poultry"};
private int clickCount = 0;
protected void Button1_Click(object sender, EventArgs e)
{
MyTextBox.Text = messages[clickCount];
clickCount++;
if (clickCount == messages.Count)
clickCount = 0;
}

why does the CLEAR button seem to disable my listbox label output?

When I run my program with a listbox everything works after selecting the items and pressing enter, but when I press the clear button and I select the items again and press enter nothing happens. I've tried the following for the clear button and they clear my label text and the selected listbox but I can no longer produce another output when I try pressing enter button again after selecting the items.
public partial class frmLabSix : Form
{
public string strCakes;
public int cakeCost;
public frmLabSix()
{
InitializeComponent();
}
private void lstCakes_SelectedIndexChanged(object sender, EventArgs e)
{
for (int index = 0; index < lstCakes.SelectedItems.Count; index++)
{
strCakes += Environment.NewLine + lstCakes.SelectedItems[index].ToString();
if (lstCakes.SelectedIndices[index] == 0) cakeCost += 18;
if (lstCakes.SelectedIndices[index] == 1) cakeCost += 25;
if (lstCakes.SelectedIndices[index] == 2) cakeCost += 40;
if (lstCakes.SelectedIndices[index] == 3) cakeCost += 30;
}
}
private void lblOrdered_Click(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
double tax = 1.13;
lblOrdered.Text = "You have ordered: " + strCakes + '\n' + "Total Cost: " + (tax * cakeCost).ToString("C");
lblOrdered.Visible = true;
}
private void btnClear_Click(object sender, EventArgs e)
{
lstCakes.SelectedItems.Clear();
lblOrdered.Visible = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
Can someone tell me why this is?
This line of code hides your label.
lblOrdered.Visible = false;
You make your label invisible on clear button click. Do you reset its visibility after?
I don't know, what happens in other part of your code, but it should probably be like this:
// if the label is not visible, the next line won't make it visible implicitly
lblOrdered.Text = ...
//you should set label's visibility explicitly
if (!lblOrdered.Visible)
lblOrdered.Visible = true;
Setting label's text doesn't make it visible. If you hide it with your own code, you should make label visible explicitly as well.

Categories

Resources