TabControl (SelectedTab) does not work, how can I fix this? - c#

I am working on a Project in the Moment and have a Problem with the selection of a Tab in TabControll. I tried to select a Tab with TabControl.SelectedTab, but then an error appears that says, that SelectedTab does not exists. Is there anything I am missing? Down below is the Code how I tried to select a Tab.
TabControl tabControl1 = new TabControl();
tabControl1.SelectedTab = tabControl1.TabPages["tb_config"];
Thx for any help :)

Ok I finally got the solution:
System.Windows.Forms.TabControl tabControl1 = new System.Windows.Forms.TabControl();
this.tabControl1.SelectedIndex = 2;
If I use this Code, it works without Problems :)

Related

C# - Add glass button on toolstrip control?

How do I add a GlassButton to a ToolStrip using C#. My code is:
ToolStrip toolStripTaskBar = new ToolStrip();
GlassButton gBtn = new GlassButton();
ToolStripButton button = (ToolStripButton)gBtn;
toolStripTaskBar.Items.Add(button);
I'm getting the following exception:
Cannot convert type 'Glass.GlassButton' to 'System.Windows.Forms.ToolStripButton'
Any suggestions how can I achieve this?
Use ToolStripControlHost instead of ToolStripButton to add your custom button to the ToolStrip:
ToolStrip toolStripTaskBar = new ToolStrip();
GlassButton gBtn = new GlassButton();
ToolStripControlHost button = new ToolStripControlHost(gBtn);
toolStripTaskBar.Items.Add(button);
You're going to have to wrap your GlassButton is a class that bases ToolStripItem. The scope of issues you're going to run into with this is unknown and too broad for this forum.
The bottom line, a GlassButton is not, and does not base, ToolStripItem and that's why you're getting the error.

WP7 - ContextMenu doesn't show on Hyperlink

I'm trying to programmatically add a ContextMenu to a Hyperlink. I've searched in the documentation and in forums, and seems that the correct solution should be this:
var link = new Hyperlink();
link.Inlines.Add(new Run() { Text = linkText });
link.FontWeight = FontWeights.Bold;
link.TargetName = linkText;
link.Click += new RoutedEventHandler(link_Click);
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += new RoutedEventHandler(CopyLink);
item.Header = "copy link";
item.Tag = linkText;
menu.Items.Add(item);
ContextMenuService.SetContextMenu(link, menu);
This code compiles and does not throw any exception. I've tested and the ContextMenu is indeed added to the Hyperlink. The problem is that it will not show anytime. I can tap & hold the link all the time of the world and the menu will not appear. Also tried to add a listener with GestureService and GestureListener, but the Hold event does not fire at all.
Anyone can help me? Thanks.
You can't do ContextMenus on Hyperlinks. You can do it on a HyperlinkButton, though. I'm not exactly sure of the reason, but it does work. Depending on what you're trying to do, HyperlinkButton may have been what you wanted anyway (Hyperlink is usually only used inside documents of text).
I just found the answer while reading the Windows Phone 7.5 Unleashed book by Daniel Vaughan. My problem was that I needed to add a hyperlink in a RichTextBox, and I can only use Inlines to show text. Hyperlink is an Inline, but does not support ContextMenu. As Tim suggested, I needed to use HyperlinkButton. The solution is that there is a class called InlineUIContainer. So, this
var inline = new InlineUIContainer { Child = SomeHyperlinkButton }
made the trick for me.

IBeam caret on contextmenu of RichTextBox

I create a contextmenu and add this to a richtextbox. Now when I Right Click on the richtextbox, the contextmenu appears, but the cursors is an IBeam caret and not the default arrow.
Does anybody know how to fix this?
var tb = new RichTextBox();
tb.Text = "test";
var items = new[] {new MenuItem("Save",SaveTextFile)};
var ctxMenu = new ContextMenu(items);
tb.ContextMenu = ctxMenu;
Thanks,
Erik
I'm dreadfully sorry, but there is no way to fix this. It has been an issue with Winforms for a long time. See here and here.
Microsoft promised to look into it, but apparently they haven't fixed it yet.

Adding controls to TabItems dynamically at runtime in C#

So, here is my issue, im working in a Wpf application, so i dont have all of the standard windows form controls.. so im attempting to use a windowsformshost, to hold a webbrowser inside of a tabitem. So here is what i have:
Tab t = new Tab();
Browser newbrowse = new Browser(t);
WindowsFormsHost host = new WindowsFormsHost();
Grid g = new Grid();
host.Child = newbrowse;
newbrowse.Dock = DockStyle.Fill;
g.Children.Add(host);
t.Header = "New Tab";
t.Content = g;
tabControl1.Items.Add(t);
now, Tab, and Browser, are just my custom implementations of the controls, and they both are tested and working. So that is not the issue. Now as far as i can see, that code should work. But I'm left staring at a blank tab page. Note that this will need to be in the codebehind and cant be included in the WPF itself.
Thanks for any input! :) Cheers
EDIT: Note that i have also tried the operation with the standard, controls.tabitem, and forms.webbrowser to the same effect
I just tried this, and apparently what breaks your plan is the line:
newbrowse.Dock = DockStyle.Fill;
Comment it out, and watch how it suddenly works!

C# dynamically add text to tabcontrol

Having a slight problem on C#, still quite new to the language but hoping you can help. I have a program which dynamically creates tab forms and then I'm trying to add controls to the tabform (text boxes and labels), but no matter what I try it just doesn't seem to want to work. Here's the code I'm currently using (just to get one textbox in each form):
int i = dogresults;
while (i > 0)
{
i--;
DataRow dogrow = ds1.Tables["confirmdogs"].Rows[i];
string dogname = dogrow.ItemArray.GetValue(3).ToString();
TabPage newpage = new TabPage(dogname);
tcNewCustomer.TabPages.Add(dogname);
TextBox tb1 = new TextBox();
tb1.Location = new Point(20, 10);
newpage.Controls.Add(tb1);
tb1.Name = "txtDogNo" + i;
}
Any help would be greatly appreciated!
EDIT: Doh! Got it!
You're not adding the new TabPage you're creating. This line:
tcNewCustomer.TabPages.Add(dogname);
should be like this:
tcNewCustomer.TabPages.Add(newpage);
(A small test app shows the tab pages being created without any textboxes with the first version, but with the second version working fine.)
That looks okay at a glance (although I haven't tried it - a short but complete demo program would help). When you say it "just doesn't seem to want to work" - what exactly is happening?
Have you tried moving the location down a bit? I know some controls are odd in terms of where their logical "top" is (i.e. it's not the first visible pixel).
What about setting the text in the textbox? Currently you're just setting the name...
Although I'd still expect you to see a border on the box + background colour assuming that differs from the tabpage background.

Categories