PictureBox not updating on label clicked - winforms

I've created this program in C++-cli Visual Studio to update a picturebox on a label click but it doesn't get updated.
private: System::Void label1_Click2(System::Object^ sender, System::EventArgs^ e) {
if (ctapas==1)
pictureBox14->ImageLocation = "./Release/IMG/420.png";
if (ctapas==2)
pictureBox14->ImageLocation = "./Release/IMG/421.png";
ctapas++;

Related

right click mouse to paste on a richtextbox vc++ windows forms

I am searching on how to implement a mouse down right click to paste data from a buffer into a richtextbox with visual c++ windows forms. My richtextbox is working but I can only paste data from buffer using shift+insert key.
Thank you so much in advance.
Solution:
Changed the RichTextBox to TextBox and use the code:
private: System::Void button1_Click(System::Object^ sender System::EventArgs^ e) {
String^ fileName = "output.txt";
StreamWriter^ sw = gcnew StreamWriter(fileName);
sw->WriteLine(textBox1->Text);
sw->Close();
}
The "paste" option is automatically available on the textbox (multi-line in my case). I think this is not possible with RichTextBox without heavy coding?
Do you mean a right click menu which can paste the clipboard's text to the RichTextBox?
1) Add a ContextMenuStrip (contextMenuStrip1)
2) Add a ToolStripMenuItem and name it paste (pasteToolStripMenuItem)
3) Set richTextBox1's ContextMenuStrip to contextMenuStrip1
4) Double-click the pasteToolStripMenuItem
private: System::Void pasteToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Paste();
}

Handling a Click for all controls on a Form & getting mouse location

I am working on a windows form that is covered in panels representing a grid.
I'm trying to create an event handler that handles all mouse clicks regardless of which panel the click occurs on and then moves a PictureBox to the location of the panel.
I managed to find a topic that covered the event handler, but I am unable to get the location of the mouse click from the event handler. Below is the code I have so far (mostly pinched from another post):
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
c.MouseClick += new MouseEventHandler(myEvent_handler_click);
}
}
public void myEvent_handler_click(object sender, EventArgs e)
{
Point point = new Point(e.X, e.Y);
game.MoveToSquare(point);
}
The line of code Point point = new Point(e.X, e.Y); doesn't work because I can't refer to the X of e or the Y of e.
How can I get the location of the mouse at the time it is clicked?
Any help is appreciated. Feel free to ask me more questions if I wasn't clear enough!
The delegate for the event handler is defined like so:
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
MouseEventArgs inherits from EventArgs, that's why your code is working. If you change the definition of your EventHandler, you should be able to access the coordinates:
public void myEvent_handler_click(object sender, MouseEventArgs e)
{
Point point = new Point(e.X, e.Y);
}
You can also simply access e.Location to get point:
Point point = e.Location;

It is possible to create "new MouseButtonEventArgs" in Silverlight?

I'm newbie in Silverlight and I need to activate MouseRightButtonDown(object sender, MouseButtonEventArgs e) from an another event handler in my application.
I have found, that in WPF it is possible to do somthing like this:
public void OnContextMenuOpened(object sender, RoutedEventArgs e) {
MouseButtonEventArgs args = new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice,
0,
MouseButton.Right);
MouseRightButtonDown(sender, args);
}
But I have in Silverlight neither InputManager-Class nor MouseButton-Class... It is generally possible to realise something like that?
I want to do it, because I try to select an DataGridRow(within an custom control) with help of right-mouse-button. Without context menu it is easily possible, but when I switch context menu on, then context menu opens and event will not fired...
My code snippet:
public override void OnApplyTemplate() {
DataGrid = (DataGrid)GetTemplateChild("DataGrid");
DataGrid.MouseRightButtonDown += DataGridMouseRightButtonDown;
ContextMenu = (ContextMenu)GetTemplateChild("ContextMenu");
ContextMenu.Opened += OnContextMenuOpened;
}
private void DataGridMouseRightButtonDown(object sender, MouseButtonEventArgs e) {
//My code to select an DataGridRow
}
public void OnContextMenuOpened(object sender, RoutedEventArgs e) {
//This event-handler now will be always activated if I do
//right-mouse-button-click
}
Thanks a lot for help!
The Results of my research has shown, that it is impossible in silverlight -.-

What event can I change the caret in in a winforms maskedtextbox

Right now, I'm calling the win32 createcaret/showcaret in the keypress event of my masked textbox. That changes it fine. I want the caret to change when the box is entered, though, either by tab or by click.
Unfortunately the enter event or even the invalidate event aren't suitable places to change that caret. It doesn't change, maybe because they fire too early.
So anyway, how can I get the caret to change on textbox enter without handling it in the enter event?
You need to add DestroyCaret to your routine, too:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_GotFocus(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 6, textBox1.Height);
ShowCaret(textBox1.Handle);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
DestroyCaret();
}

Silverlight Button Click

I'm having problems getting the Silverlight Button Click event to immediately update a control's UI element and then continue doing some other process. For example, update the text of a control and then do some process. I've tried calling the UpdateLayout() method but that doesn't help.
Here is some sample code:
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "Testing";
textBlock1.UpdateLayout();
UpdateLayout();
Thread.Sleep(2000);
textBlock1.Text = "Done";
}
In that sample, the textBlock1 control will never display the text "Testing".
It's because you've blocked the UI thread with your Sleep statement which means that the text block won't update until after it's completed, but by that time you've set the text to "Done".
This doesn't work. The UI will block when you do not exit the method. This is why in Silverlight you usually do everything asynchronously:
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "Testing";
var myTask = /* ... */
myTask.Completed += new FancyDelegate(myTask_Completed);
}
private void myTask_Completed(object sender, RoutetEventArgs e)
{
textBlock1.Text = "Done.";
}
If your tasks do not have asynchronous functions, just wrap them. But be reminded, that when you want to change your textBlock1.Text property from another thread, you must invoke it with the Dispatcher.

Resources