C# Windows Forms code not working - Attach Event to button - c#

Could someone explain the reason why the code below does not work?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Speaker
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("bravo you did it");
}
}
}
The window I have designed that corresponds to this code is a single window with a single button. I have the intention to do quite an extented program, but encountering problems, I decided to start with a small sample to see what's wrong, and I see neither this simple code works. Any suggestions? When I press the button1, nothing happens at all.

Make sure attached the event Click with your button. You can do it by going to designer, double click the button, it will create the event handler for you in the code. You can also attach the event handler in your Form Constructor like:
public Form1()
{
InitializeComponent();
button1.Click += button1_Click;
}
You can go to the designer, right click on Button1, click properties, Got to events and there you can attach the event handler:

You need to bind the button click to the event of button click! :)
button1.Click += button1_Click;

Related

c# mouse event sequence

Question... why does this code only produce a message box "Down"? I'm not getting the Up. If I block down code, the up works.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mouse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
MessageBox.Show("Up");
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("Down");
}
}
}
Because you are showing a MessageBox
Whenever a mouse down event happened a MessageBox will popup. The MessageBox Will be in foreground and the up event will be on the MessageBox instead of the form. Thus the up event in the form doesn't fire
Just do a Console.WriteLine instead of MessageBox and it should work as expected
They will both fire if you remove the message box call. this is interferring with the mouse events because your loosing focus on the form. Instead of using message box try using...
Console.WriteLine("MouseUp");
This will display in the ouput window and not interfere with the events

The text does not appear after I click the button

I am following a tutorial but I am not receiving the same results. Based off the code below, a button is supposed to appear. After clicking on that button, a small dialogue box appears with the message "hello". The results I am receiving is after running my code the button appears but when I click on it, nothing happens. What am I doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NewPrjct
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello", "MyTitle");
}
}
}
In form1.designer.cs there should be an initializer for the button to link it to the form window. It is quite possible that it is not related to the form1.
This generally happens if you copy and paste a button or don't click on the button to open the source...
Go to to Form Design window and double click on the button and type the Messagebox.show("Hello", "My Title"); in there, it will either open up in
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello", "MyTitle");
}
and initialize your button or in some cases it will rename button 1 to
private void button1_Click_1(object sender, EventArgs e)
{
MessageBox.Show("Hello", "MyTitle");
}
Did generate this code by double clicking the button in Visual Studio or Type this out manually? If you typed it out manually you need to add code elsewhere I believe. I think you can just double click the button in the designer and it will add what you need.

Manipulate Dragged files from WinForm Application

wan't to write a outlook add-in which manipulate the email message when the user want to save the file local on the desk via drag and drop.
At the moment I'm not able to get the MouseDown event of the window. Is there an specific Windows Message which i can observe?
And is there any way to manipulate the data which is currently in the drag event?
Many thanks for you help!
I've already tried
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
namespace OutlookAddIn1
{
public partial class ThisAddIn
{
Outlook.Explorer explorer;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
explorer = Application.ActiveExplorer();
explorer.BeforeItemCopy += new Outlook.ExplorerEvents_10_BeforeItemCopyEventHandler(Explorer_BeforeItemCopy);
}
private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
MessageBox.Show("OnDragEnter");
}
void Explorer_BeforeItemCopy(ref bool cancel)
{
MessageBox.Show("copied");
}
}
}
There is no need to use Windows Messages, nor MouseDown event for this. You are simply using the wrong event. What you need to handle is:
DragEnter
DragOver
DragDrop
DragLeave
See this sample:
https://www.codeproject.com/articles/9017/a-simple-drag-and-drop-how-to-example

How to use event handlers in C#

I created a form with just 2 textboxes and a button. In the first one I type a temperature in Fahrenheit and when I press the button "Convert", the program calculates and puts the temperature in Celsius in the other TextBox. It's working fine.
Now I want the program to clear the second TextBox when I start typing in the first TextBox. Below I show just a part of the code, which didn't work. Can anybody help me?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Conv_Temp
{
public partial class Frm_Principal : Form
{
public Frm_Principal()
{
InitializeComponent();
}
public event EventHandler Leave;
private void Tb_Temp_Leave(object sender, EventArgs e)
{
MessageBox.Show("Leaving TB Tb_Temp");
Tb_Result.Text="";
}
}
}
I think you are almost there.
Try adding this under InitializeComponent();
this.Tb_Temp.TextChanged += new System.EventHandler(this.Tb_Temp_Leave);
Add new Event handler in your form designer code.
this.textBox1.TextChanged += new System.EventHandler(this.ModifyTextBox1);
and implement this event in above form.cs file(Form_Principal )
private void ModifyTextBox1(object sender, EventArgs e)
{
textBox2.Text = String.Empty;
}
Please follow good convention for writing codes this is just a demo.

Is there any way to focus ( tab control ) picture box?

I am working on .NET 2.0 with C#. How to make focus the control to picture box after pressing the tab from text box ? Please, give me a solution.
Picturebox control is not selectable control hence it doesn't receive focus. Even if you try to set tabindex and tabstop properties on form load it doesn't get the focus.
Why do you want to set focus to picturebox? Are you using click event of this control as a button click event?
Can you provide some more detail on this, so that we can provide a proper solution for this?
Create a button1, then set its TabIndex less than pictureBox1's; put the button1 on top of pictureBox1. Then on runtime hide it behind pictureBox1. To give visual cue that pictureBox has the focus, set the BorderStyle to Fixed3d, set it to none when it loses focus.
Proof of concept:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestPicture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.SendToBack();
pictureBox1.Click += button1_Click;
}
private void button1_Enter(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}
private void button1_Leave(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.None;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
}
}
You need to enclose your PictureBox in a control that can receive click events.

Categories

Resources