C# How to Resize Controls on a winform?

Hi
I have a couple of newbie questions
On form load the listbox Anchored on the top left is populated how do I prevent the first row being selected until the user clicks a movie title

I would like the controls on my form to resize in proportion when the form is resized
The picture shows what happens when the parent form is resized all the controls stay put
ResizeForm.JPG
LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
Do you use Windows Forms?
What control do you use in left side of you form? If this control is "ListBox",  default selected item must be null (or selected index = -1)
May be you set selected item programmatically?
For certain you can set selected item  to null by this way (code):
	private void Form1_Load(object sender, EventArgs e)
		{
			listBox1.SelectedIndex = -1;
		}

Open in new window

The second part of your question. You should use property "Anchor" of your TabControl. The Anchor property is intended for fixing of edges of your component to edges of a parent member, at the same time changing the parent'a size, distance between the specified edges remains a constant (constants).

For example, in your disgner you can input this code:
			this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));

Open in new window


or you can set this property in Form Disegner.
Also you can read good article of Anchor and Dock:
Anchor and Dock
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Hi
Yes  I'm using a listbox on a win forms

setting the selected index to null as suggested didn't work

        private void Form1_Load(object sender, EventArgs e)
        {
            TitlesListBox.SelectedIndex = -1;
            string sql = "select id,title from movie order by title";
            GetFilms(sql);  // this loads the films
        }
        private DataTable GetFilms(string sql)
        {
            DataTable dtMovies = new DataTable();
            using (SQLiteConnection con = new SQLiteConnection(connString))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
                {
                    SQLiteDataReader reader = null;
                    try
                    {


                        con.Open();
                        Debug.WriteLine("Connection is " + con.State.ToString());
                        SQLiteDataAdapter dap = new SQLiteDataAdapter(cmd);
                        DataTable tblTitles = new DataTable();
                        dap.Fill(tblTitles);
                        TitlesListBox.DisplayMember = "title";
                        TitlesListBox.ValueMember = "id";
                        TitlesListBox.DataSource = tblTitles;
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.ToString());
                    }
                    finally
                    {
                        if (reader != null) reader.Close();
                        con.Close();
                    }
                }
            }



            return dtMovies;
        }

Open in new window


Resize The Form

I looked at your link this was very helpful however it seems to get a control to resize Dock is set to fill but this then fills the control to the parent container if that is the form the layout is askew

I'm not sure if this is best practice but I used a TableLayoutPanel to make 3 columns ResizeForm3Cols.JPGI then found out each panel can only have 1 control  so I ended up nesting  TableLayoutPanel2 inside column 1 of TableLayoutPanel1

Now everything resizes as expected but TitlesListBox.SelectedIndex jumps back to the first row
Any ideas why?
0
MishaProgrammerCommented:
It is very interesting! Try to populate ListBox at the first, and then set selected index = -1.

For example,

        private void Form1_Load(object sender, EventArgs e)
        {            
            string sql = "select id,title from movie order by title";
            GetFilms(sql);  // this loads the films
            TitlesListBox.SelectedIndex = -1;
        }

Open in new window

0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

trevor1940Author Commented:
Hi
Your suggestion produced a error after GetFilms

I found this how-to-prevent-listbox-selectedindexchanged-event

Setting private bool _populating = false; worked the first item is still highlighted but not selected

However why dose Resizing the form trigger form load?

        private void Form1_Load(object sender, EventArgs e)
        {            
            string sql = "select id,title from movie order by title";
// breakpoint on next line is activated when the form is resized
            GetFilms(sql);  // this loads the films
// errors here in your example            TitlesListBox.SelectedIndex = -1;
        }

Open in new window

0
MishaProgrammerCommented:
May be I not correct understand you, but if you launch your application, at the firtst time your application appear in task bar, then appear Point on yout screen, then your form resize to size, which you selected, and then appear event Form load, where you populate your LisBox...
0
trevor1940Author Commented:
Apologies I thought I had sent a reply

Your last comment is correct

Is this good practice to trigger  populating a LisBox on form load?

If not how should I?

Later on I'm intending to add a function to filter by actor I can not see if I resize the form this will overwrite any filltering
0
MishaProgrammerCommented:
Yes, usually people populate ListBox, ConboBox and other in FormLoad event, if use Window Forms.  It is  a good practice.
Your future filter will not worsen this situation. Filter not should make some effect to resize form.
0
trevor1940Author Commented:
Thank you for your help
0
MishaProgrammerCommented:
No problem! Glad to help you! =)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.