What's new
  • Visit Rebornbuddy
  • Visit Resources
  • Visit API Documentation
  • Visit Downloads
  • Visit Portal
  • Visit Panda Profiles
  • Visit LLamamMagic

TextBox Autocompletion in Windows Forms

tictoc

Member
Joined
Sep 21, 2012
Messages
380
Reaction score
5
Hi,

for me the AutoCompletion for Windows Forms Textboxes does not work. I do get runtime exceptions.

This seems to be related to a requirement of the STA threading model. How can i get my Windows Form to use the STAThread attribute?

Anybody already tried/solved this?

Greetings,
tictoc
 
Before you start your form from your plugins main cs file, use this

Code:
formThread.ApartmentState = ApartmentState.STA;

e.g.

Code:
Thread formThread = new Thread(StartFormWithSTA); 

formThread.ApartmentState = ApartmentState.STA; // Do this before starting
formThread.Start(); 

private void StartFormWithSTA() { 
    Application.Run(yourform); 
}
 
Hint:
to save your form settings and intercept application events use UIContex:
UIContext
Code:
internal class UIContext:ApplicationContext
    {
        private  Host host;
        
        internal MainFrm mainFrm;

        public UIContext(Host _host)
        {
            Application.ApplicationExit += Application_ApplicationExit;
            Application.ThreadExit += Application_ThreadExit;
            Application.EnableVisualStyles();

            host = _host;

            mainFrm = new MainFrm();
            mainFrm.StartPosition=FormStartPosition.Manual;
            mainFrm.Location = new System.Drawing.Point(host.settings.MainFormX, host.settings.MainFormY);
            mainFrm.SetHost(host);
            mainFrm.Closed += new EventHandler(OnFormClosed);
            mainFrm.TopMost = host.settings.MainFromTopMost;
            mainFrm.Show();
        }

        void Application_ThreadExit(object sender, EventArgs e)
        {
            if (mainFrm != null)
            {
                mainFrm.Close();
                if (mainFrm.WindowState == FormWindowState.Normal)
                {
                    host.settings.MainFormX = mainFrm.Location.X;
                    host.settings.MainFormY = mainFrm.Location.Y;
                }
                host.settings.SaveSettings(host.settingsFileName);
            }
        }
        private void Application_ApplicationExit(object sender, EventArgs e)
        {
            if (mainFrm != null)
            {
                mainFrm.Close();
                if (mainFrm.WindowState == FormWindowState.Normal)
                {
                    host.settings.MainFormX = mainFrm.Location.X;
                    host.settings.MainFormY = mainFrm.Location.Y;
                }
                host.settings.SaveSettings(host.settingsFileName);
            }
        }
        private void OnFormClosed(object sender, EventArgs e)
        {
            if (mainFrm.WindowState == FormWindowState.Normal)
            {
                host.settings.MainFormX = mainFrm.Location.X;
                host.settings.MainFormY = mainFrm.Location.Y;
            }
            host.settings.SaveSettings(host.settingsFileName);

            ExitThread();
            host.PluginStop();
        }
    }
PluginRun,PluginStop
Code:
public void PluginRun()
        {
            needToStop = false;

            mainThread = new Thread(Run);
            mainThread.IsBackground = true;
            mainThread.SetApartmentState(ApartmentState.STA);
            mainThread.Start();

            try
            {
                while (!needToStop)
                    Thread.Sleep(10);
            }
            catch(ThreadAbortException)
            {
               //skip
            }
            finally
            {
                uiContext.ExitThread();
                mainThread.Join(); 
            }
        }
        public void PluginStop()
        {
            needToStop = true;
        }
Run
Code:
private void Run()
        {
            try
            {
                ClearLogs();
                Log(String.Format("Running the \"{0}\" plugin version {1}", PluginName, GetPluginVersion()));

                //Settings load
                InitSettings();

                //UI init
                uiContext = new UIContext(this);

                //Logic init
               
                Application.Run(uiContext);

                pluginControlModule.Stop();

                PluginStop();

                Log("Stop plugin!");

            }
            catch(Exception error)
            {
                Log(error.Message + Environment.NewLine + error.StackTrace, System.Drawing.Color.Red);
            }
        }
Host - Core child.
 
Back
Top