How to Make Form1 Label.Text Change When Checkbox on Form2 Is Checked

How to make Form1 label.text change when checkbox on form2 is checked?

You can subscribe to the event CheckedChanged of the checkbox in the Form2 instance, directly from the Form1 instance.
Inside Form1, just before displaying the Form2 subscribe to the CheckedChanged event of the checkbox

Form2 frm = new Form2();
frm.chkBox1.CheckedChanged += new EventHandler(this.ReceiveCheckedChanged);
frm2.ShowDialog();

and then define in Form1 (this) the handler for the checkedChanged event raised in Form2

private void ReceiveCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;
if(chk.Checked)
this.label1.Text = "Checked";
else
this.label1.Text = "UnChecked";
}

for this to work you need to change the property Modifiers on the checkbox from Private to Public

In this way your Form2 doesn't need to know that there is a Form1 and that every time someone clicks on the checkbox you need to change a label in another form. The responsability to change its internal state (the text on the label) is on Form1 who has notified the system of its requirements.

c# using checkbox from form2 in form1

I managed to do this by making the checkboxes save values in the properties settings default and then calling them that way as every time i wanted to open the program instead of having to click them again it's auto saved my current values.

Thank you all for your help though.

Here is some code for future reference if anyone else wanted to do it the way I did.

Here is the code that saves it to the settings.

            if (cbTESTING.Checked)
Properties.Settings.Default.cbTESTING = true;
else
Properties.Settings.Default.cbTESTing = false;

and here was the code to call that in a different form.

                if (Properties.Settings.Default.cbTESTING == true)
{
uri_domains += string.Format("{0}.testing,", word);
}

Hope this will help someone in the future!

Using Another Form to Show Data with Checkboxes C#

In Form1 you would have this:

var form2 = new Form2();
form2.ShowDialog();

if (form2.Check1Checked)
{
label1.Text = "Check1 is checked on form2";
}

if (form2.Check2Checked)
{
label2.Text = "Check2 is checked on form2";
}

form2.Dispose();

On Form2 set up some properties to expose the checkbox's checked value. This way you don't have to expose the entire control.

// C# v5.0 or earlier
public bool Check1Checked
{
get { return checkBox1.Checked; }
}
// C# v6.0
public bool Check2Checked => checkBox2.Checked;

Changing the Label1.Text of Form1 from Form2 Button1_Click in C#

There are several ways you can do this. Passing the form or the control/variable you want to access is probably the best approach as it does not unnecessarily expose the passed control/variable and everything can be kept private.

However, this can be simplified if you choose to expose the control/variable in the first form. In this example it appears that the Label control label1 in form1 is what we want to have access to in form2. Therefore, a very simple approach is to take advantage of the Forms Owner property and it should work as described below.

First, we want to expose form1’s Labellabel1 control by setting its Modifier property to public.

Then when Form1 creates and shows Form2… we want to pass Form1 to Form2 by taking advantage of the Forms Owner property which is done by simply passing the Owner form to Form2 when Form2’s Show or ShowDialog is called. Something like below….

Form2 f2 = new Form2();
f2.Show(this);

Note the this as a parameter when calling the Show/ShowDialog method. This is all we need to do in the first form. The parameter this in the Show/ShowDialog will set the called forms Owner property to a generic Form which we can later cast to a Form1 object in the called form.

Then in the second Form2 when we want to get access to Form1s publicly exposed label1… we simply have to cast form2’s Owner property to a Form1 object since the Owner is a “generic” Form object. So, this would look something like…

private void button1_Click(object sender, EventArgs e) {
((Form1)Owner).label1.Text = "Hello from form2";
}

That is all that is needed.

How to change user control label.text from a form

I think you must have two instances of ModbusMaster.

One of them is the one you can see on the display, and is NOT being updated.

The other one is the one you create in class Form1 with the line of code:

ModbusMaster mb = new ModbusMaster();

That is the one you are modifying, but it isn't the displayed one (I cannot see anywhere that you can be displaying that).

What you need to do is use the reference to the actual displayed one instead when you call mb.openPort("wooooo");

[EDIT]

Thinking about it - it's possible that you haven't instantiated another user control at all.

Did you use Visual Studio's Form Designer to add the user control to your main form? I had assumed that you did, but now I realise that might not be the case.

If not, you should do that, give it the name mb and remove the line that says ModbusMaster mb = new ModbusMaster(); and it might work without you having to make more extensive changes.

Change the div content of a form on checkbox select?

Here I made this for you

var form1 = document.querySelector("#f1"),    form2 = document.querySelector("#f2"),    check1 = document.querySelector("#checkbox1"),    check2 = document.querySelector("#checkbox2");
check1.onchange = function() { form1.classList.toggle("hidden");}check2.onchange = function() { form2.classList.toggle("hidden");}
.hidden {  display: none;}
<input type="checkbox" name="checkbox1" id="checkbox1" value="checkbox1" checked/> First
<br />
<input type="checkbox" name="checkbox2" id="checkbox2" value="checkbox2" checked/> Second
<form id="f1"><div class="form-row">
<div class="form-group col-md-6"> <label for="first">Name</label> <input type="text" class="form-control" id="name" name="name" required> </div> <div class="form-group col-md-6"> <label for="">Phone</label> <input type="text" class="form-control" id="phone" name="phone" required> </div></div></form>
<form id="f2"><div class="form-row">
<div class="form-group col-md-6"> <label for="inputCity">Address</label> <input type="text" class="form-control" id="address" name="address" required> </div> </div></form>

Show/Hide the part of the form based on checkbox selected?

I have added two options first in vanila js and second one is in jquery

    /*---- Plain JS----*/
check1.onchange = function() {
if(this.checked){
document.querySelector("#address").closest('.form-group').style.display = "none";
}
else{
document.querySelector("#address").closest('.form-group').style.display = "";
}
}

check2.onchange = function() {
if(this.checked){
document.querySelector("#name").closest('.form-group').style.display = "none";
document.querySelector("#phone").closest('.form-group').style.display = "none";
}
else{
document.querySelector("#name").closest('.form-group').style.display = "";
document.querySelector("#phone").closest('.form-group').style.display = "";
}
}

/*----Jquery----*/
$(document).on('change','#checkbox1',function(){
if($(this).is(":checked")){
$('#address').closest('.form-group').hide();
}
else{
$('#address').closest('.form-group').show();
}
});

$(document).on('change','#checkbox2',function(){
if($(this).is(":checked")){
$('#name').closest('.form-group').hide();
$('#phone').closest('.form-group').hide();
}
else{
$('#name').closest('.form-group').show();
$('#phone').closest('.form-group').show();
}
});

https://jsfiddle.net/1zu0nsjr/

Respective form should display only when i check box

The problem is that your script executes before the elements are present in the DOM. See:

//when this line executes:
$(".formGroup").hide(); //the .formGroup is not present in the DOM yet
//in other words, the jQuery object is empty and calling .hide() does nothing

//the same happens to this handler binding:
$("#chooseForm input:checkbox").on("change", ...

Your fiddle's JS is set to use the onDomReady wrapper, that's why it works properly there.

You can put the script below the form's HTML, and/or simply wrap the script contents inside a DOM ready handler:

$(function() {
$(".formGroup").hide();
$('#chooseForm input:checkbox').on('change', function() {
if($(this).is(':checked')) {
$("#" + $(this).val()).show();
}
else {
$("#" + $(this).val()).hide();
}
});
});

One of jQuery's best practices to always wrap your code in a DOM ready handler, that is:

$(document).ready(function() {
//code here
});

or the much shorter shorthand:

$(function() {
//code here
});

Both have the same effect, they prevent the code from executing until the DOM is ready.

Another JavaScript best practice is to put the JS code in the footer of the page (right before the </body>), this way scripts loading won't block the page rendering and when the DOM parser reaches the scripts, your DOM will be already ready for them.

Both best practices above are combinable. Nevertheless, simply using the DOM ready handler will solve the issue, as well as moving the scripts to below the form HTML will also suffice.


Your framework is already loading another copy of jQuery in noConflict mode, that means you can't use the $ alias in the global scope. To remedy that, you can use this special DOM ready syntax, which will alias jQuery back to $ inside its scope:

jQuery(function($) {
$(".formGroup").hide();
$("#chooseForm input:checkbox").on("change", function() {
if($(this).is(":checked")) {
$("#" + $(this).val()).show();
}
else {
$("#" + $(this).val()).hide();
}
});
});

More info on that from the DOM ready handler docs - Aliasing the jQuery Namespace:

When using another JavaScript library, we may wish to call
$.noConflict() to avoid namespace difficulties. When this function is
called, the $ shortcut is no longer available, forcing us to write
jQuery each time we would normally write $. However, the handler
passed to the .ready() method can take an argument, which is passed
the global jQuery object. This means we can rename the object within
the context of our .ready() handler without affecting other code.

How do I disable a Checkbox Control in Form1 from Form2 by checking a Checkbox Control

You can simply do it as follows:

  1. Define a new boolean setting (e.g., myFlag) in your application settings (if you do not know how to do it, please leave a comment below this answer and I'll guide you how to do that)
  2. In the properties window of Checkbox1, under the (Application Settings) section, bind the Enabled property of this checkbox to myFlag (again, let me know if you need more help on this step)
  3. Double click on the second checkbox in Form2 (I've assumed that its name is ch2) to write the CheckedChanged handler of this checkbox as follows:
    Private Sub ch2_CheckedChanged(sender As Object, e As EventArgs) Handles ch2.CheckedChanged

My.Settings.flag = Not ch2.Checked

End Sub

Finished!

Please leave comments if you have any question on each step.

Details on Step 2: Binding a Property

  1. Click on CheckBox1 to select it and the press F4 to open the Properties window.
  2. Click the + sign beside the (Application Settings) row to expand it as follows:
    Sample Image
  3. Select the PropertyBinding to open the ```Application Settings for 'CheckBox1'" window:
    Sample Image
  4. Scroll down to reach to the Enabled property and then open its dropdown list and select myFlga which you've defined in your application:
    Sample Image

Done!



Related Topics



Leave a reply



Submit