I have a TabControl where I want to keep the tabs to a fixed size and I want icons in the tabs. I have set TabControl.SizeMode = Fixed and TabControl.ItemSize = 100, 18. I have also set TabControl.ImageList and am assigning images to the tabs via TabPage.ImageKey.
Here is what it looks like if I comment-out assigning the ImageKey:
And here is what it looks like if I am assigning the ImageKey:
Is there some sort of "alignment" for the icons? I want them to be on the far left in the blank space, but instead they are starting where the text starts. Any suggestions?
(BTW - if I set TabControl.SizeMode = Normal, I get the tab content the way I want it, but the tabs aren't a fixed size):
I can verify the issue that you are seeing with TabControl.SizeMode = Fixed (on Windows 10). I initially seen it in the designer when configuring a TabPage with an icon. However the irritating thing is that the issue corrected itself if the designer is closed and reopened. This suggests a window style setting of some sort and there are some Tab Control Styles set in the CreateParams Property based on the SizeMode Property. However, I found no solution in attempting to apply the TCS_FORCEICONLEFT style. If the ImageIndex property is set prior to the control being shown, then the alignment is as desired. So I figured that there must be something being configured on handle creation.
If you call the form's RecreateHandle method after setting the TabPage.ImageIndex property, the form redraws and all looks good. However this cause the form to blink. Calling the Control.RecreateHandle method on the TabControl also works. This is a protected method and would necessitate using a derived TabControl to expose the method or you could use Reflection to invoke the method.
public class MyTC : TabControl
{
public void FixIcon()
{
RecreateHandle();
}
}
I have a Created a logwindow in my project in C#
This log window is nothing but a richtexbox. I am appending the lines in RichTextbox whenever a method is called.
What i want is : It should autoscroll down whenever a new line is Appended in the rich text box.
Can anybody tell me how to do it.
thanks
Basically you just have to set the "cursor" to the end of the box by setting the SelectionStart and afterwards tell the control to scroll to the caret (= the selection):
rtfBox.SelectionStart = rtfBox.Text.Length;
rtfBox.ScrollToCaret();
I am using a Windows.Forms.WebBrowser control as a text editor. To alter the font size of some selected Text, I display a modal window, where the user can make chices concerning the font size and after closing that window, the previously selected text is decorated with the changes. Unfortunately as soon as the modal window opens, the selection in the main windows isn´t visible anymore and I can´t find a way to save and restore it. I can determine the selected Range using
IHTMLDocument2 htmlDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLSelectionObject currentSelection = htmlDocument.selection;
but since htmlDocument.selectionis readonly, I am not able to set it after the modal closes. All I Can do is call Select() on the main window, but then the caret jumps to the end of the text and nothing is selected.
Any ideas how to solve this?
(I know I could use a ComboBox to alter Font-size, but I need the custom window...for reasons.)
You can use bookmarks. Save selection as bookmark:
var bookmark = document.selection.createRange().getBookmark();
Restore:
var range = document.selection.createRange();
range.moveToBookmark(bookmark);
range.select();
I am trying to create a popup but when it is open it is still possible to use the tab key to switch the focus to an element in the background (e.g. to a button and use space to press is). The only way I found until now is to check on every lostFocus event (which also fires for every element contained in the Border element) and check if the focus is now in a element inside the Border. If not I manually set the focus.
Is there a nicer way to keep the focus within the Border (or a Grid,...)
I'm working on a Windows 8 App.
Do you mean that using a Modal Dialog with Form.ShowDialog(Owner) still allows you to focus the parent components with Tab?
Can you give a sample of your code call?
Form2 form = new Form2(); //Make an instantiation of your Form
form.ShowDialog(); //ShowDialog()!!! NOT form.Show()!!! Or anything else :/
A few ideas:
Set Enabled to False on the background visual tree, though that might change the way things look if you still want to show them partly
Set IsHitTestVisible to False to disable pointer input
Use RenderTargetBitmap.Render() if targeting Windows 8.1 to render the content of the background to an image and simply replace all that visual tree with an image of it
I have a one question on my university's test about C#. Could label get a focus? As I can see on MSDN site, all Controls can get a focus, but some of them aren't selectable. So it's seems to me that the right answer is "Label could get a focus, but couldn't be selected". Also Label has a Focus() method. Please, help me understand. Thanx.
Yes there is a Focus() method on Label and yes it is absolutely right it works; but behave differently. let me try to explain
A Label can be associated with some one input control, for instance a label for a user name text field, so there is concept of Associated Control with the label. AssociatedControlID on msdn
So you can associate an input control with a label and whenever label is selected the control passed to the associated input control.
Example here click on Email or Password labels in login box and see what happened, similarly if you call focus method on the label the focus will passed to the associated control.
From the documentation:
A control can be selected and receive
input focus if all the following are
true: the Selectable value of
ControlStyles is set to true, it is
contained in another control, and all
its parent controls are both visible
and enabled.
Since a Label control is not selectable, it cannot receive input focus, even if it inherits a Focus() method from Control. Therefore, the answer is no.
It's easy to findo out if a control's ca get focus. Just read the
.CanFocus
property which is inherited from the Control class.
The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable. (see MSDN documentation)
Panel
GroupBox
PictureBox
ProgressBar
Splitter
Label
LinkLabel (when there is no link present in the control)
Also:
The Focus method returns true if the
control successfully received input
focus. The control can have the input
focus while not displaying any visual
cues of having the focus. This
behavior is primarily observed by the
nonselectable controls listed below,
or any controls derived from them.
A control can be selected and receive
input focus if all the following are
true: the Selectable value of
ControlStyles is set to true, it is
contained in another control, and all
its parent controls are both visible
and enabled.
If you need a Label-like control that you can focus, just use a TextBox and make it readonly. Set a few other properties (styles, not selectable etc.) and you're done.
You will see that there is a read only property called CanFocus on a label, if you have a look at this property while debugging you will see it is false.
Every control that inherits from Control has the focus method, but that does not mean that it can be focused.
Label does gets the focus but it escalates it to the input field specified in its "for" attribute. Like:
<label for="firstname">First Name</label><input type="text" name="firstname" />
In this scenario if you click on the label it will throw the focus to the input field "firstname" associated with it.
This is a year old, however I had a similar issue as the Op. In my case it was a user control that had a single label (docked at fill) on it (it has other functions behind the scenes - it is a calendar control and pops up a date picker - not the standard one - in either a panel (popunder) or a form (popup)).
The issue there was that UserControls are really intended as containers and resist focus (pushing it off to child controls) - as the label is the only child control, it stops the UserControl getting focus. Using readonly TextBox is a poor substitute as it lacks vertical alignment and must be multiline to size the height.
The reason I add the following as an answer here is because it IS possible (sorry guys who said here it is not) and I found this post and many like it that were little help when I looked. Anyway, the way to do it is to override the Label and set the SetStyle - also the OnPaint to draw a focus rectangle (I manually drew mine as DrawFocusRectangle didn't seem to do anything) - so as below:
internal class SelectableLabel: Label
{
public SelectableLabel():base()
{
SetStyle(ControlStyles.Selectable, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//ControlPaint.DrawFocusRectangle(e.Graphics, ClientRectangle);
if (!Focused) return;
if (BorderStyle == BorderStyle.Fixed3D)
{
e.Graphics.DrawLines(Pens.CadetBlue, new[] { new Point(1, Height - 1), new Point(1, 1), new Point(Width - 1, 1) });
e.Graphics.DrawLines(Pens.Aquamarine, new[] { new Point(2, Height - 1), new Point(Width - 1, Height - 1), new Point(Width - 1, 2) });
}
else
{
e.Graphics.DrawRectangle(Pens.Aquamarine, 0, 0, Width - 1 , Height - 1 );
}
}
}
I am not concerning myself on whether it is accademically (purist view) right to do so, but that there are valid reasosn to allow an output control (like label) to sometimes gain focus.