Determine Which Control In My WPF Has Focus Via Script [closed] - c#

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 5 years ago.
Improve this question
How to determine which control has focus in C# ?
Example :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox); /*Here there my textBox has focus.*/
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, /*I mean here : the focus remains where it is already, on the textBox.*/);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
myControl.Focus();
}
In the NotCorrect() method :
textBlock.Background means : I want the same brush as the one already existing (see the first line of the Click event), I do not want to change anything.
(I prefer to write textBlock.Background rather than rewrite Brushes.White.)
Now I ask if there is also a way to say : I want the focus to remain on the control on which it is already, I do not want to change anything.

Form.ActiveControl is you should search for.

I found out a kind of solution using null :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox);
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, null);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
if (myControl != null)
myControl.Focus();
}
Another simpler solution I found is to use this :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox);
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, this);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
myControl.Focus();
}

Related

Unable to reset time duration [closed]

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 2 years ago.
Improve this question
On this code I am starting and stopping random sampling based on a fixed sampling time.
When I clicked stop sampling, timers stops correctly.
But when I start back again, the times do not start correctly.
could you please help to check what is wrong here?
public partial class MainWindow : Window
{
public float samplingTime = 10f;
double currentDateTime = 0.0;
DateTime dtEnd;
public bool running;
public MainWindow()
{
InitializeComponent();
timer1 start.code()
void timer_Tick(object sender, EventArgs e)
{
dtEnd = DateTime.Now.AddSeconds(samplingTime);
someCode();
}
}
private void startSample_Click(object sender, RoutedEventArgs e)
{
timer2 start code()
void windowtimer_Tick(object sender, EventArgs e)
{
currentDateTime = (dtEnd - DateTime.Now).TotalSeconds;
if (currentDateTime < 0.1 && running == true)
{
code();
}
}
}
private void stopSample_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
windowTimer.Stop();
}
You are adding the Tick handler multiple times:
windowTimer.Tick += windowtimer_Tick;
is executed every time you start, but you don't unhook it. Probably you should set the handler elsewhere (in the designer maybe).
Or you can add:
windowTimer.Tick -= windowtimer_Tick;
to the Stop handle

C#: How can I make textarea that accepts only some specific words? [closed]

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 5 years ago.
Improve this question
How can I make a TextArea that accepts only the words I have already set
with C# on Visual studio.
I'm a beginner in programming.
update:
what (-3) about?
like I said I'm a beginner, trying to build android game with c#.
until now just do some mistakes.
this website little complex to handle with but I will get used to it :) and my english poor
thank you all.
Use TextBox and add TextChanged event on your tool
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]"))
{
MessageBox.Show("This textbox accepts only alphabetical characters");
textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
More Info
textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
private void textBox1_TextChanged(object sender, EventArgs e)
{
Regex regex = new Regex(#"[^0-9^+^\-^\/^\*^\(^\)]");
MatchCollection matches = regex.Matches(textBox1.Text);
if (matches.Count > 0) {
//tell the user
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string yourWord = "mouse";
if (textBox1_Text.Text.Contains(yourWord))
{
textBox1_Text.Text.Replace(yourWord, "");
}
}

Rename a checkbox by implementing ContextMenu c# winforms [closed]

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 years ago.
Improve this question
I have set of dynamically created checkboxes on a panel and also have implemented ContextMenuStrip on all checkboxes.
I am unable to detect that which control currently displays the shortcut menu defined in the ContextMenuStrip.
I have got the answer.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a MenuItem
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenu that contains this MenuItem
ContextMenu menu = menuItem.GetContextMenu();
// Get the control that is displaying this context menu
Control sourceControl = menu.SourceControl;
}
}
Use the SourceControl() property.
With a ContextMenu:
private void menuItem1_Click(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)contextMenu1.SourceControl;
Console.WriteLine(cb.Name);
}
With a ContextMenuStrip:
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)contextMenuStrip1.SourceControl;
Console.WriteLine(cb.Name);
}

ListBox items doesn't appear when I get it from Method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have problem with ListBox.
I made a method that does some work and returns a ListBox type. I tried to get this Listbox instance from the method, but the ListBox doesn't show any items.
I hope to know how I can replace the existing ListBox with the returned Listbox from the method.
public ListBox GetProduct()
{
ListBox MyListBox = new ListBox();
MyListBox.Sorted = true;
MyListBox.Items.Add("aaaaa");
return MyListBox;
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1 = GetProduct();
}
try this:
public void GetProduct(ListBox MyListBox)
{
MyListBox.Sorted = true;
MyListBox.Items.Add("aaaaa");
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
GetProduct(listBox1);
}

Why doesn't my second form show up? [closed]

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.

Categories

Resources