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.
Related
I'm trying to get it so when you click the button (which is START) it brings you two buttons, noob and medium. I can't figure out how to get the second one to show up. The ID is for button1 so I've tried renaming the second to button1 but won't work. How do I pass the button1 again to have it logically produce two buttons from one button?
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 App2341
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Noob";
button1.Location = new Point(100, 100);
}
private void Button2_Click(object sender, EventArgs e)
{
button1.Text = "Medium";
button1.Location = new Point(100, 150);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Try to create the three buttons in the designer and give each one a name, then set the visibility of the start-button to visible and the visibility of the other two buttons to hidden. When you click the start button you set the visibility of the startbutton to hidden and the visibility of the other two buttons to visible.
To set the buttons visibility:
button1.Visible = true; //to show the button
button1.Visible = false; //to hide the button
I'm trying to use the FlowLayoutPanel with linkLabels in Visual Studio for a quick project. I've selected "TopDown" for direction and wrapping to false. When I launch the program; however, the direction always shows left to right. Is there a box or something that I haven't checked? Or is there any reason a linklabel would ignore the flow direction?
Here's my code and some screenshots of what I see.
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 myProject
{
public partial class Form1 : Form
{
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
linkLabel1.LinkClicked += linkLabel1_LinkClicked;
linkLabel2.LinkClicked += linkLabel2_LinkClicked;
linkLabel3.LinkClicked += linkLabel3_LinkClicked;
Controls.Add(panel);
panel.Controls.Add(linkLabel1);
panel.Controls.Add(linkLabel2);
panel.Controls.Add(linkLabel3);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel1, 0);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel2, 0);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel3, 0);
}
}
}
This is the control view before I've started the program.
This is what I see when I run the program - marked with the red arrow.
Because you are initializing your FlowLayoutPanel in the code-behind, you have to set the FlowDirection property of this new instance of FlowLayoutPanel in the same code-behind:
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
panel.FlowDirection = FlowDirection.TopDown;
The FlowLayoutPanel that you declare in your code-behind is separate from the one you have in your layout, so the FlowDirection property is not set the same. I tested the code above and I believe it does what you were looking for.
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
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.
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;