I have a certain number of Textboxes, I need to track the last two focused Texboxes. This is the approch that I attempted.
private Control _focusedControl;
private Control _lastfocusedControl;
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
This is not working because when I press the button the contenent of the _lastfocusedControl will be the same as _focusedControl because another control was focused by clicking that button.
You can handle Enter event of all those TextBox controls using a single handler and in the handler and keep track of last n focused TextBox controls:
const int n = 2;
TextBox[] textBoxes = new TextBox[n];
private void textBox_Enter(object sender, EventArgs e)
{
var destination = new TextBox[n];
Array.Copy(textBoxes, 1, destination, 0, textBoxes.Length - 1);
textBoxes = destination;
textBoxes[textBoxes.Length - 1] = (TextBox)sender;
}
In above example, we are shifting the array to left, then assign the sender to the last element. This way the array always contains the last n focused TextBox controls for you.
I would suggest, to make it simpple, do something like when your textbox get focused, put the name of it into an array, for example, and after that, list the last two name added of the array created.
Related
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
{
textBox2.Focus();
}
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
{
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
}
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
{
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
}
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
{
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
}
So I have a matrix of panels (maybe will change for Picture Boxes in the future), and what i want is that every time i press one of the panels after pressing the button on the toolbox it will change it's background to a certain picture.
Right now what i have is:
private void EtapaInicial_Click(object sender, EventArgs e)
{
EtapaInicialWasClicked = true;
}
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
panel1.BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
What I would like to change was the panel1 to make it work for every panel (otherwise it will only change panel1 independently of the panel i've clicked), is that possible?
Try the following
private void EtapaInicial_Click(object sender, EventArgs e)
=> EtapaInicialWasClicked = true;
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
(sender as Panel).BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Yes it is. You have to loop through each panel
and assign the same event handler but you have to make some changes in the event handler itself
foreach(var p in allPanels)
{
p.Click += panel_Click;
}
Then change your event handler like this
private void panel_Click(object sender, EventArgs e)
{
var p = (Panel)sender;
if (EtapaInicialWasClicked)
{
p .BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Remember the sender argument contains reference to the actual control that initiated the event but you have to cast it first in order to use it.
However if you want to store more data for the event you've just handled you can use the panel.Tag property. This can be used to store EtapaInicialWasClicked for example
I am new to C# and I am planing to design my own keypad but I don't know how/where to start. as shown in photo, I have 4 textBoxes the keypad buttons.
The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?).
So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing.
Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in.
Thank you professionals.
Firstly, have a textbox that represents the one that is selected (outside of subroutines but inside the class):
TextBox SelectedTextBox = null;
And then make the "Click" event of each TextBox look like this:
private void textBoxNUM_Click(object sender, EventArgs e)
{
SelectedTextBox = sender as TextBox;
}
And then make the "Click" event of each Button look like this:
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
SelectedTextBox.Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
Or if that one doesn't work, this should.
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
(SelectedTextBox as TextBox).Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
To check if a textbox is focused you can do
if(textbox1.Focused)
{
//Print the value of the button to textbox 1
}
else if (textbox2.Focused)
{
//Print the value to textbox 2
}
UPDATE:
Since the textbox will lose focus when you click the button, you should have a temporary textbox (ie lastTextboxThatWasFocused) which is saved to everytime a textbox gains focus. Write an OnFocused Method and do something like
public void Textbox1OnFocused(/*Sender Event Args*/)
{
lastTextboxThatWasFocused=textbox1;
}
Then on button click you can do
if(lastTextboxThatWasFocused.Equals(textbox1))
{
//ETC.
}
You can give something along these lines a try. Create a generic click handler for the buttons and then assign the value to a textbox the text from the button, which happens to be the value. You can check which box was the last one focused in the TextBoxes' Click event. Create a global variable to store which one and use it in the below method.
private TextBox SelectedTextBox { get; set; }
private void NumericButton_Click(object sender, EventArgs e)
{
var clickedBox = sender as Button;
if (clickedBox != null)
{
this.SelectedTextBox.Text += clickedBox.Text;
}
}
private void TextBox_Click(object sender, EventArgs e)
{
var thisBox = sender as TextBox;
if (thisBox == null)
{
return;
}
this.SelectedTextBox = thisBox;
}
Try this code:
TextBox LastTxtBox;
private void textBox_Enter(object sender, EventArgs e)
{
LastTxtBox = sender as TextBox;
}
private void button_Click(object sender, EventArgs e)
{
LastTxtBox.Text = this.ActiveControl.Text;
}
Add textBox_Enter function to all textboxes enter event.
Add button_Click to all buttons click event.
Button Enter Event
Control _activeControl;
private void NumberPadButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (_activeControl is TextBox || _activeControl is RichTextBox)
{
_activeControl.Text += btn.Text;
if (!_activeControl.Focused) _activeControl.Focus();
}
}
TextBox or RihTextBox Enter Event
private void TextBoxEnter_Click(object sender, EventArgs e)
{
_activeControl = (Control)sender;
}
I managed to create textboxes that are created at runtime on every button click. I want the text from textboxes to disappear when I click on them. I know how to create events, but not for dynamically created textboxes.
How would I wire this up to my new textboxes?
private void buttonClear_Text(object sender, EventArgs e)
{
myText.Text = "";
}
This is how you assign the event handler for every newly created textbox :
myTextbox.Click += new System.EventHandler(buttonClear_Text);
The sender parameter here should be the textbox which sent the even you will need to cast it to the correct control type and set the text as normal
if (sender is TextBox) {
((TextBox)sender).Text = "";
}
To register the event to the textbox
myText.Click += new System.EventHandler(buttonClear_Text);
Your question isn't very clear, but I suspect you just need to use the sender parameter:
private void buttonClear_Text(object sender, EventArgs e)
{
TextBox textBox = (TextBox) sender;
textBox.Text = "";
}
(The name of the method isn't particularly clear here, but as the question isn't either, I wasn't able to suggest a better one...)
when you create the textBoxObj:
RoutedEventHandler reh = new RoutedEventHandler(buttonClear_Text);
textBoxObj.Click += reh;
and I think (not 100% sure) you have to change the listener to
private void buttonClear_Text(object sender, RoutedEventArgs e)
{
...
}
I guess the OP wants to clear all the text from the created textBoxes
private void buttonClear_Text(object sender, EventArgs e)
{
ClearSpace(this);
}
public static void ClearSpace(Control control)
{
foreach (var c in control.Controls.OfType<TextBox>())
{
(c).Clear();
if (c.HasChildren)
ClearSpace(c);
}
}
This should do the job :
private void button2_Click(object sender, EventArgs e)
{
Button btn = new Button();
this.Controls.Add(btn);
// adtionally set the button location & position
//register the click handler
btn.Click += OnClickOfDynamicButton;
}
private void OnClickOfDynamicButton(object sender, EventArgs eventArgs)
{
//since you dont not need to know which of the created button is click, you just need the text to be ""
((Button) sender).Text = string.Empty;
}
I am very new and extremely confused as to how I can accomplish this project. The project requests us to create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. This is where I am starting to have problems; I don't need to have what the user selects displayed in a message but the message reflecting what the font size and type were that the user selected. After the user clicks a button, display "Hello" in the selected font and size. I need help in getting the button to display the message in the desired font in a C# Windows Visual Studio 2010 form. I have just a basic code written to start me off which includes the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Since this is the beginning of this project, the font name and size that the user selects will eventually produce a message in that chosen font name and size. Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
You can use the ListBox.SelectedIndex property to set the initial selection of the listboxes. For example, you can add the following lines of code to explicitly select the first items in the listboxes after you add the event handlers:
this.listBox1.SelectedIndexChanged += new System.EventHandler (this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler (this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
By default, the SelectedIndex property of a ListBox is -1, which means there is no selection.
To answer your second question, to display 'Hello' in the selected font and size, I will assume that we can simply change the font of the textBox1 control.
First, make sure that textBox1 has some text; put this statement into the Form1 constructor after the call to InitializeComponent:
textBox1.Text = "Hello!";
Then, modify the event handlers to change the typeface and size of the font:
private void UpdateFont()
{
if (listBox1.SelectedIndex == -1 || listBox2.SelectedIndex == -1)
return; // selection not complete yet, so do nothing
string typeface = listBox1.SelectedItem.ToString();
float size = Convert.ToSingle(listBox2.SelectedItem.ToString());
textBox1.Font = new Font(typeface, size);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateFont();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateFont();
}
to put the default value of your listboxes:
listBox1.SelectedItem = "Arial";
listBox2.SelectedItem = "8";
or a better "dynamic solution":
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
the following code will make the text font and size change, depending on what the user has selected in the listbox.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
edit:
you are getting an error because there's probably no text in your textbox.
TextBox1.Text ="this is some text";
add this to your form.