Modify editor GUI content using mouse click

I’m trying to make a custom editor window that has a window area and an inspector-like area to the right, as follows:


It behaves as expected, but I have an issue: I need to know which window is current selected, so I’m holding the current window ID in a property, as follows:

void windowFunc(int id){
	if ((Event.current.button == 0) && (Event.current.type == EventType.MouseDown)) {
		selectedWindow = id;
	// More window stuff...

and, on OnGUI function, I reset the selected window so whenever I click outside an window but inside window area, i de-select any selected window:

void OnGUI () {
	// Some GUI stuff...
	if ((Event.current.type == EventType.MouseDown) && (Event.current.button == 0) && bounds.Contains(Event.current.mousePosition)) {
		// reset selected window
		selectedWindow = -1;
	// More GUI stuff, and a catch later...

So far, so good. The problem lies when I need to modify the side view to show/hide content based on selected window:

void OnGUI() {
	// The same OnGUI as before...
	if (selectedWindow != -1){
		// draw some controls over here...
	// if statement is false, don't draw extra items

This code works flawless if the amount of controls is higher than before (in the image example, if a window has more outputs than the window selected before). Otherwise, the error pops up on console:

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

I know why the error occurs (I shouldn’t change window content between a layout and a repaint event), but how can I work-around this issue? Does removing “Layout” functions should get rid of these errors (yes, I’m using Layout functions to draw the controls)?

Just in case anyone need to know, here’s what I did to handle this situation:
On windowFunc, I added a call: Event.current.Use(); . After that, no more console errors, and I’m happy :smiley: