c# - Get width directly after Button.AutoResize is set -
i'm adding buttons dynamically form
, trying lay out side side. i'm content using latest button.right
starting point next button (with margin left between of course), buttons have adjust fit text.
so, i'm doing setting autoresize
property true , storing right
property, not work because guess resizing doesn't happen until button drawn (i think). tried invalidate()
, refresh()
, update()
, think couple more functions, , of course together, no avail, still old position , next button starts beneath one.
so the question is, after setting autoresize
true on forms
component, how force resize can grab new width
/right
without waiting window redrawn?
thanks in advance!
note: if else fails i'll rough approximation of width of buttons based on string's length, don't bother fancy solution, it's not requirement perfect
you can use control's getpreferredsize method obtain final auto-sized dimensions. font property must either explicitly set or control must parented displayed control such can inherit font use in layout. in following example, control's parent
property set inherits parent control's font
.
private random rnd = new random(1000); private void button1_click(object sender, eventargs e) { const int32 xdelta = 5; // horizontal distance between added buttons int32 y = button1.location.y + 5 + button1.height; int32 x = button1.location.x; point loc = new point(x, y); this.suspendlayout(); // form parent container of buttons (int32 = 1; <= 10; i++) { button btn = new button { parent = this, autosize = true, autosizemode = autosizemode.growandshrink }; btn.text = new string('a', rnd.next(1, 21)); btn.location = loc; size sz = btn.getpreferredsize(size.empty); // size of btn based on font , text loc.offset(sz.width + xdelta, 0); } this.resumelayout(true); }
Comments
Post a Comment