C# Forms Event, want button click to produce two buttons - c#

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

Related

FormLayoutPanel, Not Changing Direction when using LinkLabels

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.

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.

mousewheel event not triggered after clicking off the canvas

thanks to http://www.eqqon.com/index.php/Piccolo_Snippets, i had mousewheel zooming working well until i added winform widgets to the form outside of the canvas; see pic of a test form below:
i found that if i clicked on button1, and moused back onto the canvas, i no longer get mousewheel events. Other mouse events (e.g. PNode entry/leave) still work however. even after clicking on the canvas, the mousewheel is still dead. the canvas's mousedown event works fine also. so only the mousewheel breaks. below is minimalist code to demonstrate what i'm seeing.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UMD.HCIL.Piccolo;
using UMD.HCIL.Piccolo.Event;
using UMD.HCIL.Piccolo.Nodes;
namespace piccolo_wheel_test {
public partial class Form1 : Form {
int mdown_count = 0;
int mwheel_count = 0;
public Form1() {
InitializeComponent();
PNode rect = PPath.CreateRectangle(40, 40, 20, 50);
rect.Brush = Brushes.Blue;
pCanvas1.Layer.AddChild(rect);
pCanvas1.Camera.MouseWheel += new PInputEventHandler(Camera_MouseWheel);
pCanvas1.Camera.MouseDown += new PInputEventHandler(Camera_MouseDown);
}
void Camera_MouseWheel(object sender, PInputEventArgs e) {
Debug.WriteLine("got mouse wheel: " + (mwheel_count++).ToString());
}
void Camera_MouseDown(object sender, PInputEventArgs e) {
Debug.WriteLine("got mouse down: " + (mdown_count++).ToString());
}
private void pCanvas1_Enter(object sender, EventArgs e) {
Debug.WriteLine("enter pcanvas");
}
private void pCanvas1_Leave(object sender, EventArgs e) {
Debug.WriteLine("leave pcanvas");
}
private void button1_Enter(object sender, EventArgs e) {
Debug.WriteLine("enter button");
}
private void button1_Leave(object sender, EventArgs e) {
Debug.WriteLine("leave button");
}
}
}
as an aside, i see that the canvas does not raise "enter"/"leave" events consistently; i see one "enter" when the form loads and one "leave" if i click button1 but no more "enter"/"leave" if i go back and forth. further, when i click on button1, i raises its "enter" event but when i click back on the canvas, "button1" doesn't raise its "leave" event (which it does if i clicked on other winform widgets, such as the trackbar.) thanks.

User Placed Text on a picturebox in c#

I am trying to make a tool that allows me to choose a certain location on a picturebox to put text from a textbox on. It will need to be able to place multiple different texts on the picturebox and then be able to be deleted. This is my current code:
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 TextboxTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Graphics G = Graphics.FromImage(pictureBox1.Image);
G.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.Black, new Point(MousePosition.X, MousePosition.Y));
}
}
}
At the moment i can type the text in the textbox, but can't draw the string on the picturebox and choose its location. I have a button which is meant to confirm the text written is right and then allow the user to choose its location. Please can someone help me sort this code out?
Thanks-
The MousePosition property is relative to the screen, not the PictureBox.
You should handle the MouseClick event and draw the string at e.X and e.Y.
Alternatively, you can call pictureBox1.PointToClient to transform screen coordinates to control-relative coordinates.
Also, you should dispose the Graphics object in a using statement.
Finally, I'm pretty sure you'll need to call pictureBox1.Invalidate() after modifying the image to force it to repaint.

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