I want to show a inherited text box control on mouse over on the form. But text is not displayed. Below is my code.
private ChartCalloutBox m_calloutbox = null;
public Form2()
{
InitializeComponent();
this.MouseMove += Form2_MouseMove;
}
void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (m_calloutbox == null)
{
m_calloutbox = new ChartCalloutBox();
}
m_calloutbox.Location = e.Location;
m_calloutbox.Show();
}
internal class ChartCalloutBox : TextBox
{
public ChartCalloutBox()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Location = new System.Drawing.Point(350, 170);
this.ClientSize = new System.Drawing.Size(130, 40);
this.Size = new System.Drawing.Size(130, 40);
this.BackColor = System.Drawing.Color.Black;
this.ForeColor = System.Drawing.Color.Brown;
this.Name = "CalloutBox";
this.Text = "Callout Rect";
this.ResumeLayout(false);
//
}
}
Any one help on this how to show the text box on mouse over. and the text box place should be change based on the mouse position.
Thanks,
Bharathi.
Add your control to controls collection.
Code should be like
void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (m_calloutbox == null)
{
m_calloutbox = new ChartCalloutBox();
this.Controls.Add(m_calloutbox);
}
m_calloutbox.Location = e.Location;
}
Related
I am trying to create a Form in Visual Studio Windows Forms Apps C# in which during runtime the user or operator can create a new picturebox with a left mouse click and then have the ability to move each picturebox created.
I really do not know where to begin. Anyone have any ideas?
To add a new PictureBox dynamically, you can subscribe to the event Form_MouseClick and create the PictureBox like this:
public Form1()
{
InitializeComponent();
this.MouseClick += Form_MouseClick;
}
private void Form_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pictureBox = new PictureBox();
// cursor location
pictureBox.Location = new Point(e.X, e.Y);
pictureBox.BackColor = Color.Red;
this.Controls.Add(pictureBox);
}
To drag and move the PictureBox, you also nee to subscribe to the event MouseDown, MouseUp, and MouseMove.
Here is a simple demo you can refer to.
private void Form_MouseClick(object sender, MouseEventArgs e)
{
// create new control
PictureBox pictureBox = new PictureBox();
pictureBox.Location = new Point(e.X, e.Y);
pictureBox.BackColor = Color.Red;
this.Controls.Add(pictureBox);
// bind event for each PictureBox
pictureBox.MouseDown += pictureBox_MouseDown;
pictureBox.MouseUp += pictureBox_MouseUp;
pictureBox.MouseMove += pictureBox_MouseMove;
}
Point mouseDownPoint = Point.Empty;
Rectangle rect = Rectangle.Empty;
bool isDrag = false;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint = e.Location;
rect = (sender as PictureBox).Bounds;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrag)
{
isDrag = false;
(sender as PictureBox).Location = rect.Location;
this.Refresh();
}
reset();
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrag = true;
rect.Location = this.PointToClient((sender as PictureBox).PointToScreen(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)));
this.Refresh();
}
}
private void reset()
{
mouseDownPoint = Point.Empty;
rect = Rectangle.Empty;
isDrag = false;
}
You can add the form elements during runtime:
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(16, 16),
Location = new Point(100, 100),
Image = Image.FromFile("test.jpg"),
};
this.Controls.Add(picture);
Afterward, you can move it with the Mouse Events
I'm creating TextBoxes at runtime and add an EventHandler to eac, but I can only move the last one created, when I try to move a previous one, it disappears.
This is my code:
int Naslov_rnd;
TextBox tb;
private void Naslov_p_Click(object sender, EventArgs e)
{
Naslov_rnd++;
tb = new TextBox();
VizitKartica.SuspendLayout();
tb.Location = new Point(0, 0);
tb.Multiline = true;
tb.Size = new Size(200, 20);
tb.BorderStyle = BorderStyle.None;
tb.BackColor = Color.DodgerBlue;
tb.ForeColor = Color.White;
tb.Name = "Naslov_" + Naslov_rnd.ToString(); ;
tb.Text = "Dodajte Vaš naslov";
tb.Font = new Font("Microsoft Sans Serif", 12);
VizitKartica.Controls.Add(tb);
elementi_lista.AddItem(tb.Name);
VizitKartica.ResumeLayout(true); Controls collection
tb.MouseMove += new MouseEventHandler(tb_MouseMove);
tb.MouseDown += new MouseEventHandler(tb_MouseDown);
}
protected void tb_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
tb.Left = e.X + tb.Left;
tb.Top = e.Y + tb.Top;
}
}
protected void tb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point MouseDownLocation = e.Location;
}
}
As #LarsTech said, you cannot make one TextBox object point to all of the TextBoxes that will be created, a simple and effective solution to this is to use the sender object.
The EventHandler provides you with an argument wich will get passed to the method, and it will point to the control that caused the event to be fired.
Since we know that all the TextBoxes are sharing the same event and they are all TextBoxes, we can type-cast the sender object to the TextBox class and then use it.
Here is how :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Naslov_rnd;
private void button1_Click(object sender, EventArgs e)
{
Naslov_rnd++;
TextBox tb = new TextBox();
VizitKartica.SuspendLayout();
tb.Location = new Point(0, 0);
tb.Multiline = true;
tb.Size = new Size(200, 20);
tb.BorderStyle = BorderStyle.None;
tb.BackColor = Color.DodgerBlue;
tb.ForeColor = Color.White;
tb.Name = "Naslov_" + Naslov_rnd.ToString();
tb.Text = "Dodajte Vaš naslov";
tb.Font = new Font("Microsoft Sans Serif", 12);
VizitKartica.Controls.Add(tb);
VizitKartica.ResumeLayout(true);
tb.MouseMove += new MouseEventHandler(tb_MouseMove);
tb.MouseDown += new MouseEventHandler(tb_MouseDown);
}
protected void tb_MouseMove(object sender, MouseEventArgs e)
{
TextBox tb2 = (TextBox) sender;
if (e.Button == MouseButtons.Left)
{
tb2.Left = e.X + tb2.Left;
tb2.Top = e.Y + tb2.Top;
}
}
protected void tb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point MouseDownLocation = e.Location;
}
}
}
Hope that helped you and what you are looking for.
I'm having a weird behavior with a class I created inheriting from PictureBox.
It's a class made to behave like a button (basically what I care about is the mouse enter, mouse leave, mouse down, mouse up events).
Everything works perfectly until I add the TextLabel (code below) to the control of my customized class. It shows the label, centered and everything as I wanted, but the events I mentioned before (and every other event in that matter) becomes disabled, for some reason it won't fire them.
I would like to know what is the reason for that behavior and if there is any fix for it.
public class RoundedButton : PictureBox
{
private readonly Image r_BasicImage = RoundedButtonCheck2.Properties.Resources.basicRoundedButtonIcon as Image;
private readonly Image r_HoverImage = RoundedButtonCheck2.Properties.Resources.hoverRoundedButtonIcon as Image;
private readonly Image r_ClickImage = RoundedButtonCheck2.Properties.Resources.clickRoundedButtonIcon as Image;
private string m_Text;
private Font m_Font;
public Color m_TextColor;
private Label LabelText = new Label();
public RoundedButton()
{
this.Width = 130;
this.Height = 40;
this.Image = r_BasicImage;
this.BackColor = Color.Transparent;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.MouseDown += RoundedButton_MouseDown;
this.MouseUp += RoundedButton_MouseUp;
this.MouseEnter += RoundedButton_MouseEnter;
this.MouseLeave += RoundedButton_MouseLeave;
LabelText.Font = ButtonFont;
ButtonTextColor = Color.Black;
//PROBLEMATIC CODE HERE:
***********this.Controls.Add(LabelText);***************
}
public Color ButtonTextColor
{
get
{
return m_TextColor;
}
set
{
m_TextColor = value;
LabelText.ForeColor = m_TextColor;
}
}
public Font ButtonFont
{
get
{
if (m_Font == null)
{
m_Font = new Font("Calibri Light", 12);
}
return m_Font;
}
set
{
m_Font = value;
LabelText.Font = ButtonFont;
adjustLabel();
}
}
public string ButtonText
{
get
{
return m_Text;
}
set
{
m_Text = value;
LabelText.Text = m_Text;
adjustLabel();
}
}
private void adjustLabel()
{
const int MARGIN = 10;
LabelText.AutoSize = true;//needed for autosize calculation of the label;
Size newSize = new Size(LabelText.Size.Width, LabelText.Size.Height); ;
LabelText.AutoSize = false;//after auto-calculated size of the label, set to false in order for centering label works well.
this.MinimumSize = newSize;
this.Size = new Size(newSize.Width + MARGIN, newSize.Height + MARGIN);
LabelText.TextAlign = ContentAlignment.MiddleCenter;
LabelText.Dock = DockStyle.Fill;
}
private void RoundedButton_MouseLeave(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_BasicImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void RoundedButton_MouseEnter(object sender, EventArgs e)
{
RoundedButton hoveredButton = sender as RoundedButton;
if (hoveredButton != null)
{
hoveredButton.Image = r_HoverImage;
hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void RoundedButton_MouseUp(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_BasicImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void RoundedButton_MouseDown(object sender, MouseEventArgs e)
{
RoundedButton clickedButton = sender as RoundedButton;
if (clickedButton != null)
{
clickedButton.Image = r_ClickImage;
clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}
I have a button in my windows forms which I need to enable/disable. When disabled I need to change its backcolor and retain the forecolor to show that it is disabled.
Following is what I tried. this retains the forecolor(white here), but does not change the backcolor
private void button1_EnabledChanged(object sender, System.EventArgs e)
{
buttonScan.ForeColor = Color.White;
buttonScan.BackColor = Color.Aqua;
}
private void button1_Paint(object sender, PaintEventArgs e)
{
var btn = (Button)sender;
var drawBrush = new SolidBrush(btn.ForeColor);
var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center};
e.Graphics.DrawString(btn.Text, btn.Font, drawBrush, e.ClipRectangle, sf);
drawBrush.Dispose();
sf.Dispose();
}
I need to change the backcolor when the button is disabled
f you want to use your custom color, you need to set UseVisualStyleBackColor to false or the color will only be applied to the button upon mouse over.
a simple test uploaded to GitHub
public partial class mainForm : Form
{
Random randonGen = new Random();
public mainForm()
{
InitializeComponent();
}
private void mainForm_Load(object sender, EventArgs e)
{
populate();
}
private void populate()
{
Control[] buttonsLeft = createButtons().ToArray();
Control[] buttonsRight = createButtons().ToArray();
pRight.Controls.AddRange(buttonsRight);
pLeft.Controls.AddRange(buttonsLeft);
}
private List<Button> createButtons()
{
List<Button> buttons = new List<Button>();
for (int i = 1; i <= 5; i++)
{
buttons.Add(
new Button()
{
Size = new Size(200, 35),
Enabled = true,
BackColor = GetColor(),
ForeColor = GetColor(),
UseVisualStyleBackColor = false,
Left = 20,
Top = (i * 40),
Text = String.Concat("Button ", i)
});
}
return buttons;
}
private Color GetColor()
{
return Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255));
}
}
I have an application in C# and using forms, I am placing a label each time I right click on the form. This label can be moved around, re-sized and modified its color.
So far so good, but I want to make a server that will receive everything I do and send this to other clients so they can see everything I do, and also they can do exactly the same things. I have made eventhandlers, but I have no idea how to send the information through the network, or what information to send to update the form for each client.
internal System.Windows.Forms.ContextMenu mnuForm;
internal System.Windows.Forms.MenuItem mnuNewSquare;
internal System.Windows.Forms.ContextMenu mnuLabel;
internal System.Windows.Forms.MenuItem mnuColorChange;
private void mnuNewSquare_Click(object sender, System.EventArgs e)
{
// Create and configure the "square".
Label newLabel = new Label();
newLabel.Size = new Size(40, 40);
newLabel.BorderStyle = BorderStyle.FixedSingle;
// To determine where to place the label, you need to convert the
// current screen-based mouse coordinates into relative form coordinates.
newLabel.Location = this.PointToClient(Control.MousePosition);
// Attach a context menu to the label.
newLabel.ContextMenu = mnuLabel;
// Connect the label to all its event handlers.
newLabel.MouseDown += new MouseEventHandler(lbl_MouseDown);
newLabel.MouseMove += new MouseEventHandler(lbl_MouseMove);
newLabel.MouseUp += new MouseEventHandler(lbl_MouseUp);
// Add the label to the form.
this.Controls.Add(newLabel);
}
// Keep track of when fake drag or resize mode is enabled.
private bool isDragging = false;
private bool isResizing = false;
// Store the location where the user clicked on the control.
private int clickOffsetX, clickOffsetY;
private void lbl_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Retrieve a reference to the active label.
Control currentCtrl;
currentCtrl = (Control)sender;
if (e.Button == MouseButtons.Right)
{
// Show the context menu.
currentCtrl.ContextMenu.Show(currentCtrl, new Point(e.X, e.Y));
}
else if (e.Button == MouseButtons.Left)
{
clickOffsetX = e.X;
clickOffsetY = e.Y;
if ((e.X + 5) > currentCtrl.Width && (e.Y + 5) > currentCtrl.Height)
{
// The mouse pointer is in the bottom right corner,
// so resizing mode is appropriate.
isResizing = true;
}
else
{
// The mouse is somewhere else, so dragging mode is
// appropriate.
isDragging = true;
}
}
}
private void lbl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Retrieve a reference to the active label.
Control currentCtrl;
currentCtrl = (Control)sender;
if (isDragging)
{
// Move the control.
currentCtrl.Left += e.X - clickOffsetX;
currentCtrl.Top += e.Y - clickOffsetY;
}
else if (isResizing)
{
// Resize the control.
currentCtrl.Width = e.X;
currentCtrl.Height = e.Y;
}
else
{
// Change the pointer if the mouse is in the bottom corner.
if ((e.X + 5) > currentCtrl.Width && (e.Y + 5) > currentCtrl.Height)
{
currentCtrl.Cursor = Cursors.SizeNWSE;
}
else
{
currentCtrl.Cursor = Cursors.Arrow;
}
}
}
private void lbl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isDragging = false;
isResizing = false;
}
private void mnuColorChange_Click(object sender, System.EventArgs e)
{
// Show color dialog.
ColorDialog dlgColor = new ColorDialog();
dlgColor.ShowDialog();
// Change label background.
mnuLabel.SourceControl.BackColor = dlgColor.Color;
}
private void DrawingSquares_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.ContextMenu.Show(this, new Point(e.X, e.Y));
}
}
This is the code for form1.cs, and the next code is for form1.designer.cs
private void InitializeComponent()
{
// this.SuspendLayout();
//
// Form1
//
this.mnuForm = new System.Windows.Forms.ContextMenu();
this.mnuNewSquare = new System.Windows.Forms.MenuItem();
this.mnuLabel = new System.Windows.Forms.ContextMenu();
this.mnuColorChange = new System.Windows.Forms.MenuItem();
//
// mnuForm
//
this.mnuForm.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuNewSquare});
//
// mnuNewSquare
//
this.mnuNewSquare.Index = 0;
this.mnuNewSquare.Text = "Create New Square";
this.mnuNewSquare.Click += new System.EventHandler(this.mnuNewSquare_Click);
//
// mnuLabel
//
this.mnuLabel.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuColorChange});
//
// mnuColorChange
//
this.mnuColorChange.Index = 0;
this.mnuColorChange.Text = "Change Color";
this.mnuColorChange.Click += new System.EventHandler(this.mnuColorChange_Click);
//
// DrawingSquares
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(628, 426);
this.ContextMenu = this.mnuForm;
this.Name = "DrawingSquares";
this.Text = "DrawingSquares";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawingSquares_MouseDown);
}
This does the client side of the application, in which you can draw a label and modify its properties. I need some help on how to do the server side, any help is much appreciated, thank you in advance.
You might want to look at SignalR:
A client and server side library for .NET that provides messaging and an abstraction over a persistent connection.