ArgumentException: Getting control 2's position in a group with only 2 controls when doing mouseDown Aborting

Exist unity answer cannot fix my question, following is my code:

public class TestDrawOnCond : EditorWindow
{
    [MenuItem("Test/Draw On Cond")]
    static void St()
    {
        var win = GetWindow<TestDrawOnCond>();
        win.Show();
        win.Focus();
    }

    private void OnGUI()
    {
        var ev = Event.current;
        if (ev.type == EventType.MouseDown)
        {
            GUILayout.Button("Btn");
        }
    }
}

when I click shown window, it raise err, I don’t understand, why I cannot dynamic generate button

update 2017-9-16:
my old code is not perfect, it will maybe not repeat OnGUI, in some case(for ex, when i type in textfield, and don’t move mouse, next OnGUI will not be invoked), so I should force Repaint(force invoke next OnGUI)

    [MenuItem("Test/Draw On Cond")]
    static void St()
    {
        var win = GetWindow<TestDrawOnCond>();
        win.Show();
        win.Focus();
    }

    bool rendered;

    private string text;

    private void OnGUI()
    {
        var ev = Event.current;
        if (rendered)
        {
            GUILayout.Button("btn");
        }
        if (ev.type == EventType.MouseDown)
        {
            rendered = true;
            Repaint();
        }
    }

old answer:
I guess render ui cannot be done in Event “if” statements, but I can use it in other non-event “If” statements, here is my tmp solution:

public class TestDrawOnCond : EditorWindow
{
    [MenuItem("Test/Draw On Cond")]
    static void St()
    {
        var win = GetWindow<TestDrawOnCond>();
        win.Show();
        win.Focus();
    }

    bool rendered;

    private string text;

    private void OnGUI()
    {
        var ev = Event.current;
        if (rendered)
        {
            GUILayout.Button("btn");
        }
        if (ev.type == EventType.MouseDown)
        {
            rendered = true;
        }
    }
}

I still wanna know what cause my error, if you know please tell me.

I came across this error myself – Essentially it has to do with executing code that affects the display of currently-drawing GUI elements with remnants of a previously-drawn GUI element (e.g. showing a texture or other element for a control whose internal control ID was previously used in another GUI drawing command that internally references that very same control ID (but with different data or an internal control type) and is essentially being activated between two “internally the same ID – but externally different types” of control executions, which, essentially confuses the engine.)

To put it more simply – the internal order of Event processing/execution is breaking the semi-persistent nature of the GUILayout commands by trying to reuse the same controlID (say ID 2) from another, previously-drawn, control (who also internally seems to have ID 2, but actually has different data, which confuses the compiler), but that new data exists outside of the current control ID’s drawing context (thus the error with the “Repaint” event, since drawing code and logic code execution each happen at different times.)