When I click on my radio buttons I would like the textblock1.text to update. This is what I have and thought it would work but it doesnt. This is for visual studio 2010express
private void changetitle(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked==true)
textBlock1.Text = "Celsius";
textBlock3.Text = "Farenheight";
if(radioButton2.IsChecked==true)
textBlock1.Text = "Inch";
textBlock3.Text = "cm";
}
this on the otherhand does work but ONLY after I press my button (which I want the title to change before hand so the user knows what they are entering (inches or cm and where they should put them)
private void button1_Click(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked == true)
{
textBlock1.Text = "Celsius";
textBlock3.Text = "Farenheight";
CalcDegrees();
}
if (radioButton2.IsChecked == true)
{
textBlock1.Text = "Inch";
textBlock3.Text = "cm";
Calcinch2cm();
}
}
Any input or help would be greatly appreciated please, thank you. V/r
You have to wire an event handler to the onChanged event for the radio buttons. In this event handler, you can change the text.
Also, if this is an ASP.NET application, insure that the radio buttons have runat="server" otherwise the event wiring will not function.
Related
Im making a calculator.and for the buttons that type numbers, I wrote a condition that if the focus was on text box 1, it would enter the text there, if not, it would enter text box 2. But unfortunately the code does not work and I dont understand the problem.
(WindosForm(.Net framework))
if (textBox1.Focus() == true)
{
textBox1.Text = textBox1.Text + "1";
}
else
{
textBox2.Text = textBox2.Text + "1";
}
Subscribe to "Enter" event for your two textbox and save it. Use the same method for the two textboxes.
TextBox focusedTB;
private void textBox_Enter(object sender, EventArgs e)
{
focusedTB = sender as TextBox;
}
...
this.textBox1.Enter += new System.EventHandler(this.textBox_Enter);
...
this.textBox2.Enter += new System.EventHandler(this.textBox_Enter);
Now you know the last textbox that got focus.
private void button1_Click(object sender, EventArgs e)
{
focusedTB.Text += "1";
}
Your code appears to be attempting to check if the control is focused. The correct way to do that is:
if (textBox1.Focused)
{
// Because 'Focused' is a property. 'Focus()' is a method.
textBox1.Text = textBox1.Text + "1";
}
.
.
.
The answer to your question Why I can not change focus? is that textBox1 receives the focus every time you call this:
if (textBox1.Focus())
As mentioned in one of the comments, here's how the Focus method works:
// Summary:
// Sets input focus to the control.
//
// Returns:
// true if the input focus request was successful; otherwise, false.
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool Focus();
Note: This is a copy-paste of metadata that you can look at by right-clicking over Focus() in your code and selecting Go to Definition then expanding the definition.
I think you talk about Windows Form ?
You cannot manage like this but use event "Enter" of your textboxes, when you click inside the textbox, you give the focus to this textbox and you can do anything inside. Here I put the right focuses TextBox in a variable.
private TextBox _textBoxFocused; //this is always the righ TextBox
private void textBox1_Enter(object sender, EventArgs e)
{
_textBoxFocused = textBox1;
}
private void textBox2_Enter(object sender, EventArgs e)
{
_textBoxFocused = textBox2;
}
i have 46 button in form and all of them to same work just diffrent in value.
mean button1 plus 1 to sum , button2 plus 2 to sum ...
is there a way to understand which one of buttons are pressed to get it Text
private void button1_Click(object sender, EventArgs e)
{
// call a function with this button TEXt
// if button 1 selected -> func("1")
// if button 2 selected -> func("2")
}
is there any way do it ?
sender returns your button.So you can get the button like
Button clicked = (Button)sender;
In specifying the buttons, you can check that sender button's Text or you can give them Tag and check them. You said text, so
Button clicked = (Button)sender;
func(clicked?.Text);
should work.
Button clickedButton = sender as Button;
if (clickedButton != null)
{
button.Name....
}
The most straightforward solution is to create for each button an event_click (so button1_Click, button2_Click etc. Better to name the buttons appropriate.
Than from within each event handler, call the same function to add the number to the sum e.g.:
private void button1_Click(object sender, EventArgs e)
{
add(1);
}
private void button2_Click(object sender, EventArgs e)
{
add(2);
}
// Same for other event handlers.
private void add(int number)
{
sum += number;
}
You can link all your event handlers and then use the Name property to decide which button was clicked.
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += this.button1_Click;
private void button1_Click(object sender, EventArgs e)
{
var btn = sender as Button;
switch(btn.Name.ToLower())
{
case "button1":
MessageBox.Show("Add 1");
break;
case "button2":
MessageBox.Show("Add 2");
break;
default:
MessageBox.Show("Button not found");
break;
}
}
I want to know how to output to a TextBox as soon as a user has clicked on a series of RadioButtons and clicked the CheckBox(es) which are found inside various GroupBoxes on the Form.
Any help will be really appreciated, in case this question has already been answered in the past let me know I have search for it but could not find anything like this.
Sample Form layout:
I am no good at chasing the pictures and especially code as picture doesn't help anyone. Anyway next time please don't do that.
First, for all of your radio and checkboxes (radChocolate, radVanilla, ... radSmall, ..., chkChocoChips, ...) double click and fill Checked event such as:
private void radChocolate_CheckedChanged(object sender, EventArgs e)
{
CalculatePrice();
}
private void radVanilla_CheckedChanged(object sender, EventArgs e)
{
CalculatePrice();
}
// Do the same for other radio and checkboxes
Then add the CalculatePrice code as such (prices are arbitrary):
private void CalculatePrice()
{
decimal price = 0M;
if (radChocolate.Checked) price += 75M;
if (radVanilla.Checked) price += 65M;
if (radStrawberry.Checked) price += 55M;
if (radSmall.Checked) price += 20M;
if (radLarge.Checked) price += 30M;
if (chkChocoChips.Checked) price += 5M;
if (chkCookieCandy.Checked) price += 4M;
if (chkNuts.Checked) price += 3M;
if (chkFreshFruits.Checked) price += 2M;
txtPrice.Text = price.ToString("C");
}
This would do what you wanted to.
You either need to create an event handler for each radio button, or create a single event handler for all the radio buttons. It would depend on what you are trying to accomplish. For the radio button you would want to subscribe to the CheckedChanged event. Then inside this event you can change the text box.
private void radioButtonChangeText_CheckedChanged(object sender, EventArgs e)
{
//Code here to change text box or call sub
textBox.Text = "Hello world";
}
Based upon your link, you can create one event handler and bind it to all the events. (Link explaining binding)
So, every time any value is changed in your form, only one function gets called.
Then, check the values of every component present in your form and calculate value of your textbox.
Right click on a radio button and the go to properties, there click on "events" (that lightning sign). There is an event there called "CheckedChanged". Double click on the cell next to it to generate the event method.
it will generate a code like this,
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("hi there");
}
you should be able to put any thing you want in there. Assuming you want to show hide the TextBox, you can do it in there.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
setCheckBoxValue();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
setCheckBoxValue();
}
private void setCheckBoxValue()
{
int finalPrice = 0;
if (radioButton1.Checked == true)
{
finalPrice = finalPrice + 75;
}
else if (radioButton2.Checked == true)
{
finalPrice = finalPrice + 87;
}
textBox1.Text = finalPrice.ToString("C");
}
I need to dynamically create buttons (one for loop) and add "onClick" and "doubuleClick" events on it.
I did it like this:
Button bt = new Button();
bt.Click += bt_Click;
bt.DoubleClick += bt_DoubleClick;
private void bt_Click(object sender, EventArgs e)
{
label1.Text = this.Text;
}
private void bt_DoubleClick(object sender, EventArgs e)
{
//some code
}
First: My "bt_Click" method gets "main form" text in "label1". In debugger I see that sender is a button. What is wrong with it?
Second: My "bt_DoubleClick" event do not react at all, am I doing something wrong here?
Any help is appreciated.
You should cast sender to Button to get the bt.Text:
Button bt = new Button();
bt.Click += bt_Click;
bt.Text = "click me";
bt.Location = new Point(100,100);
this.Controls.Add(bt);
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (sender as Button).Text;
}
Buttons doesn't react to double click event. You can read it here in detail.
In response to the first question, if I understand you correctly, in this.Text, this refers to the form because the method bt_Click is a member of the Main Form class. I think you might have meant to do:
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (Button)sender.Text;
}
Second: Is this just a case of the bt_Click handler firing twice?
The easiest way to do this it is to use "datagrid".
Datagread has the great support for all events and for organization of items (image, text and so on).
I have made "save" or "open" dialog form to browse content from remote SFTP server, very easy with datagrad, but I had a problem to do it with buttons or labels.
I'm making a windows forms application using C#. I add buttons and other controls programmatically at run time. I'd like to know how to handle those buttons' click events?
Try the following
Button b1 = CreateMyButton();
b1.Click += new EventHandler(this.MyButtonHandler);
...
void MyButtonHandler(object sender, EventArgs e) {
...
}
Use this code to handle several buttons' click events:
private int counter=0;
private void CreateButton_Click(object sender, EventArgs e)
{
//Create new button.
Button button = new Button();
//Set name for a button to recognize it later.
button.Name = "Butt"+counter;
// you can added other attribute here.
button.Text = "New";
button.Location = new Point(70,70);
button.Size = new Size(100, 100);
// Increase counter for adding new button later.
counter++;
// add click event to the button.
button.Click += new EventHandler(NewButton_Click);
}
// In event method.
private void NewButton_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
for (int i = 0; i < counter; i++)
{
if (btn.Name == ("Butt" + i))
{
// When find specific button do what do you want.
//Then exit from loop by break.
break;
}
}
}
If you want to see what button was clicked then you can do the following once you create and assign the buttons. Considering that you create the button IDs manually:
protected void btn_click(object sender, EventArgs e) {
Button btn = (Button)sender // if you're sure that the sender is button,
// otherwise check if it is null
if(btn.ID == "blablabla")
// then do whatever you want
}
You can also check them from giving a command argument to each button.
Check out this example How to create 5 buttons and assign individual click events dynamically in C#
seems like this works, while adding a tag with each element of the array
Button button = sender as Button;
do you know of a better way?
In regards to your comment saying you'd like to know which button was clicked, you could set the .Tag attribute of a button to whatever kind of identifying string you want as it's created and use
private void MyButtonHandler(object sender, EventArgs e)
{
string buttonClicked = (sender as Button).Tag;
}