I am new to C# and want to change the size of some Pictureboxes based on one trackbar Value.
If i write the below code refering to a spesific picturebox (e.g. Picturebox1 instead of PB) it works, but i would like to use one Doubleclick event for all Pictureboxes that i Doubleclick.
The below code gives PB = null.
I get the selected Pictureboxname but how can i refer to this Picturebox?
'''
private void PictureBoxesDoubleClick(object sender, EventArgs e)
{
//get the selected Picturebox name
String PictureBoxName = ((PictureBox)sender).Name;
//This part doesn't work
PictureBox PB = (PictureBox)this.Controls[PictureBoxName];
//Resize the Picture box according to the trackBar Value
PB.Size = new Size(trackBar1.Value, trackBar1.Value);
PB.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
PB.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
}
'''
Which pictureBox was selected? C#
Thank you
This is the common function which I used for the two picture boxes on their double click and it is working fine. Check if you also had a common function for all the picture boxes in the form.
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
//get the selected Picturebox name
String PictureBoxName = ((PictureBox)sender).Name;
PictureBox PB = (PictureBox)this.Controls[PictureBoxName];
//Resize the Picture box according to the trackBar Value
PB.Size = new Size(trackBar1.Value*10, trackBar1.Value*10);
PB.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
PB.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
}
Related
I created a line map and save it as jpeg, I would like to use it for my windows application in c#.
What I wanted to do is click on the specific area of image and and add a pushpin on it and add some information, so that I can click on it later to reveal the information on it. Is this possible?
It's possible to get a specific area inside your picturebox when mouse is clicked. You can do it with this:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int xCoordinate = e.X;
int yCoordinate = e.Y;
var picture = new PictureBox
{
Size = new Size(50, 50),
Location = new Point(e.X, e.Y),
Image = Image.FromFile("pushpin.png"),
SizeMode = PictureBoxSizeMode.StretchImage
};
this.Controls.Add(picture);
picture.BringToFront();
}
UPDATE: I've also added how you should add the image.
This should be the result:
Hope it helps!
I am begin to study in Wpf, I want to use slider but I want to custom the slider control like image below:
The value of slider will be some of column with height increase like chart, and default column background color is black. When User drag and move from left to right, the column background of left side will be green color and opposite, the color will be black again. Please let me know if my question is not clear.
I found the solution by myself. I have two image stack up together, green background image is at bottom and black background image is at top. I have two event: MouseMove and MouseDown on image will get the position of mouse and set the opacity mask of top image. Opacity mask will set a part of image to transparent background. Of course, the bottom image will be display. See code below .
private void imgMusicBlack_MouseDown(object sender, MouseButtonEventArgs e)
{
var img = sender as Image;
SetOpacityMask(img, e.GetPosition(img).X);
}
private void imgMusicBlack_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var img = sender as Image;
SetOpacityMask(img, e.GetPosition(img).X);
}
}
private void SetOpacityMask(Image img, double pointX, double offset = -1)
{
if (offset == -1)
offset = Math.Round(pointX / img.ActualWidth, 2);
LinearGradientBrush linear = new LinearGradientBrush();
linear.StartPoint = new Point(0, 0.5);
linear.EndPoint = new Point(1, 0.5);
linear.GradientStops = new GradientStopCollection();
linear.GradientStops.Add(new GradientStop(Colors.Transparent, offset));
linear.GradientStops.Add(new GradientStop(Colors.Black, offset));
img.OpacityMask = linear;
}
I want to erase image loaded on canvas on mouse move and display the background image in wp7.
in C# MakeTransparent method is available.
But in windows phone no such method is available.
what to do ?
Canvas.Opacity = 0; //This will make the control transparent
Also you can use .Opacity function almost every visual user control (Image, Grid, Lists etc.)
Cleverly, you can use canvas_MouseMove event for an eraser effect.
Just subscribe to canvas mousemove event like in see the example below
//Canvas MouseMove Event
private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
currentPoint = e.GetPosition(this.canvas);
//Initialize line according to currentpoint position.
Line line = new Line() { X1 = currentPoint.X, Y1 = currentPoint.Y, X2 = oldPoint.X, Y2 = oldPoint.Y };
line.StrokeDashCap = PenLineCap.Round;
line.StrokeEndLineCap = PenLineCap.Round;
line.StrokeLineJoin = PenLineJoin.Round;
line.StrokeThickness = 10;
line.Stroke = new SolidColorBrush(Colors.White) ;
////////////////////////////////
//Set color & thickness of line.
//Line add in canvas children to draw image & assign oldpoint.
this.canvas.Children.Add(line);
oldPoint = currentPoint;
}
Not too sure with it but still, I hope it helps.
How can I get PropertyGrid's TextBox from specified field?
I need this TextBox to set Pointer to the end of text.
var num = txtBox.GetCharIndexFromPosition(Cursor.Position);
txtBox.SelectionStart = num + 1;
txtBox.SelectionLength = 0;
So how can I get this TextBox from PropertyGrid?
Also, property in PropertyGrid is read-only.
If what you want is the cursor to be located right after the last character written in the textbox, you can rely on the following code (triggered by the TextChanged Event of the TextBox):
private void txtBox_TextChanged(object sender, EventArgs e)
{
int newX = txtBox.Location.X + TextRenderer.MeasureText(txtBox.Text, txtBox.Font).Width;
int newY = txtBox.Bottom - txtBox.Height / 2;
if (newX > txtBox.Location.X + txtBox.Width)
{
newX = txtBox.Location.X + txtBox.Width;
}
Cursor.Position = this.PointToScreen(new Point(newX, newY));
}
Bear in mind that its Y position is always in the middle.
----- UPDATE AFTER THE KINGKING COMMENT
As far as the code in the question was referred to the TextBox, I focused my answer on the TextBox. Nonetheless, KingKing is right and the PropertyGrid has to be brought into consideration. Below these lines I adapted the code you can find in MSDN for PropertyGrid:
private void Form1_Load(object sender, EventArgs e)
{
PropertyGrid propertyGrid1 = new PropertyGrid();
propertyGrid1.CommandsVisibleIfAvailable = true;
propertyGrid1.Location = new Point(10, 20);
propertyGrid1.Size = new System.Drawing.Size(400, 300);
propertyGrid1.TabIndex = 1;
propertyGrid1.Text = "Property Grid";
this.Controls.Add(propertyGrid1);
propertyGrid1.SelectedObject = txtBox;
}
After txtBox is added to propertyGrid1, its position is updated and thus the original code can be used without any problem.
In summary, the idea is not looking for the TextBox inside the PropertyGrid, but accessing directly the TextBox control (which is added at runtime to the PropertyGrid).
1)
I develop a c# user control.
In that control, I have a button. When the user clicks the button at runtime, a new control (for example, pictureBox) is created ,next to the previous pictureBox.
I did it that way:
PictureBox pb = new PictureBox();
pb.Location = new Point(oldPb.X, oldPb.Y + 100);
pb.Size = oldPb.Size;
Controls.Add(pb);
The problem is, that I want to be able to manage all of the created items.
I want, for example, to index the pictureBoxes, then get a number from the user and change the photo of the wanted photoBox.
for example:
photoBox3.Image = .......
How can I do it?
2)
I want to be able to recognize when the user clicks on one of those photoBoxes and do an action on the chosen photoBox.
How can I do that?
Thanks
List<PictureBox> pictureBoxes = new List<PictureBox>();
for (int i = 0; i < 10; i++)
{
PictureBox pb = new PictureBox();
pb.Location = new Point(.....);
pb.Size = ......;
pb.Click += pb_Click;
Controls.Add(pb);
pictureBoxes.Add(pb);
}
pictureBoxes[3].Image=..... //Use like this
void pb_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
//Do work
}
You can use the Tag Property of the PictureBox to store some kind of index.
You can then have all your PictureBoxes respond to a click event:
pb.Click += new EventHandler(picturebox_Click);
and check the Tag there
private void picturebox_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
if (pb != null)
{
string s = pb.Tag
}
}