Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I create a new TextBox by clicking an enter button? And how do I focus the cursor on the new textbox (the cursor focus on the last textbox every time the new textbox has been created)?
I has been tried this code:
"private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Enter += new EventHandler(textBox1_Enter);
}
private void textBox1_Enter(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Size = new Size(100, 50);
tb.Location = new Point(100, 100);
Controls.Add(tb);
}"
But it not create a new textbox when i press an enter button
When you load your form:
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
then
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
TextBox textbox = new TextBox();
this.Controls.Add(textbox);
textbox.Focus();
}
}
Essentially, create the event that fires when a key is pressed. If the key is the return key, create the textbox.
Your sample code doesn't really make sense because you are firing events from a textbox, when you want them fired from the form.
TextBox myTextBox=new TextBox();
myTextBox.Visible=true;
myTextBox.Left=100;
myTextBox.Top=200;
myTextBox.Text="ABC";
this.Controls.Add(myTextBox);
As for moving the cursor:
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(this.Left+myTextBox.Left+50,this.Top+myTextBox.Top+50);
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 8 days ago.
Improve this question
How is it possible to use a var after "this."
i want to set the Background color of a TextBlock to Blue.
Brush blue = new SolidColorBrush(Colors.LightBlue); //Working Fine
//Click on the TextBlock (Working Fine)
private void MouseDown_TextBlock(object sender, MouseButtonEventArgs e)
{
var stand = ((TextBlock)sender).Name; //Here i am getting the Name of the TextBlock (Working Fine)
this.stand.Background = blue; //This is not working here
}
If you want to access the control you receive as Sender in an event, just cast it to the appropriate class:
private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get sender as TextBlock
TextBlock standTextBlockFromSender = (TextBlock)sender;
standTextBlockFromSender.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}
If you want to find the control by name, you can use the FindName method.
private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get name of sender
string stand = ((TextBlock)sender).Name;
// Get TextBlock by name
TextBlock standTextBlockFromName = (TextBlock)this.FindName(stand);
standTextBlockFromName.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}
This question already has answers here:
C# - Add button click events using code
(3 answers)
Closed 6 years ago.
I program a game in c# and I created buttons array for the game board. The buttons defined in the code (I did not draw them on the panel). How can I creat an action that happens when the user clicks on the button?
You say you have an array of buttons:
Button[] buttons = new Button[10]; // Let's say 10
for (int i=0; i<buttons.Length; i++)
{
buttons[i] = new Button();
buttons[i].Click += button_Click;
}
private void button_Click(object sender, EventArgs e)
{
// This method is invoked when any of the buttons is clicked
}
You could add something like this:
Button _myButton= new Button();
button.Click += (s,e) => { your code; };
button.Click += new EventHandler(myButton_Click);
container.Controls.Add(button);
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.
I'm working on a project that is a quiz. I am using 4 asp buttons that are styled with CSS, and I am using those 4 buttons as answers, with the right or wrong answer in the button's text field. I have read around and found another post regarding something very similar to this but the answers were to use a dictionary list. I am kind of sure that I don't need to use a list, that I should be able to randomly assign the text attribute. Here is the code that I am using to add the answers to the buttons text attribute but I need to randomize where the answers go, because it wouldn't be right to have the correct answer in the same button all the time.
lblQuestion.Text = ds.Tables[0].Rows[myNum]["Question"].ToString();
btn1.Text = ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString();
btn2.Text = ds.Tables[0].Rows[myNum]["WrongAnswer1"].ToString();
btn3.Text = ds.Tables[0].Rows[myNum]["WrongAnswer2"].ToString();
btn4.Text = ds.Tables[0].Rows[myNum]["WrongAnswer3"].ToString();
each button has an onlick method to change to the next question and changes the buttons text to the next answers. This code is in every buttonclick event...
protected void btn1_Click(object sender, EventArgs e)
{
lblQuestion.Text = ds.Tables[0].Rows[myNum]["Question"].ToString();
btn1.Text = ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString();
btn2.Text = ds.Tables[0].Rows[myNum]["WrongAnswer1"].ToString();
btn3.Text = ds.Tables[0].Rows[myNum]["WrongAnswer2"].ToString();
btn4.Text = ds.Tables[0].Rows[myNum]["WrongAnswer3"].ToString();
myNum = myNum + 1;
}
I'm unsure but maybe I need to be able to use one method that can randomize all the buttons text instead of coding into each buttonclick event?
Thanks
I wrote a simple subroutine to demonstrate how to randomly order strings and then attach them to buttons. This example simply demonstrates how to randomize the location of the correct answer among 4 possible places on the screen.
static void Main()
{
// create array of 4 string (or answers in your case)
//string[] array = new string[4] { "apple", "banana", "cranberry", "dragon fruit" };
string[] array = new string[4] {
ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString(),
ds.Tables[0].Rows[myNum]["WrongAnswer1"].ToString(),
ds.Tables[0].Rows[myNum]["WrongAnswer2"].ToString(),
ds.Tables[0].Rows[myNum]["WrongAnswer3"].ToString(),
};
// randomize the ordering of the items
System.Random rnd = new System.Random();
array = array.OrderBy(x => rnd.Next()).ToArray();
// each time you run this, the correct answer will be in a different place:
btn1.Text = array[0];
btn2.Text = array[1];
btn3.Text = array[2];
btn4.Text = array[3];
}
Bonus answer, you can also wire-up multiple buttons to a single Event Handler like so. Once inside the handler, you can access the Button that raised the event and perform actions:
protected void Page_Load(object sender, EventArgs e)
{
// attach event handlers
Button1.Click += ButtonClick;
Button2.Click += ButtonClick;
Button3.Click += ButtonClick;
Button4.Click += ButtonClick;
}
private void ButtonClick(object sender, EventArgs e)
{
// your code here...
// this is the button that raised the event
Button button = (Button)sender;
// check its ID? you could also check its text, perform any actions you wish, etc.
if (button.ID == "Button1")
{
System.Diagnostics.Debug.Print("Button 1 Clicked!");
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
WPF: Button single click + double click problem
WPF/MVVM - how to handle double-click on TreeViewItems in the ViewModel?
I need to to handle both the single click and the double click of a button in a WPF application with different reaction. On one click I only want to show that button has focus and on double click I want to show a new window(similar to Windows icon behavior). But on a doubleclick, WPF fires two clicks so it's hard to handle this situation. Can anyone help to solve this problem? Thanks for any help.
I tried this:
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer(
new TimeSpan(0, 0, 0, 1),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
Trace.WriteLine("Double Click");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
MainWindow c = new MainWindow()
c.focusButton.Background = new SolidColorBrush(Colors.DarkBlue);
}
but focusButton Bacground didnĀ“t change when I clicked on the focusButton.
Interesting question.
In my opinion, on the first step you have to handle Button_click in different ways as usual.
Maybe try to use some counter and timer?