I want to be able to declare multiple separate pictureboxes with different positions, backgroundcolors and tags. I would like to use the tags to refer to the pictureboxes in my code, to change and/or remove them individually.
How do I go about this?
I have attempted to create a separate class, meant for creating the same picturebox as a public static but encountered some issues with the trbRed.Value values, since they don't exist in that class. I am not sure if this solution would work for my cause, and neither do I have a clue on how to create the class either way.
This program uses drag and drop, and therefore uses the mousepositions
to create pictureboxes with different backcolors.
picBlock is declared earlier in the class with
PictureBox picBlock = new PictureBox();.
private void myPanel_MouseMove(object sender, MouseEventArgs e)
{
int mouseX = e.X / 25 + 1;
int mouseY = (myPanel.Size.Height - e.Y) / 25 + 1;
if (myPanel.BackgroundImage != null)
{
lblMousePos.Show();
lblMousePos.Text = "pos (" + mouseX + ", " + mouseY + ")";
}
}
public void myPanel_DragDrop(object sender, DragEventArgs e)
{
picBlock = new PictureBox
{
BackColor = Color.FromArgb(255, trbRed.Value,
trbGreen.Value, trbBlue.Value),
Image = Image.FromFile("my picture here.png"),
Size = new Size(25, 24),
Location = new Point((mouseX - 1) * 25,
this.myPanel.Height - mouseY * 24),
SizeMode = PictureBoxSizeMode.Zoom,
Tag = mouseX.ToString() + ", " + mouseY.ToString()
};
listX_pos.Add(mouseX);
listY_pos.Add(mouseY);
picBlock.MouseMove += picBlock_mousemove;
picBlock.MouseDown += picBlock_mousedown;
this.myPanel.Controls.Add(picBlock);
}
public void picBlock_mousedown (object sender, MouseEventArgs e)
{
this.myPanel.Controls.Remove(picBlock);
for (int i = 0; i < listX_pos.Count; i++)
{
if (listX_pos[i] == ((picBlock.Location.X / 25) + 1) && listY_pos[i] == (this.myPanel.Location.Y - picBlock.Location.Y / 25) - 11)
{
listX_pos.RemoveAt(i);
listY_pos.RemoveAt(i);
}
}
//pcbPicture is a picturebox with the same content of "my picture here.png"
picBlock.DoDragDrop(pcbPicture.Image, DragDropEffects.Move);
}
public void picBlock_mousemove(object sender, MouseEventArgs e)
{
if (myPanel.BackgroundImage != null)
{
//a label to show the position of the mouse
lblMousePos.Show();
Point point = myPanel.PointToClient(MousePosition);
int mouseX = point.X / 25 + 1;
int mouseY = (myPanel.Size.Height - point.Y) / 25 + 1;
lblMousePos.Text = "pos (" + mouseX + ", " + mouseY + ")";
}
}
I have no issues creating the pictureboxes, but can only change the position of the most recently created picturebox from picBlock_mousemove as well as picBlock_mousedown voids.
I want to be able to change the position for any of the created pictureboxes as well as their own controls (mousemove and mousedown).
As #rene said, your problem is that in your picBlock mouse handlers, you refer to picBlock, which contains only the last image created.
The solution is in the event. Each event in .NET, has two properties, by convention: object sender and System.EventArgs e.
e is the more understable; It's a class drived from System.EventArgs (or itself) that contains some required information about the event: the pressed mouse button/keyboard key, mouse X and Y, and so on - any relevant information.
sender is less usable. It contains the object that triggered the event.
Why? Can't we access it directly, such as by member variable?
The answer is your (and similar) cases: we attach a single method to events of some objects, then we want to know on which element to work. For example, a simple example that pops up a messagebox every time you click a button, with it's content:
private void Form_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
var btn = new Button
{
Text = "Button #" + i, Top = i * 50, Left = 0
};
btn.Click += btn_Click;
this.Controls.Add(btn);
}
}
private void btn_Clicked(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Text); // Access the clicked button by `sender`
}
Returning to your question, use it like (you don't need a member variable picBlock):
private void myPanel_MouseMove(object sender, MouseEventArgs e)
{
if (myPanel.BackgroundImage != null)
{
lblMousePos.Show();
lblMousePos.Text = "pos (" + (e.X / 25 + 1) + ", " + ((myPanel.Size.Height - e.Y) / 25 + 1) + ")";
}
}
public void myPanel_DragDrop(object sender, DragEventArgs e)
{
var picBlock = new PictureBox
{
BackColor = Color.FromArgb(255, trbRed.Value,
trbGreen.Value, trbBlue.Value),
Image = Image.FromFile("my picture here.png"),
Size = new Size(25, 24),
Location = new Point((mouseX - 1) * 25,
this.myPanel.Height - mouseY * 24),
SizeMode = PictureBoxSizeMode.Zoom,
//Tag = musX.ToString() + ", " + musY.ToString()
// You don't need the Tag property
};
picBlock.MouseMove += picBlock_mousemove;
picBlock.MouseDown += picBlock_mousedown;
this.myPanel.Controls.Add(picBlock);
}
public void picBlock_mousedown (object sender, MouseEventArgs e)
{
var picBlock = (PictureBlock)sender;
this.panelSpel.Controls.Remove(picBlock);
for (int i = 0; i < listX_pos.Count; i++)
{
if (listX_pos[i] == ((picBlock.Location.X / 25) + 1) && listY_pos[i] == (this.myPanel.Location.Y - picBlock.Location.Y / 25) - 11)
{
listX_pos.RemoveAt(i);
listY_pos.RemoveAt(i);
}
}
//pcbPicture is a picturebox with the same content of "my picture here.png"
picBlock.DoDragDrop(pcbPicture.Image, DragDropEffects.Move);
}
public void picBlock_mousemove(object sender, MouseEventArgs e)
{
var picBlock = (PictureBlock)sender;
if (myPanel.BackgroundImage != null)
{
//a label to show the position of the mouse
lblMousePos.Show();
Point point = myPanel.PointToClient(MousePosition);
int mouseX = point.X / 25 + 1;
int mouseY = (myPanel.Size.Height - point.Y) / 25 + 1;
lblMousePos.Text = "pos (" + musX + ", " + musY + ")";
}
}
Related
I have a problem with event management (MouseMove, MouseDown and MouseClick). I created the label dynamically (this number of labels depend on the result of a SQL query) the pobléme I want when i move this labels created dynamically i want start MouseMove function ... here is the code I used:
int count;
OleDbCommand cmd = new OleDbCommand();
cnx.Open();
cmd.Connection = cnx;
string mois = cmbMat.SelectedValue.ToString();
cmd.CommandText = "select count(*) from Table Where Month(Date)=" + mois;
count = (int)(cmd.ExecuteScalar());
int i =count;
Label Jalon;
for(i=0,i<count,i++)
{
Jalons = new Label();
this.Jalons.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MouseClickLab);
this.Jalons.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownLab);
this.Jalons.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMoveLab);
this.Controls.Add(Jalons);
}
so how I will specify the label in the function mousemoveLab,MouseDownLab and MouseMoveLab?? i thinking for this :
private void label1_MouseMove(object sender, MouseEventArgs e)
{
//Text = e.Location.X + ":" + e.Location.Y;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.ActiveControl.Left = e.X + this.ActiveControl.Left - MouseDownLocation.X;
this.ActiveControl.Top = e.Y + this.ActiveControl.Top - MouseDownLocation.Y;
this.ActiveControl.Height;
}
}
You can use the sender that you receive in the event to identify the label.
private void label1_MouseMove(object sender, MouseEventArgs e)
{
Label label = (Label)sender;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
label.Left = e.X + label.Left - MouseDownLocation.X;
label.Top = e.Y + label.Top - MouseDownLocation.Y;
}
}
i got the picture box to move. But when i click down on the picture box it jumps all over the place. Can anyone help me with this at all?
namespace Move_Shapes
{
public partial class Form1 : Form
{
int X = 0;
int Y = 0;
int mX = 0;
int mY = 0;
bool Move = false;
public Form1()
{
InitializeComponent();
}
private void pic_TL_Click(object sender, EventArgs e)
{
//X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
//Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
//label1.Text = X.ToString();
//label2.Text = Y.ToString();
//X = MousePosition.X - this.Location.X - 8;
//Y = MousePosition.Y - this.Location.Y - 30;
//label3.Text = X.ToString();
//label4.Text = Y.ToString();
}
private void pic_TL_MouseDown(object sender, MouseEventArgs e)
{
X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
label1.Text = X.ToString();
label2.Text = Y.ToString();
Move = true;
}
private void pic_TL_MouseMove(object sender, MouseEventArgs e)
{
mX = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
mY = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
if (Move)
{
pic_TL.Location = new Point(mX - X, mY - Y);
}
}
private void pic_TL_MouseUp(object sender, MouseEventArgs e)
{
Move = false;
}
}
}
On MoseMove you set location to difference of current mouse position and initial mouse position.
pic_TL.Location = new Point(mX - X, mY - Y);
I case mouse moved over one pixel, picture will move to left-top corner.
I have a TableLayoutPanel and I want to add a control to the cell that I click on.
The problem is that I can't determine the cell that I click on at run time.
How do I determine the cell being clicked on?
You can use GetColumnWidths and GetRowHeights methods to calculate the cell row and column index:
Point? GetRowColIndex(TableLayoutPanel tlp, Point point)
{
if (point.X > tlp.Width || point.Y > tlp.Height)
return null;
int w = tlp.Width;
int h = tlp.Height;
int[] widths = tlp.GetColumnWidths();
int i;
for (i = widths.Length - 1; i >= 0 && point.X < w; i--)
w -= widths[i];
int col = i + 1;
int[] heights = tlp.GetRowHeights();
for (i = heights.Length - 1; i >= 0 && point.Y < h; i--)
h -= heights[i];
int row = i + 1;
return new Point(col, row);
}
Usage:
private void tableLayoutPanel1_Click(object sender, EventArgs e)
{
var cellPos = GetRowColIndex(
tableLayoutPanel1,
tableLayoutPanel1.PointToClient(Cursor.Position));
}
But notice that the click event only is raised if the cell does not already contain a control.
This worked for me:
public TableLayoutPanel tableLayoutPanel { get; set; }
private void Form_Load(object sender, EventArgs e)
{
foreach (Panel space in this.tableLayoutPanel.Controls)
{
space.MouseClick += new MouseEventHandler(clickOnSpace);
}
}
public void clickOnSpace(object sender, MouseEventArgs e)
{
MessageBox.Show("Cell chosen: (" +
tableLayoutPanel.GetRow((Panel)sender) + ", " +
tableLayoutPanel.GetColumn((Panel)sender) + ")");
}
Note that my tableLayoutPanel is declared globally so that I can just use it without having to pass it to each function. Also, both the tableLayoutPanel and each Panel within it are created completely programatically elsewhere (my form [design] is completely blank).
My answer is based on #Mohammad Dehghan's answer above but has a couple of advantages:
It now takes into account vertical scrolling
The columns are in the correct order (starts at i=0 instead of i=length), meaning columns of different widths or heights are processed in the correct order
Here is the updated version of the code:
public Point? GetIndex(TableLayoutPanel tlp, Point point)
{
// Method adapted from: stackoverflow.com/a/15449969
if (point.X > tlp.Width || point.Y > tlp.Height)
return null;
int w = 0, h = 0;
int[] widths = tlp.GetColumnWidths(), heights = tlp.GetRowHeights();
int i;
for (i = 0; i < widths.Length && point.X > w; i++)
{
w += widths[i];
}
int col = i - 1;
for (i = 0; i < heights.Length && point.Y + tlp.VerticalScroll.Value > h; i++)
{
h += heights[i];
}
int row = i - 1;
return new Point(col, row);
}
Nick's answer was the best solution, except that it can be made generic for TableLayoutPanels that contain different kinds of controls in the cells. Just change the explicit "Panel" type to "Control":
public TableLayoutPanel tableLayoutPanel { get; set; }
private void Form_Load(object sender, EventArgs e)
{
foreach (Control c in this.tableLayoutPanel.Controls)
{
c.MouseClick += new MouseEventHandler(ClickOnTableLayoutPanel);
}
}
public void ClickOnTableLayoutPanel(object sender, MouseEventArgs e)
{
MessageBox.Show("Cell chosen: (" +
tableLayoutPanel.GetRow((Control)sender) + ", " +
tableLayoutPanel.GetColumn((Control)sender) + ")");
}
This works great and doesn't require doing coordinate math to find which cell was clicked.
What i mean is that i want to activate the mouse leave event only when the user is moving the mouse pointer out of the pictureBox1 client area. This happens only if he presses down the left mouse button but not if the user doesn't press the left mouse button, i.e he can move the mouse around free and the event wont do anything.
I have a variable in the top of Form1 called mouseLeave type bool.
In constructor i made it to be false;
In the pictureBox1 mouse down event i did the mouseLeave variable to be true.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Visible = true;
label4.Visible = true;
// find the index that is closest to the current mouse location
float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);
if (t == -1)
{
button3.Enabled = false;
}
else
{
button3.Enabled = true;
{
selectedIndex = t;
mouseMove = true;
mouseLeave = true;
In the pictureBox1 mouse move event i check if mouseMove is true then move the point drag it around:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
{
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
pictureBox1.Refresh();
}
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}
So when the user click on the mouse left button without leave it and drag the point then the point will move aorund the pictureBox1 client area.
Now in the pictureBox1 mouse leave event i did:
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (mouseLeave == true)
{
mouseMove = false;
}
}
But it dosent work. I add a point drag it move it around but this event activate do something only when i move the mouse pointer out of the pictureBox1 area without dragging the point only when im not clicking down the left mouse button.
What i want is that only when i click down the mouse left button and move it around like in the mouse move event only then this leave event will do something in this case will make mouseMove to be false.
So the user will not be able to drag the point out of the pictureBox1 area. What should i do in the mouse leave event then ?
EDITED**
This the button1 click where i add each time a new point to the pictureBox1 area.
And the paint event where i draw the points.
Maybe this will help to solve the problem where it stop but not in the right places when drag out of bounds of pictureBox.
private void button1_Click(object sender, EventArgs e)
{
halfX = pictureBox1.ClientRectangle.Width / 2;
halfY = pictureBox1.ClientRectangle.Height / 2;
Random rnd = new Random();
offsetX = rnd.Next(-10, 10);
offsetY = rnd.Next(-10, 10);
wireObject1.addPoint(halfX + offsetX, halfY + offsetY);
if (wireObjectCoordinates1 == null)
wireObjectCoordinates1 = new WireObjectCoordinates() { FrameNumber = currentFrameIndex };
wireObjectCoordinates1.Point_X.Add(halfX + offsetX);
wireObjectCoordinates1.Point_Y.Add(halfY + offsetY);
wireObjectAnimation1._coordinatesList.Add(wireObjectCoordinates1);
pictureBox1.Refresh();
numberOfPoints++;
label5.Text = "{X = " + (halfX + offsetX) + "}" + " " + "{Y = " + (halfY + offsetY) + "}";
label5.Visible = true;
label7.Visible = true;
label16.Text = numberOfPoints.ToString();
label16.Visible = true;
label15.Visible = true;
buttonLockMode = true;
button8.Enabled = true;
button4.Enabled = true;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Red);
Pen p = new Pen(brush);
for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);
// g.FillEllipse(brush, rect);
}
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
int startIndex = wireObject1._connectionstart[i];
int endIndex = wireObject1._connectionend[i];
connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
}
}
I think i solved it by changing the if in the mouse move to this:
if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) ||
!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5)))
So the move event should look like this:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
mouseDrag = true;
Point NewPoint = e.Location;
if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) ||
!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5)))
{
if (mouseDrag)
{
mouseMove = false;
return;
}
}
{
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
pictureBox1.Refresh();
}
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}
Instead of using the MouseLeave event you can check to see if the Mouse Location is in your PictureBox's Client Rectangle in your MouseMove Event, something like this.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint))
{
if (mouseLeave)
{
mouseMove = false;
return;
}
}
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
pictureBox1.Refresh();
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}
}
Do something like this:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.MouseMove -= OnMouseMove;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
pictureBox1.MouseMove += OnMouseMove;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
Debug.WriteLine(e.Location);
}
Where "OnMouseMove" is not attached to pictureBox event by default.
I have a Multiline richtextbox control into which i want to integrate the feature of adding a line number. i have considered many approaches
Add a label and updating the line numbers as the line count changes
Add a picturebox along with to draw string on it.
Add another textbox along with and show line numbers on it
Add listbox along and display line numbers in it.
I got two doubts.
The richtextbox which i'm using is a custom made control and derieves from RichTextBox class. How can i add multiple controls to it.
What is the best approach to show line numbers for the multiline text in c#
My own example. All is fine, but wordwrap must be disabled :(
int maxLC = 1; //maxLineCount - should be public
private void rTB_KeyUp(object sender, KeyEventArgs e)
{
int linecount = rTB.GetLineFromCharIndex( rTB.TextLength ) + 1;
if (linecount != maxLC)
{
tB_line.Clear();
for (int i = 1; i < linecount+1; i++)
{
tB_line.AppendText(Convert.ToString(i) + "\n");
}
maxLC = linecount;
}
}
where rTB is my richtextbox and tB is textBox next to rTB
J.T. jr
this code helped me thank you, needed to convert visual basic but could:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim maxlc As Integer = 1
Dim linecount As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Height) + 1
If linecount <> maxlc Then
TextBox2.Clear()
For i = 0 To linecount - 1 Step 1
TextBox2.AppendText(Convert.ToString(i) + vbNewLine)
Next i
maxlc = linecount
End If
End Sub
public int getWidth()
{
int w = 25;
// get total lines of richTextBox1
int line = richTextBox1.Lines.Length;
if (line <= 99)
{
w = 20 + (int)richTextBox1.Font.Size;
}
else if (line <= 999)
{
w = 30 + (int)richTextBox1.Font.Size;
}
else
{
w = 50 + (int)richTextBox1.Font.Size;
}
return w;
}
public void AddLineNumbers()
{
// create & set Point pt to (0,0)
Point pt = new Point(0, 0);
// get First Index & First Line from richTextBox1
int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
// set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
pt.X = ClientRectangle.Width;
pt.Y = ClientRectangle.Height;
// get Last Index & Last Line from richTextBox1
int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
// set Center alignment to LineNumberTextBox
LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
// set LineNumberTextBox text to null & width to getWidth() function value
LineNumberTextBox.Text = "";
LineNumberTextBox.Width = getWidth();
// now add each line number to LineNumberTextBox upto last line
for (int i = First_Line; i <= Last_Line + 2; i++)
{
LineNumberTextBox.Text += i + 1 + "\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
if (pt.X == 1)
{
AddLineNumbers();
}
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
LineNumberTextBox.Text = "";
AddLineNumbers();
LineNumberTextBox.Invalidate();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Text == "")
{
AddLineNumbers();
}
}
private void richTextBox1_FontChanged(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
{
richTextBox1.Select();
LineNumberTextBox.DeselectAll();
}
private void Form1_Resize(object sender, EventArgs e)
{
AddLineNumbers();
}
WORKS 100%!!! But you need to add richTextBox2 for line numbers, if you want change it to other
form like listbox, anyway it served me well.
private void richTextBox1_keyDown(object sender, KeyEventArgs e)
{
for (int i = 0; i <= richTextBox1.Lines.Count(); i++)
{
if (!(e.KeyCode == Keys.Back))
{
if (!richTextBox2.Text.Contains(i.ToString()))
{
richTextBox2.Text += i.ToString() + "\n";
}
}
else
{
richTextBox2.Clear();
}
}
}