c# - Set Text to the lowest right edge of a label? -
easy example, lets i'm creating label that:
label label = new label(); label.text = "hello" + "20.50"; label.width = 250; label.height = 100; panel1.controls.add(label);
how "20.50" should appear in lowest right edge of label?
for clarity made little example in word:
how achieve this? appreciated!
there's no built-in support label control. you'll need inherit label create custom control, , write painting code yourself.
of course, you'll need way differentiate between 2 strings. +
sign, when applied 2 strings, concatenation. 2 strings joined compiler, this: hello20.50
. either need use 2 separate properties, each own strings, or insert sort of delimiter in between 2 strings can use split them apart later. since you're creating custom control class, i'd go separate properties—much cleaner code, , harder wrong.
public class cornerlabel : label { public string text2 { get; set; } public cornerlabel() { // label doesn't support autosizing because default autosize logic // knows primary caption, not secondary one. // // either have set size manually, or override // getpreferredsize function , write own logic. not // hard do: use textrenderer.measuretext determine space // required both of strings. this.autosize = false; } protected override void onpaint(painteventargs e) { // call base class paint regular caption in top-left. base.onpaint(e); // paint secondary caption in bottom-right. textrenderer.drawtext(e.graphics, this.text2, this.font, this.clientrectangle, this.forecolor, textformatflags.bottom | textformatflags.right); } }
add class new file, build project, , drop control onto form. make sure set both text
, text2
properties, , resize control in designer , watch happens!
Comments
Post a Comment