I want to move the location of label vertically or horizontally and it should appear from in the windows form and should be invisible at a location by moving. I want to make it through .net application using c# so can any one help me for this?
Buddy, you can use the property Location
MyLabel.Location.x = ??
MyLabel.Location.y = ??
Then hide it by using the propertyVisible
MyLabel.Visible = false`
It's difficult to tell exactly what you're trying to accomplish here. I'm going to assume that you want to move a label from its current position on the form to a new position while the application is running. I also assume that you want to make the label invisible while you're moving it so that you cannot see it move across the form.
You can do this easily by setting the Location property of the label you want to move to its new location. (If necessary, like if you want to move the label a relative number of pixels, you can get the label's current position from the Location property before you set it.) The label control also has a Visible property that you can set to True or False to show/hide the control, respectively:
//Hide the label first
myLabel.Visible = false;
//Move the label to a new location on the form
myLabel.Location = new Point(30, 25);
//Make the label visible again
myLabel.Visible = true;
If I guessed wrong, and you're just trying to move the label during design-time (before you start running your program), you can just drag-and-drop it to a new position on the form.
you just drag and drop
You can make a label invisible by using the Visible Property.
myHiddenLabel.Visible = false;
Related
I have a windows form that I can drag around programmatically created control labels. I am using the location property to determine each label position.
Each label is created as a preview with placeholder '#' where a number is to be. I want to know what the form coordinates are of a specific character in that label.
As a code example:
Label myLabel = new Label
{
Text = "Average: ##.#Unit",
Name = "myLabelName"
};
Controls.Add(myLabel);
...
// After label is dragged around
Point myLabelLocation = myLabel.Location;
This gives me the label's coordinates. Let's say I want similar coordinates, but I want them for the first #.
Another way to look at this, I "split" the label right before '#'. How do I get this location?
I was trying to Load a pictureBox in run time. There is no run time error. But the PictureBox does not show up.
Later on I also added InitializeComponent(), but it did not help.
My Code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox picLoadingNew = new PictureBox();
picLoadingNew.BackColor = System.Drawing.Color.Transparent;
picLoadingNew.Image = global::QuiDip.Properties.Resources.ajax_loader_Long;
picLoadingNew.Location = new System.Drawing.Point(790, 760);
picLoadingNew.Name = "picLoadingNew";
picLoadingNew.Size = new System.Drawing.Size(142, 22);
picLoadingNew.Dock = DockStyle.Fill;
//InitializeComponent();
this.Controls.Add(picLoadingNew);
picLoadingNew.Show();
picLoadingNew.Visible = true;
}
You wont need .Show or .Visible lines as this would be the default anyway, but you do certainly need the this.Controls.Add(picLoadingNew) - as long as the form is going to be the parent and not a Panel or anything else. If the PictureBox is sitting on top of another control like a panel, you actually need: panel.Controls.Add(picLoadingNew) - and not your current this.Controls.Add(picLoadingNew) line.
What I would try first as I can't see anything too wrong with your code is, change the default location of your picture box to:
picLoadingNew.Location = new System.Drawing.Point(5, 5);
And remove the .Dock line.
This might be setting the PictureBox off screen (depending on the form's size?) - you haven't provided this so guessing what could be wrong. If the PictureBox is off screen, the new coordinates will force it to be top-left - so you should definitely see it then. Once you know it's working, you can then move it into position and set anchors later once you've proved the code is actually working.
You might find setting .SizeMode = StretchImage might be useful incase you're image is too large and again is "off screen". StretchImage will force the image to resize to fit nicely inside your PictureBox with the current .Size dimensions that you provided.
Something like:
PictureBox picLoadingNew = new PictureBox();
picLoadingNew.BackColor = System.Drawing.Color.Transparent;
picLoadingNew.Image = global::QuiDip.Properties.Resources.ajax_loader_Long;
picLoadingNew.Location = new System.Drawing.Point(5, 5);
picLoadingNew.Size = new System.Drawing.Size(142, 50);
picLoadingNew.SizeMode = PictureBoxSizeMode.StretchImage;
parent.Controls.Add(picLoadingNew);
The code above worked for me (using my own image of course!).
Hope that helps.
Your code should work but there is a little problem. If your image is exactly positioned at the same location of another image and its size is identical, you can't see it. It is located behind the current image on screen.
Remove the Show and Visible call but add a call to BringToFront
this.Controls.Add(picLoadingNew);
// picLoadingNew.Show();
// picLoadingNew.Visible = true;
picLoadingNew.BringToFront();
By the way, what is the point in giving a specific size to your new image if then you ask it to Fill the available space on its container?
I have added an View List onto a Panel like this:
panelComponent.Controls.Add(viewListComponent);
Everything works just fine. Mouse events are handled, repainting works. But one problem: I can't move it around dynamically. If I change the control.Top variable, it just sits there without moving.
It's like the control is glued to the top left corner. Resizing the right and bottom properties works just fine! I did it without dynamically adding and then no problem.
What could be causing this, and how do I fix it?
Two possible explanations. First is the Dock property, docking it to the top keeps the control at the top of the container, no matter what you assign to the Top or Location properties.
The other one is value types, the Location property is Point, a struct. This code will not move the control:
var lbl = new Label();
panel1.Controls.Add(lbl);
var pos = lbl.Location;
pos.Y = 42; // No effect
Try using the Location property:
viewListComponent.Location = new Point(42, 42);
In the process of translating an application with C# + Winforms, I need to change a button's text depending on the language.
My problem is the following :
Let's say I want to translate a button from "Hi all!" to "Bonjour tout le monde" !
As you can guess, the button's size won't be the same if I enter english text or french one... My question is "simple", how can I manage to resize the button on the fly so the text fits its content in the button ?
So far I got something like that !
[Hi all!]
[Bonjour]
There's absolutely no need to use the underlying Graphics object as the other posters have said.
If you set the button's AutoSize property to true, the AutoSizeMode to GrowAndShrink, and the AutoEllipsis to false, it will resize automatically to fit the text.
That being said, you may need to make several layout adjustments to make this change fit into your UI. You can adjust the button's padding to add space around the text, and you may want to place your buttons in a TableLayoutPanel (or something) to stop them from overlapping when they resize.
Edit:
#mastro pointed out that: AutoEllipsis is only valid when AutoSize is false (As explained in the documentation), so it can be safely ignored as long as the other three properties are set correctly.
Your best bet is to set the AutoSize property as described ach's answer
However if AutoSize isn't working for you, resizing the button in code is easy enough. You can just need to set the button's width. The trick is making it big enough to fit your text.
using(Graphics cg = this.CreateGraphics())
{
SizeF size = cg.MeasureString("Please excuse my dear aunt sally",this.button1.Font);
// size.Width+= 3; //add some padding .net v1.1 and 1.0 only
this.button1.Padding = 3;
this.button1.Width = (int)size.Width;
this.button1.Text = "Please excuse my dear aunt sally";
}
Try this:
Button.AutoSize = true;
Button.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Button.TextAlign = ContentAlignment.MiddleLeft;
Button.Padding = new Padding(0, 0, 0, 0);
To enable a Button in WinForms grow and/or shrink depending on the size of the Text, you need to set the button's AutoSize property to True and the AutoSizeMode property to GrowAndShrink.
// C#
btn.AutoSize = true;
btn.AutoSizeMode = AutoSizeMode.GrowAndShrink;
' VB.NET
btn.AutoSize = True
btn.AutoSizeMode = AutoSizeMode.GrowAndShrink
Please note that the AutoSize property will only allow the button's size to grow if the AutoSizeMode property is set to GrowOnly; by changing the AutoSizeMode property to GrowAndShrink, the button will now automatically extend or reduce in width and height based on its Text property.
Also note that in setting the two properties as shown above, you can make use of new lines (Environment.NewLine or vbCrLf) in the Text property and the button will scale down as needed.
As Andrew Hanlon explains, you can set AutoSize = true.
When doing so, you can also attain a perfect layout of the buttons automatically by placing them on a FlowLayoutPanel.
The horizontal distance between them will always stay the same when the FlowDirection of the FlowLayoutPanel is LeftToRight or RightToLeft. You can adjust this distance by setting the Margin property of the buttons appropriately. You can create groups of buttons by increasing the left margin of buttons beginning a new group.
If you set the Dock property of the buttons to DockStyle.Fill, they will even grow their width automatically in order to fit to the widest button if the FlowDirection of the FlowLayoutPanel is TopDown or BottomUp.
btn.AutoSizeMode = AutoSizeMode.GrowOnly;
btn.AutoSize = true;
btn.Dock = DockStyle.Fill;
In addition to setting the AutoSize to true and the AutoSizeModeto GrowAndShrink, as suggested in the other answers, you may also need to set the TextImageRelation property, if you have set the button image, so that the text doesn't overlap the image.
I'm concatenating a string that sometimes is long enough for it not to fit in a label control. How can i make it autoscroll to the rightside so i always see the end of the string?
While I'm sure there are ways of doing, I have to ask, why? I think it would look and/or work very badly and probably confuse the user.
Why not have the text get trimmed with an ellipse (...) at the end and show a tooltip on the label?
using System.Windows.Forms;
var label = new Label();
label.AutoSize = false;
label.AutoEllipsis = true;
label.Text = "This text will be too long to display all together.";
var labelToolTip = new ToolTip();
labelToolTip.SetToolTip(label, label.Text);
Now the tooltip will show the full text when the user hovers over it. Since the text in the label will be truncated and end in an ellipse, the user should know to hover over for more info (usually the standard way).
The TextAlign property allows you to specify the alignment. If you right-justify it with this, the right side of the text will always be visible. However, if you want it to be left or center justified and still have the behavior you describe, I suspect you will need to perform some measurement using Graphics.MeasureString to determine if the text fits and change the alignment dynamically.
AFAIK there's no way to scroll a label. A hack would be to use a TextBox (read-only, turn off border) then use SendKeys.Send() to move the cursor to the end of the text. Something like:
textBox1.Focus();
SendKeys.SendWait("{END}");
To get the text to not show up as selected I had to change it's position in the tab order (so that it wasn't 1) but that may not be a problem in your case.