c# - PrintPreviewDialog: What is the name of the toolstrip to get it through reflection

StackOverflow https://stackoverflow.com/questions/13157263

  •  23-07-2021
  •  | 
  •  

Pergunta

I would like to know what is the correct name of the printpreviewdialog's toolstrip to get it through reflection. I want to know the correct name to use from windows xp, vista and seven. I do no know if its name is Windows version dependant. Currently I am doing (I am referring to it as toolStrip1):

    Type type = typeof(this);
    FieldInfo toolStripBar = type.GetField("toolStrip1", BindingFlags.Instance | BindingFlags.NonPublic);
    FieldInfo printToolStripButton = type.GetField("printToolStripButton", BindingFlags.Instance | BindingFlags.NonPublic);
    ToolStrip toolStrip1 = (ToolStrip)toolStripBar.GetValue(this);
    ToolStripButton printButton = (ToolStripButton)printToolStripButton.GetValue(this);
    Bitmap bitmap = new Bitmap(MyBmpImage);
    printButton.Image = bitmap;

Is toolStrip1 name working always indenpendently of the Windows version?

Foi útil?

Solução

You are poking at private variables of the class. That voids the warranty, Microsoft can decide to change the class and that will break your code.

But yes, it is called "toolStrip1" and it has had this name since .NET version 2.0, possibly before that. It is quite unlikely they'll ever change this name, Winforms is in maintenance mode and only critical security problems and operating system compatibility problems get fixed.

If this at all makes you uncomfortable then don't hesitate to implement your own preview dialog. There is very little to it, the heavy lifting is done by the PrintPreviewControl class. You can take a peek at the Reference Source for hints.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top