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));
}
Related
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 5 years ago.
Improve this question
I have 1 panel in windows form, set to a certain width and height, and want to recreate something similar to how you can adjust width and the height the paint box in Microsoft paint. How do I make a panel resizable for the user, so that they can resize from both width and height?
You can create your own UserControl (e.g. called ResizablePanel), use a Panel in it, set its Anchor to Top, Bottom, Left, Right.
Resize that Panel so it nearly covers all the corners of your UserControl. The black part in this picture is the UserControl, the white part, your Panel.
Then you have to use 3 Events: ResizablePanel_MouseDown, ResizablePanel_MouseMove and ResizablePanel_MouseUp.
private void ResizablePanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isResizeMode = true;
}
}
private void ResizablePanel_MouseMove(object sender, MouseEventArgs e)
{
if (isResizeMode)
{
this.Size = new Size(e.X, e.Y);
}
}
private void ResizablePanel_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isResizeMode = false;
}
}
Now you can click on your ResizablePanel border to resize it directly.
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();
}
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 6 years ago.
Improve this question
I want that if i click on an image it gives a visual effect like whirl effect or glow effect or anything else on specific part around the point where i clicked with the mouse. for example if anyone has used the picture password of UC browser of windows phone exactly the same i want.
I have not tried anything because i have no knowledge of animation and graphics hence i haven't tried anything.
public void start()
{
messagebox.show("i haven't tried anything yet no knowledge of animation");
}
This code is nothing but i wrote it because i was not able to post the question.
In Winforms you could write code like this:
int AnimationCounter = 0;
Point AnimationCenter = Point.Empty;
Timer AnimationTimer = new Timer();
private void pictureBox1 _MouseClick(object sender, MouseEventArgs e)
{
AnimationCenter = e.Location;
AnimationTimer.Interval = 20;
AnimationTimer.Start();
}
void AnimationTimer_Tick(object sender, EventArgs e)
{
if (AnimationCounter > 15)
{
AnimationTimer.Stop();
AnimationCounter = 0;
pictureBox1.Invalidate();
}
else
{
AnimationCounter += 1;
pictureBox1.Invalidate();
}
}
private void pictureBox1 _Paint(object sender, PaintEventArgs e)
{
if (AnimationCounter > 0)
{
int ac = AnimationCounter / 2;
e.Graphics.DrawEllipse(Pens.Orange, AnimationCenter.X - ac,
AnimationCenter.Y - ac, ac * 2, ac * 2);
}
}
Don't forget to hook up the Paint and MouseClick event and also the AnimationTimer_Tick event.!
The result will draw a growing circle at the spot you click on which will disappear after ca. 10 * 20 ms..
Update: The first version suffered from repeatedly hooking up the Tick event. This one is better tested ;-)
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);
}
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);