Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello for everyone I am new here,sorry if this post doens't match with at all with how should be,because It's my first post this.
I am wanting to change the value of the Label when I choose one CheckBox,but It's seems that the Label just changes when I click in the Label.
I was looking for see if the Label had some Events for make work it but I didn't found one.
The Label it's in another groupBox,It's seems like that :
In one GroupBox has 3 CheckBox and i want that when i choosing one or all from that CheckBox will change the value of the Label in the another GroupBox.
Please guys,help me here,I am really wanting to understand why It's not working.
You can just register an event handler for the CheckChanged event and change the label's caption in the event handler :
public partial class Form1 : Form
{
public MyForm()
{
InitializeComponent();
myCheckbox.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox1.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox2.CheckedChanged += new System.EventHandler(this.checkedChanged);
}
private void checkedChanged(object sender, EventArgs e)
{
myLabel.Text = "Some text";
}
}
use this code here, but change the event to the control you're looking for
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your value here";
}
you can do this by double clicking on the radio button control and then it will generate the event for you and then set your code as per the example here.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Good morning everyone!
So, I have 8 textboxes and under those are checkboxes. To those textboxes I'am always receiving data from a serialport and I need to do an average from those values. But I only want to do the average from the textboxes from which I checked the checkbox under them.
How can I add those values to the average formula when selected by checking the checkbox?
If anyone could help, I would really apreciate.
Thanks,
Jonathan
I think you should write something like this :
bool[] CheckBoxList { get; set; } = new bool[8];
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBoxList[0] = checkBox1.Checked;
CalcAverage();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBoxList[1] = checkBox2.Checked;
CalcAverage();
}
in CalcAverage method , Textboxes with a true value in the list are computed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I would like to pass variable which I insert into textbox in Window to Page in WPF application I only found how could I do it other way.
Basically I need app to prompt for password which I need to use in different page.
I call window from page like this:
Password_Prompt PassWindow = new Password_Prompt();
PassWindow.Show();
It's just window with a textbox and a button and after I input a password and click ok, I would like to send the password into variable on page from I called the window.
The most efficient way to achieve this would be to raise an event when you click the button on the window and subscribe to it from the page.
Window
public event EventHandler<string> PasswordInput;
// the function you are going to call when you want to raise the event
private void NotifyPasswordInput(string password)
{
PasswordInput?.Invoke(this, password);
}
// button click event handler
private void OnButtonClick(object sender, RoutedEventArgs e)
{
// get the password from the TextBox
string password = myTextBox.Text;
// raise the event
NotifyPasswordInput(password);
}
Page
...
Password_Prompt PassWindow = new Password_Prompt();
// add this part to subscribe to the event
PassWindow.PasswordInput += OnPasswordInput;
PassWindow.Show();
...
// and the method to handle the event
private void OnPasswordInput(object sender, string password)
{
// use the password from here
}
You could add a property to PassWindow.xaml.cs that returns the value of the Text property of the TextBox or PasswordBox:
public string Password
{
get { return _passwordBox.Password; }
set { _passwordBox.Password = value; }
}
XAML:
<PasswordBox x:Name="_passwordBox" />
You could then retrieve the password using this property. You probably also want to block the calling thread until the window has been closed. Then you should call ShowDialog() instead of Dialog():
Password_Prompt PassWindow = new Password_Prompt();
PassWindow.ShowDialog();
string password = PassWindow.Password;
The other option would be to handle the Closed event:
Password_Prompt PassWindow = new Password_Prompt();
EventHandler handler = null;
handler = (s, e) =>
{
string password = PassWindow.Password;
PassWindow.Closed -= handler;
};
PassWindow.Closed += handler;
PassWindow.Show();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Everytime I click enter, he deletes the textbox, adds the text of the word into the listbox?
Basically, the code-behind of an Windows Forms App that does what you asked in a REALLY BASIC way, would be the following:
using System.Windows.Forms;
namespace WindowsFormsApp1 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 13) //checks if "enter" was pressed
{
listBox1.Items.Add(textBox1.Text);//adds the text to the list
textBox1.Clear(); //clear the text of the textbox
}
}
} }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i need that when you click a garbage button, the "Selection mode" turn on (A bool variable became true) and when you click that control (let's say a button) the application "selects it" and with another button, (Ok button) the application should knew Wich controls you clicked, and then, delete them, the problem i Have is in the recognize witch controls you selected
update
For "selection" I mean that the app knows which control you clicked
You can implement the required functionality as follows. The idea is simple, to keep track of our last selection we have a "previousSelection" variable of type "Control" in our class. We can even use a list of type "Control" to keep track of multiple selections.
There's a toggle button to enable / disable "Garbage Mode" which maps to "IsGarbageModeEnabled" field of type "bool". Then we have an "InitControlsRecursiveMethod(ControlCollection collection)" which takes in a collection of controls, to which we would attach an event handler, which is "GenericClickHandler(Control c)" in our case. This handler simply updates the "previousSelection" on each button click.
And lastly we have our button "ConfirmDeletionBtn", upon clicking which, we would check whether "GarbageMode" is enabled or not, if it is, we would have some basic validation to check whether the control being deleted isn't our "Delete" or "GarbageModeToggle" button itself (this could cause trouble if user double clicks the 'Delete' button). Afterwards, it would remove / dispose the control that is to be deleted.
public partial class FormName : Form
{
//To keep track of the previously selected control (i.e. to be deleted)
private Control previousSelection { get; set; }
//To keep track of whether "Garbage Mode" is enabled or disabled
private bool IsGarbageModeEnabled { get; set; }
//Constructor
public FormName()
{
InitializeComponent();
IsGarbageModeEnabled = false;
previousSelection = new Control();
//Attach a generic click handling event to each control to
//update "previousSelection" with each click.
//Similar logic can be used for other events as well
//(e.g. GotFocus, which might even accomodate control selection via keyboard).
InitControlsRecursive(this.Controls);
}
//This attaches the GenericClickHandler(Control c) to each control on the form.
private void InitControlsRecursive(Control.ControlCollection collection)
{
foreach (Control c in collection)
{
c.MouseClick += (sender, e) => { GenericClickHandler(c); };
InitControlsRecursive(c.Controls);
}
}
//The generic click handling event we're using to update "previousSelection".
private void GenericClickHandler(Control c)
{
previousSelection = c;
}
//By clicking the confirm deletion / OK button, we would delete the last selected control.
private void ConfirmDeletionBtn_Click(object sender, EventArgs e)
{
if(IsGarbageModeEnabled == true)
{
if(previousSelection != ConfirmDeletionBtn || previousSelection != ToggleGarbageModeBtn)
{
this.Controls.Remove(previousSelection);
previousSelection.Dispose();
}
}
}
//This is used to enable/disable Garbage Mode. Controls can be deleted only once it is enabled.
private void ToggleGarbageModeBtn_Click(object sender, EventArgs e)
{
IsGarbageModeEnabled = !IsGarbageModeEnabled;
}
}
Further Reading:
Handling a click for all controls on a form
Removing a control from a form
Firstly you need somewhere to keep track of the controls that were "selected" (clicked). So add this to the form's codebehind:
List<Control> _itemsToDelete = new List<Control>();
And you need a flag to indicate whether the user has activated garbage mode:
bool _garbageMode = false;
To activate garbage mode:
async void GarbageMode_Click(object sender, EventArgs e)
{
_garbageMode = true;
}
Now when they "select" a control you add it to the list:
async void Control_Click(object sender, EventArgs e)
{
if (_garbageMode)
{
_itemsToDelete.Add((Control)sender);
}
}
Then to delete
foreach (var control in _itemsToDelete)
{
this.Controls.Remove(control);
control.Dispose();
}
_itemsToDelete.Clear();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I made a timer, but when I press a button nothing happens.
My code is:
private void button3_Click(object sender, EventArgs e)
{
Instellingen form = new Instellingen();
form.Show();
}
Instellingen.cs code:
public partial class Instellingen : Form
{
public Instellingen()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("text", "text");
}
}
}
The button3 event doesn't even fire (confirmed by adding a breakpoint, it doesn't get there)
Check if the button's event realy connected to the method button3_Click.
Put a breakpoint in the first line of button3_Click and check if the code get there.
Make sure your button's click event is button3_Click. Probably your button's click event is empty.