First-principles calculations of surfaces or two-dimensional materials with a finite surface charge invariably include an implicit or explicit compensating countercharge. We show that an ideal constant-charge counterelectrode in the vacuum region can be introduced by means of a simple correction to the electrostatic potential in close analogy to the well-known dipole correction for charge-neutral asymmetric slabs. Our generalized dipole correction accounts simultaneously for the sheet-charge electrode and the huge voltage built up between the system of interest and the counterelectrode. We demonstrate its usefulness for two prototypical cases, namely, field evaporation in the presence of huge electric fields (20 V/nm) and the modeling of charged defects at an insulator surface. We also introduce algorithmic improvements to charge initialization and preconditioning in the density functional theory algorithm that proved crucial for ensuring rapid convergence in slab systems with high electric fields.