Add and bind a new tabpage within c# application - c#

I'd like to add , by code, a new tabpage to my tabcontrol :
XtraTabPage resultat = new XtraTabPage();
resultat.Text = "Résultat";
resultat.Name = "tpResultat";
resultat.Size = new System.Drawing.Size(736, 624);
resultat.DataBindings.Add(new Binding("PageVisible", DataContextForm2, "ResultVisibility", true, DataSourceUpdateMode.OnPropertyChanged));
XtraTabControl1.SuspendLayout();
XtraTabControl1.TabPages.Add(resultat);
XtraTabControl1.ResumeLayout();
The ResultVisibility is False as default,
when I run the application the resultat tabpage is visible!! when i select it , it disappears .
And when I return to main user control and I try to change the visibility of resultat the binding works, but if I did not select the resultat tabpage at the begining , the tabpage will disappear and the binding were lost .
So, I need to know:
What is the reason of this problem?
What is the best way to resolve it?

I think to solve your problem you have to work with property "PageVisible"
var firstTabPage = new XtraTabPage();
firstTabPage.Text = "first";
var secondTabPage = new XtraTabPage();
secondTabPage.Text = "second";
xtraTabControl1.TabPages.Add(firstTabPage);
xtraTabControl1.TabPages.Add(secondTabPage);
xtraTabControl1.TabPages[0].PageVisible = false;

Related

TableLayoutPanel: How can i navigate with tab key through dynamically added controls?

I have a form which contains a dynamically added TableLayoutPanel, which contains some dynamically added Labels, TextBox, CheckBox. I am obtaining exactly the visualization I would like to have, but I am struggling to get the "tab key" to work for moving from one control to the other.
I have tried to add a:
control.TabIndex = tabIndex++;
control.TabStop = true;
But this doesn't seem to have any impact...
This is the (tested) stub code:
class MyForm : Form
{
public MyForm()
{
InitializeComponent();
string[] titles = {"first","second"};
var myLayout = new TableLayoutPanel();
myLayout.AutoSize = true;
int myTabIndex = 1; //Not really necessary
int rowNumber = 0;
foreach (var title in titles)
{
var label = new Label();
label.Text = title;
myLayout.Controls.Add(label, 0, rowNumber);
var control = new TextBox();
control.TabIndex = myTabIndex++; //Not really necessary
myLayout.Controls.Add(control, 1, rowNumber);
rowNumber++;
}
this.Controls.Add(myLayout);
}
}
This is the window I get, and I am not able to navigate from first to second field using the tab key.
Update:
Applying Visual Studio 2013 Update 5 did not help.
Something must have been corrupted in my project. The best I could do is to move on with a new clean Windows Form project, and everything now is working.

WPF dynamic binding of a control property to another control property

I have a question - I was coding happily a quick little feature to an app, which was a simple comparison output window.
Basically, user clicks a button and I generate a window with a datagrid of two columns of data.
It's all great and a five minutes code living inside one method with no unnecessary reference to anything else. The only problem I encountered was when I wanted to add a TopMost checkbox to this window.
How do I bind the IsChecked property of the box to the TopMost property of the window?
var checkbox = new CheckBox();
checkbox.Name = "cb";
checkbox.Content = "Top most";
var grid = new DataGrid();
grid.ItemsSource = wcList;
grid.Margin = new Thickness(5);
var panel = new StackPanel();
panel.Children.Add(checkbox);
panel.Children.Add(grid);
var win = new Window();
//Binding b = new Binding("cb");
//b.Mode = BindingMode.TwoWay;
//b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
//win.SetBinding(Window.TopmostProperty, b);
win.Title = "WordCount comparison";
win.Content = panel;
win.SizeToContent = SizeToContent.WidthAndHeight;
win.Icon = this.Icon;
win.Show();
This was supposed to be a 5-minutes one-off method, which is why I don't want to go as far as adding any xaml or properties into the code.
Cheers
Bartek
The other way around as you tried (in the commented code):
checkbox.SetBinding(CheckBox.IsCheckedProperty, new Binding("Topmost") { Source = win });
just after you instantiated your win variable.

How to Find toolstripButton inside toolstrip in a UserControl

I used this code below to Enable/Disable Button Control inside my usercontol on my Form which works perfect.
var btnAdd = this.userControlCommonTask1.Controls.Find("btnAdd", true);
btnAdd[0].Enabled = true;
But when i use toostrip(toolstrip1) with buttons (btnAdd,btnEdit,btndelete etch..) and use my code above
I got:
Index was outside the bounds of the array.
I tried this one but it only works in toolstrip.
var btnAdd = this.userControlCommonTask1.Controls.Find("toolstrip1", true);
btnAdd[0].Enabled = true;
Thanks in Regards
I already Solve my Problem:
var toolstrip1 = this.userControlCommonTask1.Controls.Find("toolstrip1", true);
var toolstrip1Items = toolstrip1[0] as ToolStrip; <-- set to toolstrip control
var btnRead = toolstrip1Items.Items.Find("btnRead", true); <--get BtnRead on toolstrip Item.Find
btnRead[0].Enabled = false; <--disable/Enable btn
This can be a reference for other Developers.
Cheers!
Try a 0 instead of 1 your array is zero based
Toolstrip is another usercontrol. Try make a reference to it and then find it's child controls
ie. ctlTooolStrip.Controls.Find("BtnAdd",true);
Also try toolStrip.Items

How to Dynamically Create Tabs

This is in C#
I Need to basically make TabPages from a textbox.Text so for example:
textBox1.Text = "test";
TabPage textBox1.Text = new TabPage();
That is what i want to do.. i know that won't work directly, but that should give you the idea of how i want to create the tabPages.. then i want to be able to call them later on too so for example:
String browser = "browser 1";
(textBox1.Text as TabPage).Controls.Add(WebBrowser browser)
I need all the names to be dynamic because what this will be is a program that can run tests for customer accounts There would be a TabControl which has the "Account Number as the tabPage control name and then inside each of those tabPages would be another TabControl with a set up tabs with each invidivual test in it's own tab. So Tabs within Tabs basically.
Make it look similar to this:
var page = new TabPage(textBox1.Text);
var browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
page.Controls.Add(browser);
tabControl1.TabPages.Add(page);
browser.Navigate("http://stackoverflow.com");
page.Select();
Actually one other thing i want to know, How can i call on this Tab # another time outside of this function?
This is basically what i turned out to look like.
String browserName = "Test Check";
var tabPageName = new TabPage(textBox1.Text);
var tabPageBrowser = new TabPage(browserName);
var tabPageTabControl = new TabControl();
var browser = new WebBrowser();
tabPageName.Controls.Add(tabPageTabControl);
tabPageTabControl.TabPages.Add(tabPageBrowser);
tabPageBrowser.Controls.Add(browser);
mainTabControl.TabPages.Add(tabPageName);
mainTabControl.SelectedTab = tabPageName;

Adding and removing dynamic controls Windows Forms using C#

I have three Tabs in my Windows Forms form. Depending on the selected RadioButton in the TabPages[0], I added few dynamic controls on the relevant TabPage. On the Button_Click event the controls are added, but the problem is I'm not able to remove the dynamically added controls from the other (irrelevant) TabPage.
Here's my code:
Label label235 = new Label();
TextBox tbMax = new TextBox();
label235.Name = "label235";
tbMax.Name = "txtBoxNoiseMax";
label235.Text = "Noise";
tbMax.ReadOnly = true;
label235.ForeColor = System.Drawing.Color.Blue;
tbMax.BackColor = System.Drawing.Color.White;
label235.Size = new Size(74, 13);
tbMax.Size = new Size(85, 20);
if (radioButton1.Checked)
{
label235.Location = new Point(8, 476);
tbMax.Location = new Point(138, 473);
tabControl.TabPages[1].Controls.Add(label235);
tabControl.TabPages[1].Controls.Add(tbMax);
tabControl.TabPages[2].Controls.RemoveByKey("label235");
tabControl.TabPages[2].Controls.RemoveByKey("tbMax");
}
else
{
label235.Location = new Point(8, 538);
tbMax.Location = new Point(138, 535);
tabControl.TabPages[1].Controls.RemoveByKey("label235");
tabControl.TabPages[1].Controls.RemoveByKey("tbMax");
tabControl.TabPages[2].Controls.Add(label235);
tabControl.TabPages[2].Controls.Add(tbMax);
}
Where am I making that mistake?
First of all, tbMax's name is not "tbMax", but "txtBoxNoiseMax". So for one, it won't be able to find the TextBox on RemoveByKey.
You're making new controls each time.
As lc already mentioned:
You named your TextBox variable tbMax, but you gave it the name txtBoxNoiseMax. If you take a look into the description of RemoveByKey, you'll see it works on the Name property. So you should change
tbMax.Name = "txtBoxNoiseMax";
into
tbMax.Name = "tbMax";

Categories

Resources