I am using VS2010 (C#) and I want to create a button that creates a new tab. I already have a tabControl container created. How would I do this?
Moved here from Brandon's solution in the question:
string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
Related
In winforms I am creating a browser based off of Gecko and I need the geckoBrowser1, urlBar1 (not yet added) and back/forwardArrow1 (not yet added) to be added to a new tabPage upon it's creation. My issue is copying these particular assets
I've already tried looking things up on google but they all talk about how to add a new tabpage from scratch.
private void button1_Click(object sender, EventArgs e)
{
//newTab is the "New Tab" button
GeckoWebBrowser geckoWebBrowser1 = new GeckoWebBrowser();
string title = "tabPage" + (tabControl1.TabCount + 1).ToString();
TabPage tabPage = new TabPage(title);
tabControl1.TabPages.Add(tabPage);
//I want to add the geckoWebBrowser1 into a new tab here
if (newTab.Location.X < Form1.ActiveForm.Width - 50)
{
newTab.Location = new Point(60 * tabControl1.TabCount - 2, 0);
}
else
{
newTab.Location = new Point(newTab.Location.X, newTab.Location.Y);
}
}
I expect the new tab I create to then take the Gecko browser and duplicate it into the new tab.
Something like this:
tabPage.Controls.Add(geckoWebBrowser1);
Here is my code for creating the textbox and label from code
int i = 0;
Label lbl = new Label();
lbl.Text = "Label - " + i.ToString();
lbl.ID = "Label - " + i.ToString();
TextBox txt = new TextBox();
txt.Text = "txt - " + i.ToString();
data.Controls.Add(lbl);
data.Controls.Add(txt);
Is it possible I add a new line after the data.Controls.Add(txt);?
I tried to use Environment.NewLine but its not working.
anyone have any idea how to do it?
data.Controls.Add(new LiteralControl("<br/>"));
Add this for new line
You should use CSS if it's purely for presentation.
Add .CssClass = "TextboxSpace" to the textbox
Add a css file to the project (or add a new style to an existing one) something like
.TextboxSpace {
margin-bottom: 20px;
}
Add a Literal Control in between your two controls for line space.
Programatically you can do it.
Set Literal Control's Mode property to PassThrough
For Example :
Literal myLiteral = new Literal();
myLiteral.Id="lit_newline"
myLiteral.Mode = "PassThrough";
data.Controls.Add(myLiteral);
Then Add this control in between your textbox and label control
Also you can do like this
data.Controls.Add(new LiteralControl("<br />"));
And If you want, you can apply css to your textbox without adding the literal control which will increase the height - as answered by user : bgs264
Are you saying the space between the two controls.. If this is the case, you can set the location for the next control such that there is a empty space after that text.
I'm trying to get the contents of dynamically created TextBlock and dynamically created RichTextboxes (they sit side by side) into the clipboard in c# + wpf, however, I'm not able to do so. I've search all over google to no avail, the latest code I came up with is
StringBuilder clipboard = new StringBuilder();
String rtb = scrlPanel.Children.OfType<RichTextBox>().ToString();
//List<RichTextBox> rtb = scrlPanel.Children.OfType<RichTextBox>().;
foreach(TextBlock txtb in scrlPanel.Children.OfType<TextBlock>())
{
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtb.ToString() + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
but it doesn't work, the codes copies the TextBlocks just fine but the RichTextBoxes content display" "System.Linq.Enumerable+d__aa`1[System.Windows.Controls.RichTextBox]"
Any help or pointers is greatly appreciated.
Thanks,
You are copying the from the ToString() - method, which by default shows the typename.
You should do it like this:
StringBuilder clipboard = new StringBuilder();
List<RichTextBox> rtbs = scrlPanel.Children.OfType<RichTextBox>().ToList();
List<TextBlock> texts = scrlPanel.Children.OfType<TextBlock>().ToList();
foreach(TextBlock txtb in texts)
{
RichTextBox rtb = rtbs[texts.indexOf(txtb)];
string rtbtext = new TextRange(rtb .Document.ContentStart, rtb .Document.ContentEnd).Text;
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtbtext + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
That is, if you just as many TextBoxes as RichTextBoxes and they have the same order.
PD: There's probably better ways of doing this, but this would be a quick fix.
I am working on a web browser project I want to make a web browser I used ToolStrip to put all the functions of the web browser(favorite, history, home, GO, back, forward). What I want now is to make tha Tabs.
1) what do you think the best way to implement the tabs is it TabControl or is there another way.
2) how do I make to click on a label next to each tab and I open the new tab with a label next to it. So I can open a third one and so on.
I found this code, but it does not add dynamically and it add the second tab with leaving the label on the first tab
this.tabControl1.SelectedTab = tabPage2;
1) i made a tabcontrol and deleted all the tabs in the form
2)i made a button look like a plus and one looks like a minus and added this code:
int Counter = 1;
this.tabControl1.TabPages.Add("Page " + Counter);
this.tabControl1.SelectTab(Counter - 1);
Counter = Counter + 1;
this will add a new tab with title page (1,2,3,4,..,n) and then i put a code when i press go to the specified Url:
RequestAndResponsHelper RS = new RequestAndResponsHelper(Url.Text);
StringBuilder s = new StringBuilder();
s = RS.GetRequest();//get the request from a different class
string HtmlString = s.ToString();
rtb = new RichTextBox();
rtb.AppendText(HtmlString);
rtb.Name = "RichText";
rtb.Dock = DockStyle.Fill;
this.tabControl1.SelectedTab.Controls.Add(rtb);
How to open a new window not in the same new window?
The following code can open a new window However after pressing button again
the content will be replaced. How to open the next or next next content in a new window not in the same new window
string strPop = "<script language='javascript'>" +
"window.open('viewpage.aspx?type=3&start=" + txtStartDate.Text + "&end=" + txtEndDate.Text + "','Report');" +
"</script>";
Page.RegisterStartupScript("Pop", strPop);
Replace the window name 'Report' with '_blank' or generate a unique window name instead of the static 'Report' name.
Always give the new windows new names and they'll open in separate windows