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.
Related
This question already has answers here:
Process.Start to open an URL, getting an Exception?
(4 answers)
Closed 1 year ago.
I am very interested in C# winforms. I decided to make it open a browser window once I click on a link label with the default browser. I searched on the internet and found the following code:
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 GUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
try
{
VisitLink();
}
catch (Exception ex)
{
MessageBox.Show("Unable to open link that was clicked.");
}
}
private void VisitLink()
{
// Change the color of the link text by setting LinkVisited
// to true.
linkLabel1.LinkVisited = true;
//Call the Process.Start method to open the default browser
//with a URL:
System.Diagnostics.Process.Start("http://www.microsoft.com");
}
}
}
When I tried it in my code, I clicked on the link but nothing shows up. Not even an exception pops up, which made me confused. Can anyone help me? Thanks!
The link is correctly marked as visited after the click ? If not, the event is probably not even got fired. Check if you correctly add the callback on the click event
I need to get a rich text box to scroll slowly when you press a button. I have only found codes that teleport you instantly to the end of a box but I need it to slowly scroll down at a readable pace.
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.SelectionStart <= textBox2.TextLength)
{
textBox2.SelectionStart += 30;
textBox2.ScrollToCaret();
Application.DoEvents();
}
}
This code does scroll but it is to fast and I need to slow it down.
Following the suggestion of Uwe Keim here is the steps you have to do in order to use dot-net-transitions(assuming you are using WinForms).
In your Package Manager Console type:
Install-Package dot-net-transitions -Version 1.2.1
Then press enter.
This will install the NuGet Package so you can use the transitions library.
In your form I assume you have a text box and a button eg:
Right click the button on the form and select properties.
From properties click the event button (it looks like a lighting bolt)
Then double click the Action Click.
This will create the click event for your button and will open the form code behind.
Then put this code in the button click event:
private void button1_Click(object sender, EventArgs e)
{
Transition t = new Transition(new TransitionType_EaseInEaseOut(2000));
t.add(this.textBox1, "Top", 200);
t.run();
}
Note
You need to tell VS that you are using Transition therefor in your form you must declare the following
using Transitions;
I have called my textbox textBox1 you might need to change that reference to the name you have used for your text box.
This would be your form
using System;
using Transitions;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Transition t = new Transition(new TransitionType_EaseInEaseOut(2000));
t.add(this.textBox1, "Top", 200);
t.run();
}
}
}
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
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;
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.