using checkbox to enable textbox - c#

i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:
private void Register_Visitor_Load(object sender, EventArgs e)
On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:
textbox1.enabled = false;
i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:
CheckState state = checkBox1.CheckState;
switch (state)
{
case CheckState.Checked:
{
textBox1.Enabled = true;
break;
}
case CheckState.Indeterminate:
case CheckState.Unchecked:
{
break;
}
now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!

You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled
private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}

you are placing code at wrong event.
Instead of placing in pageload place that code on chekchange event of checkbox.
That will help you.
private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
textBox1.Enable=true;
}
else
{
textBox1.Enable=false;
}
}

Place the above code inside the function which handles the event for check box.
In your case it is checkchanged status.

You can try this:
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Enabled = false;
}
else
{
textBox1.Enabled = true;
}
}

I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}

Related

Action perform button visible false does not work

Well, I have this situation, in a program I put a Button whose code is activated with PerformClick (programmatically), that button must be invisible in the interface so I put the value visible=false since the beginning of the program but the action on the event click doesn't perform, but if I put visible = true, the action actually is performed, any ideas of the problem?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
_btnRecargarInsumos.PerformClick();
}
this.Close();
}
_btnRecargarInsumos: is the button and is actually performed in another Form.
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
cbACDescripcion: Combobox which will be "reloaded" with the values of the DataSet: dsDescripciones.
The property visible is false since the beginnig of the program, but I also try to set visible=true and just before the method PerformClick() change it, but is the same.
But if I put visible=true since the beginning it works in that way.
If you click a button that's not visible or not enabled, nothing happens, even if you click it programmatically. Here's a workaround that works for me, although it's a bit of a hack:
_btnRecargarInsumos.SuspendLayout();
_btnRecargarInsumos.Visible = true;
_btnRecargarInsumos.PerformClick();
_btnRecargarInsumos.Visible = false;
_btnRecargarInsumos.ResumeLayout();
Why not just put your code in a separate method?
Example:
private StuffToDoAtClick()
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
//Your Button.Click() code//
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
StuffToDoAtClick()
}
//Your Datagridview code//
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
StuffToDoAtClick();
}
this.Close();
}

Remeber Me C# app setting

I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}

How to create RowHeadersVisible changed event for DataGridView?

I have a Windows Form, DataGridView and two buttons.
When I will press the button1 it changes a value of RowHeadersVisible to true.
When I will press the button2 it changes a value of RowHeadersVisible to false.
public Form1()
{
InitializeComponent();
dataGridView1.RowHeadersVisible = false;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = true;
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = false;
}
I cannot find any kind of events about "RowHeadersVisible" value changing in "DataGridView" class. As I mentioned "CellFormatting" event works for this action but it appears often, almost for all kind of action made in datagridview1.
I think we might create a custom event handler in order to make different decisions.
When "RowHeadersVisible" changes the value to false I need to call another function inside "CustomEvent".
private void CustomEvent(object sender, EventArgs e)
{
SomeFunction();
}
On the other hand "DataGridTableStyle" class has the event "RowHeadersVisibleChanged".
So How to solve this problem?
.NET 4.5, you should get help.
Dissolve it in the following way.
Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.
// Instantiate the EventHandler.
public void AttachRowHeaderVisibleChanged()
{
myDataGridTableStyle.RowHeadersVisibleChanged += new EventHandler (MyDelegateRowHeadersVisibleChanged);
}
// raise the event when RowHeadersVisible property is changed.
private void MyDelegateRowHeadersVisibleChanged(object sender, EventArgs e)
{
string myString = "'RowHeadersVisibleChanged' event raised, Row Headers are";
if (myDataGridTableStyle.RowHeadersVisible)
myString += " visible";
else
myString += " not visible";
MessageBox.Show(myString, "RowHeader information");
}
// raise the event when a button is clicked.
private void myButton_Click(object sender, System.EventArgs e)
{
if (myDataGridTableStyle.RowHeadersVisible)
myDataGridTableStyle.RowHeadersVisible = false;
else
myDataGridTableStyle.RowHeadersVisible = true;
}

How to make a textbox invisible when radio button checked

I am working on C# application. I have 10 radio buttons in a group panel, so now if I only checked the radioButton10 then textBox1 will be visible, if I checked someone of the other radio Buttons (radioButton1 .... radioButton9) then textBox1 should be invisible.
I wrote the following code but the textBox1 is still visible. If the code is right where can I wrote it(form load, some function ... etc) if it's NOT, then Please Help.
public TeamInfoForm()
{
InitializeComponent();
showTeam();
if (radioButton10 .Checked)
textBox1 .Visible = true;
else
textBox1 .Visible = false;
}
I think you forgot to implement the event that occur when you check or uncheck the radiobutton. Try to imlpement "OnCheckChanged" event for the radiobutton and you have to set autopostback to true if you want that the event occurs, otherwise the event won't work.
Initially you have to set the Visible property of textBox1 to false in Forms Designer. Otherwise you can set it in FormInitialize() method. Next you have write code like below
public void ToggleTextBox()
{
textBox1.Visible = radioButton3.Checked;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}

in c#, how can you disable a button after the 1st click, and enable it again after you click another button?

I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}

Categories

Resources