I have a "button creator"(that creates my own custom buttons) in my form and I need to, after creating some buttons in the form, clicking in 2 random ones to connect then with a simple line (can be a System.Drawing.Pen). And i should use some kind of graph logical connection to do it. But I have no idea how I should do. Any code suggestions? Thank You
Here is an example:
// two variables we will need:
Button lastBtn = null;
List<Tuple<Button, Button>> buttons = new List<Tuple<Button, Button>>();
void commonButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn == null) return;
if (lastBtn == null) { lastBtn = btn; return; }
else if (btn == lastBtn) { lastBtn = null; return; }
else { buttons.Add(new Tuple<Button, Button>(lastBtn, btn)); lastBtn = null; }
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach ( Tuple<Button, Button> t in buttons)
{
e.Graphics.DrawLine(Pens.RoyalBlue, t.Item1.Location, t.Item2.Location);
}
}
When creating your buttons hook each up with the common click event:
yourButtonClass btn = new yourButtonClass()..
..
btn.Click += commonButton_Click;
This always draw lines from and to the upper left corner. Using the middle instead or a smart loaction on an edge the is cloest to the other button if left for you. In the latter case you could also add start- and endcaps to the pen, if you want to.
Related
I am writing a program where I dynamically add buttons, I do that by storing them in a Dictionary to get a certain value from them later on (the color of the background).
I need to set a Click event on every one of them, but every Click event has to be a little different, as by clicking the button, a ColorDialog pops up and changes the background of the button.
Is there a way to know which button I clicked? In the following code, the button1 click event adds the other buttons and sets the EventHandler for each of them, what should be the code for the EventHandler? Thank you so much in advance guys.
int i = 0;
Dictionary<int, Button> buttonDictionary = new Dictionary<int, Button>();
Dictionary<int, ColorDialog> colorsDictionary = new Dictionary<int ColorDialog>();
public void button1_Click(object sender, EventArgs e)
{
i++;
buttonDictionary.Add(i, new Button());
buttonDictionary[i].Click += new EventHandler(Click);
this.Controls.Add(buttonDictionary[i]);
}
public void Click(object sender, EventArgs e)
{
//Somehow get the int key of the button that was clicked???? (in this case: int j)
int j;
if (!colorsDictionary.ContainsKey(j))
{
colorsDictionary.Add(j, new ColorDialog());
}
if (colorsDictionary[j].ShowDialog() == DialogResult.OK)
{
buttonDictionary[j].BackColor = colorsDictionary[j].Color;
}
}
The code is made just for adding the buttons, I will be glad for any kind of help, thank you guys!
Well, a direct answer to your question is: cast the sender to a Button
Button pressedButton = (Button) sender;
and then check to which button of the dictionary it matches:
foreach (var entry in buttonDictionary)
{
if (entry.Value == pressedButton)
{
j = entry.Key;
break;
}
}
However, that's overly complex for what you want to achieve. It would be much easier if you had a direct relationship between the button and the color picker:
Dictionary<Button, ColorDialog> buttonDictionary = new Dictionary<Button, ColorDialog>();
Then fill it like this:
public void button1_Click(object sender, EventArgs e)
{
i++;
var button = new Button();
this.Controls.Add(button);
button.Click += new EventHandler(Click);
buttonDictionary.Add(button, null);
}
And later access it with
public void Click(object sender, EventArgs e)
{
Button pressedButton = (Button) sender;
ColorDialog dialog = buttonDictionary[pressedButton];
if (dialog == null)
{
dialog = new ColorDialog();
buttonDictionary[pressedButton] = dialog;
}
if (dialog.ShowDialog() == DialogResult.OK)
{
pressedButton.BackColor = dialog.Color;
}
}
Even more, the question is why you would need so many ColorDialgos, since it should be possible with one dialog only. You can get rid of i, j, all dictionaries and most handling as well. IMHO, the following should be sufficient:
public void button1_Click(object sender, EventArgs e)
{
var button = new Button();
Controls.Add(button);
button.Click += Click;
}
public void Click(object sender, EventArgs e)
{
Button pressedButton = (Button) sender;
ColorDialog dialog = new ColorDialog {Color = pressedButton.BackColor};
if (dialog.ShowDialog() == DialogResult.OK)
{
pressedButton.BackColor = dialog.Color;
}
}
Bonus info:
I don't exactly know what you want to achieve. But your buttons will all be in the same place, overlapping each other. To avoid this, drag a flow layout panel onto the form and then add the buttons to the flow layout:
flowLayoutPanel1.Controls.Add(button);
This will ensure that your buttons are nicely arranged.
In my project I have a ListView and I would like to open my ContextMenuStrip when I clicked right button in the large icon. I tried many things but I am unsuccessful. When I right click inside of ListView the ContextMenuStrip opens, but I want to see just when I right clicked the large icon.
Also I need to help about get the clicked icon's name (properties).
This is a quick and dirty solution; please do put more work into it than I did..
// a class level reference, prepare it where you want..
ContextMenuStrip ms = new ContextMenuStrip();
You should either code the MouseDown or the MouseUp event:
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
// disassociate from listview at first:
listView1.ContextMenuStrip = null;
// check for right button
if (e.Button != System.Windows.Forms.MouseButtons.Right) return;
// get item info:
ListViewHitTestInfo hi = listView1.HitTest(e.Location);
// no item hit:
if (hi.Item == null) return;
// calculate the image rectangle:
// this contains the unscrolled y coordinate:
Point iloc = listView1.GetItemRect(hi.Item.Index).Location;
// we combine it with the x-position:
Rectangle r = new Rectangle(new Point (hi.Item.Position.X, iloc.Y),
imageList1.ImageSize);
// no image hit:
if ( !r.Contains(e.Location) ) return;
// maybe prepare or change the menue now..
// here I display the image name from the keys array:
ms.Items[0].Text = imageList1.Images.Keys[hi.Item.ImageIndex];
ms.Location = e.Location;
// associate with listview and show
listView1.ContextMenuStrip = ms;
ms.Show();
}
Can you please try the following and let see wether it works or not...
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (listView1.FocusedItem.Bounds.Contains(e.Location) == true)
{
contextMenuStrip1.Show(Cursor.Position);
}
}
}
This should work
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
ListView listView = sender as ListView;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ListViewItem item = listView.GetItemAt(e.X, e.Y);
if (item != null)
{
item.Selected = true;
contextMenuStrip1.Show(listView , e.Location);
}
}
}
Search the listview item on mouse click location. If it is there, show the menu.........
I made a quiz game and I want the text on the button to be bold when clicked. This code works:
button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Bold);
The problem I'm having is when I click on the 'Next' button to go to the next question, the text is still bold even though the answer hasn't been clicked. How do I solve this?
Just do this on "Next" button click
button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Regular);
You need to un-bold everything when you click Next. The code below should help (it also includes a possibly cleaner bolding implementation).
// usage
foreach(var button in GetAnswerButtons())
{
button.Click += OnClickToBold;
button.Click += OnClickSetPropertyBasedOnCorrectness;
}
nextButton.Click += NextClick;
// implementations
private void OnClickToBold(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);
}
private void OnClickSetPropertyBasedOnCorrectness(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
button.WhateverProperty = IsCorrectAnswer(button)
? valueWhenCorrect
: valueWhenWrong;
}
private void NextClick(object sender, EventArgs e)
{
foreach(var button in GetAnswerButtons())
{
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Regular);
UnsetPropertyBasedOnCorrectness(button);
}
}
private IEnumerable<Button> GetAnswerButtons() { ... }
private void UnsetPropertyBasedOnCorrectness(Button b) { ... }
I have a panel, that can be moved left or right (chosen automatically according to its current position, distance is static) by click. Also, the user can drag the panel vertically however he wants by clicking the panel, holding the button and moving his mouse. The problem is, that the panel does the left/right move also, when it is dropped after being moved vertically, so the user has to click it again afterwards, to get to correct side (left/right). Here are the methods I am using:
Adding eventhandlers to panel(called Strip here)
Strip.MouseDown += new MouseEventHandler(button_MouseDown);
Strip.MouseMove += new MouseEventHandler(button_MouseMove);
Strip.MouseUp += new MouseEventHandler(button_MouseUp);
Strip.Click += new EventHandler(strip_Click);
And here all the methods mentioned above:
void button_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as Control;
previousLocation = e.Location;
Cursor = Cursors.Hand;
}
void button_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
var location = activeControl.Location;
location.Offset(0, e.Location.Y - previousLocation.Y);
activeControl.Location = location;
}
void button_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;
Cursor = Cursors.Default;
}
void strip_Click(object sender, EventArgs e) // The one moving strip to left or right
{
activeControl = sender as Control;
if (activeControl.Left != 30)
activeControl.Left = 30;
else
activeControl.Left = 5;
}
How to make the panel not move left or right when it was moved vertically?
You'll need to distinguish between a click and a drag. So add a private field named "dragged".
private bool dragged;
In the MouseDown event handler add:
dragged = false;
In the MouseMove event handler add:
if (Math.Abs(location.Y - previousLocation.Y) >
SystemInformation.DoubleClickSize.Height) dragged = true;
In the Click event handler add:
if (dragged) return;
I have a problem with my winforms C# project.
I want to move a new (custom) button around the form (at run time).
How can I do that?
Button[] buttons = new Button[1000];
int counter = 0;
Button myText = new Button();
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
myText.MouseMove += new MouseEventHandler(myText_MouseMove);
myText.MouseDown += new MouseEventHandler(myText_MouseDown);
buttons[counter] = myText;
counter++;
}
public void myText_MouseMove(object sender, MouseEventArgs e)
{
int s = e.GetHashCode();
int check = 0;
for (int i = 0; i < counter; i++)
{
if (buttons[i].GetHashCode() == s)
check = i;
}
if (e.Button == MouseButtons.Left)
{
buttons[check].Left += e.X - move.X;
buttons[check].Top += e.Y - move.Y;
}
}
void myText_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
I use the code above to create the new button and I am trying to move it around the form.
If I code for just 1 button I am able to move it but, I want to be able to do this for more buttons as well.
Try this
public void myText_MouseMove(object sender, MouseEventArgs e)
{
Button button = (Button)sender;
if (e.Button == MouseButtons.Left)
{
button .Left += e.X - move.X;
button .Top += e.Y - move.Y;
}
}
I've come across this thread while doing something similar.
Even though the thread is quite old; I'm sharing the way I've achieved this.
Notes:
Unlike the operator question above; in the example bellow; I'm not using a Button[] (array) with 1000 buttons; (but this can be easily changed).
In the example bellow I have:
A Form with 1 Button on it.
When this button is clicked; it will create a new (Custom) Button
(hence; having two button click events in the code simply for the test purpose).
Bellow you can find: A way to generate the button; and so how to drag it around the form.
/* Variables */
// Used to Store the Mouse Location on the Screen when Button is Clicked
private Point mouseDownLocation;
// Used to know if the Button can be Moved.
private bool isMoveable = false;
// Used to prevent clicking the button when being dragged
private bool clickEnabled = true;
/// <summary> Occurrs when the visible button on the Form is clicked. </summary>
private void Button_Click(object sender, EventArgs e)
{
// Create a variable to set the Size of the new Button.
Size size = new Size(100, 100);
// Create the Button. Specifying the Button Text and its Size.
CreateButton("Button Text", size);
}
/* Custom Button : Create & Configure the Button */
private void CreateButton(string btnText, Size btnSize)
{
// Create the Button instance:
Button btn = new Button();
// Custom Button Style Configuration Code.
// i.e: FlatStyle; FlatAppearance; BackColor; ForeColor; Text; Size; Location; etc...
btn.FlatStyle = FlatStyle.Standard;
btn.FlatAppearance.BorderColor = Color.White;
btn.BackColor = Color.Purple;
btn.ForeColor = Color.White;
btn.Text = btnText;
btn.Size = btnSize;
btn.Location = new Point(100, 100);
// Custom Button Event.
btn.Click += new EventHandler(CustomButton_Click);
btn.MouseDown += new MouseEventHandler(CustomButton_MouseDown);
btn.MouseMove += new MouseEventHandler(CustomButton_MouseMove);
btn.MouseUp += new MouseEventHandler(CustomButton_MouseUp);
// Add Button to Control Collection.
Controls.Add(btn);
// Show Button.
btn.Show();
}
/* Custom Button Events */
/// <summary> Occurrs whenever the Custom Button is Clicked </summary>
private void CustomButton_Click(object sender, EventArgs e)
{
if (clickEnabled)
{
// Note: The "for" loop bellow lets you get the current button; so you can drag it.
Button btn = (Button)sender;
// Iterate over the Form Controls
for (int i = 0; i < Controls.Count; i++)
{
// If the Button Clicked Corresponds to a Found Index in Control Collection...
if (i == Controls.IndexOf(btn))
{
// Perform Corresponding Button Action.
MessageBox.Show(btn.Name + " Clicked!");
}
}
}
}
/// <summary> Occurrs whenever Left Mouse Button Clicks the Button and is Kept Down. </summary>
private void CustomButton_MouseDown(object sender, MouseEventArgs e)
{
// Check if Left Mouse Button is Clicked.
if (e.Button == MouseButtons.Left)
{
// Set mouseDownLocation (Point) variable, according to the current mouse location (X and Y).
mouseDownLocation = e.Location;
// Enable the hability to move the Button.
isMoveable = true;
}
}
/// <summary> Occurrs whenever Left Mouse Button is Down and Mouse is Moving the Button. </summary>
private void CustomButton_MouseMove(object sender, MouseEventArgs e)
{
Button btn = (Button)sender;
// If the hability to move the button is "true" and the mouse button used to drag the button is the left mouse button:
if (isMoveable)
{
if (e.Button == MouseButtons.Left)
{
// Set the Button location X and Y...
btn.Left = e.X + btn.Left - mouseDownLocation.X;
btn.Top = e.Y + btn.Top - mouseDownLocation.Y;
}
}
// Disable Click hability (See: Note at the end of this post).
clickEnabled = false;
}
/// <summary> Occurrs whenever Left Mouse Button is not longer being hold down (Left Mouse Button is Up). </summary>
private void CustomButton_MouseUp(object sender, MouseEventArgs e)
{
// Disables the hability to move the button
isMoveable = false;
clickEnabled = true;
}
Note: The Custom Button Click interferes with the MouseDown Event. In a "simple" manner:
I updated the code by including a boolean value to prevent clicking the button when actually it is being dragged.
Hope this helps you out understanding how to accomplish this feature.
Hope this helps people out achieving this goal.