Automatically updating a textbox after a radio button is selected in C# - c#

I've been watching some vids and reading some stackoverflow pages on radio buttons, but I can't find the magic solution I'm looking for.
The answers I've seen on SO generally show you how to create a group of radio buttons and then you have to press a button to update a text field. I'm using italics just to draw attention to that aspect of what I don't want.
Basically, I'm creating a simple program in which someone puts in two complex numbers into two different fields, and then selects a radio button to add, multiply, subtract or divide. I want the operator field to auto-update whenever a radio button is pressed.
Can someone give me a tip or two? I'm using C#.
TYIA.

It sounds like you need to subscribe to the check_changed event of the radio button.
rbSum_CheckChanged(object sender, Eventrgs e)
{
if(rbSum.Checked == true)
{
txtField.Text = "+";
}
}
rbSubtract_CheckChanged(object sender, Eventrgs e)
{
if(rbSubtract.Checked == true)
{
txtField.Text = "-";
}
}

Related

If statement that targets multiple buttons C# ASP.NET

How do I make an if statement that targets multiple buttons in ASP ?
For example, I want to make an if statement for 52 buttons to have green color, and when I click on them a textbox should get the text of the button and finally after clicking on the send button, one of the buttons should turn red. This is all done with a small table in SQL server management.
NOTE: The last two textboxes are just for cosmetic purposes. The textbox that needs to get the text or number of the button is the "Broj Sedista" field.
You can try making a single event for all those buttons.
protected void btn_Click (object sender, EventArgs e){
Button btn = sender as Button;
if(btn.Background == Color.green){
textFieldName.Text = "Your text";
}
}
The sender argument represents the control who started the action, so you can convert it to a button and access its properties.

Autopopulate text box based on Radio Button checked in C#?

I'm just curious if there is an easy way to have my text box auto populate a string depending on whether a radio button is clicked.
For example, I have three radio buttons: residential, commercial, industrial
and I have a text box called txtCustomerType
I'm using the input from txtCustomerType to log the info to a list, but right now the user has to manually add R,C or I. The radio buttons are being used for the charge calculation (it's a little program that has different rates depending on customer)
You can use event handlers for this.
Under properties for the item you want, you'll find event handlers here:
Then just double click the event you want, and all the necessary stuff will be automatically created for you.
And here is where you want to write the code what happens when e.g. the checked status changes:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
textBox1.Text = "Radio button 1 checked";
}

How do I highlight the selected item (tab index location) in my windows form application?

I want to be able to highlight the selected item on my page because it's busy and there is a lot going on. The user will not have access to their mouse, only keyboard so currently they tab through buttons quickly and enter in to what they need to do (it's a fast data entry sort of app if you must know).
I want to be able to highlight the selected button (so when you tab through currently it will select a button but it isn't very noticeable, it just has a slight border around it when selected).
I know that you can use a focusEnter and focusLeave event, but I would like to avoid that if at all possible just because there are so many buttons on the page that I would have to have a ton of repetitive events with almost the same code.
You can and should use just two common event handlers for Enter and Leave events for all your buttons!
Use the sender param to access the buttons:
private void buttons_Leaveobject sender, EventArgs e)
{
((Button)sender).BackColor = SystemColors.Control;
((Button)sender).ForeColor = SystemColors.ControlText;
}
private void buttons_Enter((object sender, EventArgs e)
{
((Button)sender).ForeColor = SystemColors.Control;
((Button)sender).BackColor = SystemColors.ControlText;
}
Use your own ideas about how to highlight the focussed button; this is a bit excessive imo..:
Of course Button with FlatAppearance can do the highlighting all by themselves as they have separate Colors for their states.
I would suggest creating your own class derived from Button, and then handling the background painting yourself. That would allow you to play with the background look/color and/or the border effects.

Tab index is not working on radio buttons

This is the part of my form that I am asking about
This is the tab index:
The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No
please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.
could you help me please?
Notice
both radio buttons has TabStop property to True
From How to: Set the Tab Order on Windows Forms (MSDN):
A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.
In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).
To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.
As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.
It worked for me!
first you have to create a method like this:
private void TabStopChanged(object sender, EventArgs e)
{
((RadioButton)sender).TabStop = true;
}
and then, put this in your Form_Load event:
private void Form_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(RadioButton))
((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
}
}
For radio buttons, you don't have to use Tab to navigate. Just use right and left keys to traverse radio buttons.
Check out this link to read more - https://www.csun.edu/universal-design-center/web-accessibility-criteria-tab-order

How do I specify the default button of a TextBox that is referred to by AcceptsReturn?

I have a windows form with a tool strip on it containing a text box and some buttons (think browser url bar with a go button, back, forward)
I want pressing enter to activate the goButton just as clicking it would, which I believe is what TextBox.AcceptsReturn = false is for.
I don't see anything that seems to fit the bill for "tell me what button on the form is the one that we will activate".
What am I missing?
A Form has a default button, but a specific control does not (out of the box anyway).
In your scenario, I would probably handle invoking the goButton.Click event by monitoring the keys pressed waiting for the Enter key to be pressed.
The easiest way is to set the forms "Accept Button" to the button control you want. This can be done in the designer.
I know this is an Old Question, but for someone who might to to lazy or just a beginner ,
handler might look like too much work ( though it isn't really )
But there is an easier work around for this,
you can make a panel for each of them ( 1 Textbox and 1 Button for Example ) , and set the Defaultbutton for Each panel as you need.
I used this for my site, where I had several Ajax panel , and I Wanted Each to have their own search box on different subjects and work with Enter Button.
Looks like an old question, but I will provide my solution.
private void ChangeDefaultButton()
{
if (this.TextBox.Focused)
{
this.AcceptButton = button;
}
else
{
this.AcceptButton = button1;
}
}
And then add this method to the Focus Events of the text boxes. Like...
private void TextBox_Enter(object sender, EventArgs e)
{
ChangeDefaultButton();
}
And
private void TextBox_Leave(object sender, EventArgs e)
{
ChangeDefaultButton();
}

Categories

Resources