Don't we all wish there was a magical pill to prevent D, after you ate whatever you wanted to.
Imodium is the obvious answer. You can take up to 12 tablets a day.
However, I am in the camp that eats foods that agree with me and do not produce D. I've just battled these IBS like symptoms after a colon resection, cancer, etc. for 9 loooooong years. If a candy bar gives me D, I just say it's not worth it.
I follow a low residue diet, eat 6 or so very small meals a day, take Colace (50 mg) each evening, keep my stress level as low as possible and sometimes fast before social events. All of this combined seems to work best for me.
But we're all different. Some experience D, while others experience C. Some find that a fiber rich diet is very helpful, others find that all that fiber is hard on the gut (the camp I fall into).
What I would suggest is keeping a food journal. Record everything you eat each day, if you take meds or not, etc. After a while, it's helpful to look back at what you were eating on good days without D. You might want to stick to those foods.