The MSDN documentation for the SplitContainer controls says:
With the SplitContainer control, you can create complex user interfaces;
often, a selection in one panel
determines what objects are shown in
the other panel.
So, I have a SplitContainer that is split horizontally having top and bottom panels.
What I would like to do is -- when a control within Panel1 (on top) receives focus, I need a control within Panel2 (bottom) to be focused. And then the focus must go back to the control in Panel1 again.
The problem is, the control in Panel2 returns false on CanFocus. I understand that for some kind of controls this can happen, and that every ancestor of the control must have focus for it to receive focus. I tried that too, but to no avail. The CanFocus still returns false.
Is there any alternative solution to this? Has anyone tried this. What I am trying to do is essentially the same as what Outlook does. When you select a message in your Inbox. The message body shows up in the bottom panel.
What you're describing doesn't mimic Outlook. If I select a message in the message list in Outlook, focus remains with the message list. Yes, the message body does appear below, but the focus doesn't move.
There shouldn't be anything preventing you from changing either the control or the content that is displayed in either panel. You shouldn't have to move focus to accomplish that.
Do you really need to set focus to Panel2?
I'm guessing your Panel2 contains something like a RichTextBox object, say called RTB1 for simplicity.
Say someone selects something in Panel1. For simplicity, let's say you have a ListView control called LV1.
Then, on the LV1.SelectedIndexChanged event, read that event and fill your data to RTB1.
You don't really need to give Panel2 focus, unless there is something I'm missing.
Related
I'm struggling to make a custom autocomplete style control for my application using a list view. In one form, the custom control is working as expected, however in another, when clicking on the ListView, rather than the ListViewItem being selected, the first TextBox in the form is selected instead.
Rather than clog up the question with a wall of code, here is the code for the AutoCompleteListView.
Here is an image of the form. Items are redacted due to data protection:
Also, here is a .gif showing the behaviour of the form. What I'm doing in the aformentioned gif is attempting to click on the first item within the ListView, however, as I do that, focus changes to the first text box in the form. This same behaviour occurs when clicking on any item, in any of the 3 text boxes.
I have attempted to trace the events using RuntimeFlow to watch as events fire, but it has not provided any clarity as to what is actually happening. If anyone could point me in the right direction, that'd be greatly appreciated.
My first guess is in ItemClick you are setting visibility to false, which means something else in the form needs focus, ie the textbox. So do a textbox.focus() in ItemClick to the desired textbox. And given that you will not have access to the parent form in this class, I recommend you make an OnClose event in the AutoCompleteListView and subscribe to it in the parent form, to set the focus to somewhere.
Items like below have to be accommodated on a specific area on WinForm.
"Order 1 ------------ Time Left: 00:20:00"
"Order 2 ------------ Time Left: 01:30:20"
We should be able to perform the following action on the each order:
Each item will occupy not more than one line. As the area specified
for it on the win form is limited, as more items comes in, I want to
make the area scrollable.
According to the time left the background color of the line should also change.
There is a DONE button next to it. So, if the item is selected and DONE is pressed, the item is moved off the list.
My question is what C# Form control can be used for it. I was thinking of labels, but how to make the area scrollable if there are many of them. If not labels, what else is suggested?
As suggested in comments, you can use grid, but in case it does not suits your requirements, this is like something what you can do -
Create a custom user control which will have a label control for your order detail and a Done button. Something like this (sure you will design it better!). The label will be empty initially and you will pass then from outside, using a public property for example, while creating the control.
Define an Event on this control, and raise this event when the Done button is clicked. This is required for your main form to know that when to remove this user control.
Add a FlowLayoutPanel to your main form. This will be the container for your user controls. Make sure to set the following so that the controls are created as desired
this.flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
this.flowLayoutPanel1.WrapContents = false;
this.flowLayoutPanel1.AutoScroll = true;
Now you can start adding your custom control to this FlowLayoutPanel, either by loop or the way you like. The control will added in linear way, one in each line, and you will also get scroll it it exceeds the given space.
Make sure to define event handler for the control, so that you know when to remove the control. And of course you can set other properties like back groung color etc. That's not going to be any problem.
Basically, Im making a paint application very similar to MSPaint.
The idea is that, that the the user clicks anywhere on the form and should be able to write text in a control. And then following that, that text should be displayed in g.drawstring graphic method.
I don't want to do the whole thing for you, but here is a basic outline of one way to accomplish the goals you outline. This is not necessarily the best way, but it should get you started and will introduce you to a number of WinForms concepts.
Writing the text
Create a Form and add a TextBox control to it. Make sure it is hidden by default. Override the OnMouseClick method of your Form and add code that checks if the TextBox is visible and if not, shows it and puts focus to it for the user to enter their text. If the TextBox is already visible, the code should hide it and create a new UserControl in its place that shows the text (see below for details of that UserControl).
Also add an event handler to the TextBox so that if the user hits Esc, it cancels the edit and if they hit Enter, the text is accepted and the UserControl is created.
Displaying the text
Create a UserControl and make sure that the UserPaint and Opaque styles are set in its construction (see SetStyle - you may also want to consider OptimizedDoubleBuffer and AllPaintingInWmPaint as this can reduce flickering though it does require extra paint code).
Override the OnPaint method in your UserControl and implement the code for drawing the string (remember, you'll also need a way to set the text on the control).
Conclusion
If you hook all that up, you should have something that appears to meet your requirements. For further experimentation, consider how you could remove the need for the UserControl. Good luck and have fun!
I'm building a C# WinForms program, and my textboxes do not allow the user to highlight the text consistently throughout the program.
In some places, the highlighting works normally: you type something in the box, click and drag over some text, and it highlights where you dragged.
In other places, clicking and dragging does not select the text. The only way to do it is by double clicking on the text.
I haven't changed any default properties of these textboxes or messed with any event listeners. I placed brand new textboxes in different places, and they behave differently.
I'm wondering if it has something to do with the properties of the Form the TextBox is contained in, since it seems to appear that either all textboxes in a particular form work, or none do. However, as far as I can tell the properties look to be the same across the board, and I don't ever remember changing anything.
To me it seems like it's happening randomly. I can't find any information on the topic. Does anybody have any idea what I'm talking about?
EDIT: Ok, I figured out where the problem lies, but I still don't know how to fix it.
It happens only in forms which have been added to a SplitContainer in my main window like so:
myForm.TopLevel = false;
this.splitContainer.Panel2.Controls.Add(myForm);
myForm.Show();
EDIT 2: I now know that this is the same issue encountered here: Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form . The accepted answer isn't useful to me, and the other answers seem impractical, since I'd have to add event handlers to every single textbox...
I had the same problem today. I tried changing TopLevel as others have suggested. This didn't work. Somewhere along my search I saw a suggestion to create a click event for the text box and use it to force focus on the control. This made no difference either. There were no events that should intercept and block a click event. It was just an MDI child with a few controls on it stuffed inside a panel on a split container. I couldn't highlight text in textboxes or textbox-derived controls though.
Turns out the solution was to switch the order of childform.Show() and panel.Controls.Add(childform). If you add the child form before it is shown, you apparently cause this bug.
I'm a little perplexed at what you're trying to accomplish. I'm used to using a user control if I want to embed something on a SplitPanel, and using an MDI form if I want child forms.
Do either of these approaches work for you, and if not, can you explain why not/what you are trying to accomplish?
Thanks!
James
* Edit *
You can add a panel (regular panel, not a split panel) to an MDI parent form and dock it to the left. Add whatever you currently have in the left panel of the SplitContainer to this left-docked panel, instead. Now you can instantiate forms, set them as children to the main MDI parent, and have all the window functionality you're looking for... You can maximize them, and they will fill the right-side of the MDI parent; you can pick cascade or tile from the window menu, etc.
If you want to let the user dynamically resize the left panel, drop a splitter panel into the right-hand portion of the main MDI form container; it will dock left by default, and show up to the immediate right of the panel. Now when you run, you can drag the border of the panel to resize.
Remember, an MDI form is like any other form... you can add any control you want to its surface, and .NET is pretty smart about how it incorporates the child windows.
If you're still not sure of what I'm trying to describe, I'll try to find somewhere I can drop a sample project... because everything is really done in the designer, there's not really any code I can show you. Here's the code for creating a form as an MDI child (running from within the MDI parent):
MyForm frm = new MyForm();
frm.MdiParent = this;
frm.Show();
That's all there is to it.
HTH!
James
I want to create my own custom control that is basically a TableLayoutPanel with 3 rows and 1 column. The top and bottom rows will contain labels (banners) and the middle row is where I will add other controls. The problem is that when I try to build other forms/controls from this control, the designer doesn't recognize the middle panel. How do I get it to? If I drag a textbox to the middle and set Dock=Fill, it will cover the entrie form/control. Also, is there any way to get the designer to reject dragging of controls to the top and bottom (banner) rows? I've tried the steps in the following link but haven't had any luck (http://support.microsoft.com/?scid=kb%3Ben-us%3B813450&x=21&y=15).
I figured it out. The trick was to create my own designer that inherits from ParentControlDesigner and overrides the Initialize method and calls EnableDesignMode for the inner content panel. On top of this, I needed to set the Designer attribute of my user control to this new designer. The details are shown here.
One problem, though. I can drag controls to the content panel I created and everything looks fine. But, once I recompile, the controls disappear. They are still there, I just think they're getting drawn before the banner panel. I will create a separate thread for this problem.