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?
 
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.