Windows Forms

Hi,I need to create a windows forms application having collapsible panel with some controls like label and picture box inside the panel.How to do this?Please help me by providing a sample solution or steps.Thanks in advance.

That's the wrong way of learning to develop; you start with a book or easy tutorials and work trough that - starting a project without knowing where to begin and asking for help on every step of the way will not be a "fun" way of learning, because of comments like mine

If you've been assigned this "problem" by a teacher, please introduce him/her to this thread.

Drop a Panel on your form. Notice that you can hide/show the panel using it's Visible property, or the Hide/Show methods. Also note that a Panel inherits from Control, and that it can notify you if the user enters or leaves the panel (see MSDN[^]).

Drop your other controls on the Panel, and implement the logic to show it when the mouse enters the control, hides it when it leaves the control.

Next, you'll notice that it will not unhide; that is because you cannot "enter" an invisible control. So, we'll introduce the Size property right away: if the mouse enters the control, make the Panel a 100 pixels wide, if it leaves the control, make it 25 pixels wide. First state is called "Normal", seconds state is called "Collapsed".

You can earn bonus-points by wrapping it all in a re-usable UserControl and writing an article (submit on this site of course) on the topic. Yes, we welcome articles that provide a sample solution and/or steps.

I think you got the best possible advice from Eddie Vluggen in his post above: you need to "ground" yourself in the fundamentals of what Forms, and "container controls," like the Panel Control, are:

And, the fact that true "container controls," at design-time in Visual Studio, allow you to drag-and-drop other Controls within them, at which point those dropped Controls have their Parent property set to the Container Control they were dropped into: you hide the Parent container control: everything within it is hidden.

The only thing I can add here is to suggest that, to me, a "collapsible" Panel, implies that some part of it will always be visible: the simplest way to achieve that is, as Eddie suggested, to manipulate the Size property of the Panel using the Enter and Leave events of the Panel. If you want to leave the Panel "fully open" some of the time, when the user moves the mouse outside the Panel: that requires a different solution.

There are other, more sophisticated techniques (but still rather simple to code), for when you need multiple Panels that open and collapse within an outer Panel, leaving only one open, as in the famous "Outlook Bar" interface.

edit ... Hint: use Panels set to Dock.Top within an outer Panel, and respond to Click events on the inner panels by iterating the ControlCollection of the outer Panel, and doing the right thing to hide and collapse the inner Panels.

good luck, Bill

"Everything we call real is made of things that cannot be regarded as real." Niels Bohr

Again, Eddy's advice rules. But of course you could also use a command button to control the panel size. That way it changes when you tell it to rather than based on where the mouse is.

I've only recently used the panel for the second time and on this occasion I am placing an image, label and command button on at run time rather than design time one per row, based on the number of rows on a particular database table.

Indeed, a given UI of this type (sub-panels in a Panel) is may be designed to allow any number of sub-panels to be expanded, or collapsed,or "accordion style," allow only one sub-panel to be open at-a-time: that may mean you need to deal explicitly with visibility, assuming you have the Panel's 'AutoScroll property set to 'true, and there are sub-panels in the Panel's ControlCollection which are scrolled out of view.

I assume you add these controls to your sub-panels at run-time because: either you don't know the number of rows until run-time, or, the end-user may set a parameter that affects number of rows to be rendered into sub-panels on Load, or the user "on-the-fly" changes the number of rows they want rendered into sub-panels (via selection or query or whatever).

Personally, I'm a "true believer," in this situation, in creating a UserControl that can be multiply instantiated, that contains all sub-elements: such as the ones you described: image,label, command button, and then keeping a Collection of those back on the "ranch" (some "MainForm") in a form of a Generic Dictionary, which will vary depending on the scenario, but may be like: Dictionary<subPanel, bool>, where the Boolean might indicate if its "open," or "collapsed."

... edit ...

Assuming a scrollable outer container Panel: if the user does some selection activity ... outside the container Panel ... that will then determine that sub-panel#x needs to be visible, you may have to adjust the scroll-position in your code, if the targeted sub-panel is currently scrolled out-of-view: this is a reason why I like using a generic collection here: to get away from code like

panel2.ScrollControlIntoView(panel2.Controls[0]);

... end edit ...

But, that's just another recipe from Mama's Kitchen, and I am not questioning the way you are doing things now: just extending your comment a bit.

"Each scenario may require a different UI solution that is optimal" is such a cliche, that I won't even mention it here

best, Bill

"Everything we call real is made of things that cannot be regarded as real." Niels Bohr

I am left with the idea about writing perhaps three articles, aimed at relative beginners with WinForms, showing how to use inheritance to create Forms that essentially duplicate an existing Form (where, for example, you want a set of standard identical controls with identical behaviors on every Form created).

Second, I would like to discuss the shortcomings of that approach when you, for example, wish one Form that inherits from a "master Form" to not have one of the inherited controls (which in the designer appear "locked" when: you create a new Form, add it to the Project, and then make it inherit from the "master Form.")

Third, I'd like to compare the strategy of switching between identically sized and positioned Forms to vary the user interface (which is what the OP of the QA post linked to above was after), vs. the simpler strategy of using Panels which are switched at run-time on one Form (but where said Panels may also use inheritance in the same way as described above with Forms).

My question is: given the possibility that WinForms is now no longer of that much interest to CP members compared to the latest "hot stuff:" is it really worthwhile to write this up, and include tricks like use of a static class to manage multiple Forms, or Panels, or modifying the Program.cs class to call an 'Initialize method in a static class, etc.

thanks, Bill

The glyphs you are reading now: are place-holders signifying the total absence of a signature.

Yes, it is. Not every shop uses WPF, a lot of them still build on the controls that they and their customers are familiar with. I'm still using WinForms as WPF isn't implemented in Linux. Yes, Gtk# might be an alternative, but that would introduce a learning-curve for anyone unfamiliar with Gtk#.

Hai Lee Paul Alexander,I am using your superlist control in my application. Can you please suggest me how to update,delete and add rows when it is binded through datatable.However the binding is happening by creating a custom datatable and custom datarow. No i do not know how to update, delete and add rows in the control which will reflect in the database.Thanks in Advance,Rajesh

If you have a question about something you found in one of the articles, you should ask the question in the forum at the end of that article. It's very, very unlikely that Lee Paul Alexander will happen on the question in a none related forum, whereas he gets an email if you ask on that article.

Below is the code that is actually closing the whole application when clicked on Yes.
But,I want to close the current form and open the new form when the user clicks on X with red mark i.e. close on the form.The code I wrote is :
<prelang="vb">Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox(&quot;Do you want to close?&quot;, MsgBoxStyle.Question + MsgBoxStyle.YesNo, &quot;confirm&quot;)
If response = MsgBoxResult.Yes Then
Me.Dispose()
homepage.Show()
Else
If response = MsgBoxResult.No Then
e.Cancel = True
End If
End If</pre>
end sub

I have made a procedure named abc in database which is returning only one row from login table..So i am just checking that on click of that button my procedure should run but while debugging when i come to cmd.ExecuteQuery() it throws following error..

ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.77-community-nt]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'abc()' at line 1.

If all the PC's are the same type, why not use Norton (Symantec) Ghost?

All you need to do is install software on one PC, then create a Ghost Image of that computer. Then use that image to re-image all the other computers in the class room.

It doesn't matter what the students do to their PC whilst in a class session, once it is finished run the multicast to re-image all the PC's and they are back to where you began before your students started "playing around" with the individual configurations.

Where I teach:1. Rarely happens although viruses are rampant.2. Installing IDE's and other tools are part of the learning3. Students are expected to backup to USB. They do have a docs folder on the network but this causes issues with most development IDE's

Having said that all the standard tools are installed by Admin as per department requirements. All machines are re-imaged every semester.

I developed a windows form application using c#.and i created a .exe(setup) file.it is running in my system.i want to install on another system which does not contain sql server.i want to install sql server along with the .exe file.how to include sql server during setup creation.how to add my data base to the set up...please help.......

3) You're using VB6. There is absolutely no valid reason at all to start new development work in VB6. It's dead and has been for years. VB.NET Express Edition is free and easily obtainable from here[^].

I would suggest using any type of loop (Do While, For Next etc), what ever you like to get the data from the DataGridView column.

Just get the number of rows you wish to copy then loop through the rows, either storing the data returned into the ListBox or populating an Array, which can then be used to fill the ListBox. Note in the example below data is returned from the first column.

Dim pInt As IntegerDim pIntItems As IntegerDim pStrValue as String (or what ever the value type of the column is)pIntItems = DataGridView.RowCount For pInt = 0 To pIntItems - 1 pStrValue = DataGridView.Rows(pInt).Cells(0).Value "Now either store this value to an array or make a function call to start populating the ListBox with the value." Next

Hello, 1.Get all the data from gridview into some (public static)variables.2.In the form where u want those values just write the form name of gridview values followed by the value property of list box.

I have 2 Buttons in my form. Both have background image and Flat style is "Flat".

1) On clicking TAB key, when the focus is on button, I see solid color rectangle inner, instead of solid border I want to have dotted border in the button.

2) If the button is on focus & the application loses focus (someother app is active), the button forms a outer border of white color. I want it to remain as it is i.e. dotted line inner rectangle.

3) When the button is clicked, some activity is going on. While this time the button text colro changes to grey. I don't want it to change the text color. I believe this is windows std property, but how do I not change the text color when the button is selected .

I can't find any properties to set these points. What should I do to achieve the goal. Any help is highly appreciated.

1. This is Windows drawing the focus cue on the button. You cannot change this.

2. Again, Windows. Can't change this.

3. The text turns grey only because your code has to change it. This isn't Windows doing this or a property on the control. When you click on Windows Forms Button, no colors of or inside the button change at all, unless your code is specifically written to do this.

[EDIT] 3. I haven't tested this, but this could be because of the Windows Desktop Theme your using.