I asked a question here
and im unsure how to add the class the user gave me - I have just created a new class file then pasted in the class and i dont know how to apply that to the richtextbox?
Heres how my richtextbox is found... I have a richtextbox for each tabpage opened in my text editor i created a new textbox on the newtab void
public RichTextBox GetRichTextBox()
{
RichTextBox rtb = null;
TabPage starting = tabControl1.SelectedTab;
if (starting != null)
{
rtb = starting.Controls[0] as RichTextBox;
}
rtb.TextChanged += new EventHandler(txtBox_TextChanged);
rtb.MouseClick += new MouseEventHandler(rtbh_MouseClick);
//rtb.Select(rtb.Text.Length, 0);
rtb.Font = new Font(rtb.Font.FontFamily, 12);
rtb.Select(rtb.Text.Length, 0);
return rtb;
}
The class the user gave you inherits from RichTextBox - so when adding textboxes to your text editor, add this custom class. And for your function of finding textboxes, use the custom control. So change the above function to this:
public HighlightableRTB GetRichTextBox()
{
HighlightableRTB rtb = null;
TabPage starting = tabControl1.SelectedTab;
if (starting != null)
{
rtb = starting.Controls[0] as HighlightableRTB;
}
if (rtb != null)
{
rtb.TextChanged += new EventHandler(txtBox_TextChanged);
rtb.MouseClick += new MouseEventHandler(rtbh_MouseClick);
//rtb.Select(rtb.Text.Length, 0);
rtb.Font = new Font(rtb.Font.FontFamily, 12);
rtb.Select(rtb.Text.Length, 0);
}
return rtb;
}
The actual adding of the custom textbox should probably look something like this:
TabPage tabPage = new TabPage("Test");
tabPage.Name = "Test";
tabControl1.TabPages.Add(tabPage);
HighlightableRTB customTextBox = new HighlightableRTB();
tabControl1.TabPages["Test"].Controls.Add(customTextBox);
Related
I would iterative a TabPage and a WebBrowser controls.
I want to add a new webBrowser in a new Tab after each click. But I've got this error message :
Impossible to fix the index [] to this control-object
the code is the following :
TabPage var_i = new TabPage();
webBrowser webBrowser = new webBrowser();
int i = 0;
private void toolStripButton4_Click(object sender, EventArgs e)
{
webBrowser[i].Dock = DockStyle.Fill;
tabControl1.TabPages.Add(var_i[i]);
var_i[i].Controls.Add(webBrowser[i]);
i++;
if (textBox1_url.Text != "")
{
webBrowser[i].Navigate(textBox1_url.Text);
var_i[i].Text = textBox1_url.Text;
}
}
thanks you for your contribution.
M.A.
I found the solution. I used the function "AddRange" to the control TabControl and add a new value of tab collection.
tabcontrol1.Controls.AddRange(new Control [] { this.tabPage1 };
I want to display the question and options, and for every option the radio button should be add, and also question number in should be in number circle. suggest me how can to do this one
public Form2(string paperid)
{
InitializeComponent();
if (paperid != "")
{
var papers = doc.Descendants("paper");
foreach (var paper in papers)
{
if (paper.Attribute("id").Value == paperid)
{
var questions = paper.Descendants("question");
foreach (var question in questions)
{
Label ques = new Label();
ques.Text = question.Attribute("ques").Value;
this.Controls.Add(ques);
var options = question.Descendants("option");
var i = 0;
foreach (var option in options)
{
RadioButton rdbtn = new RadioButton();
rdbtn.Name = "rdbtn" + i;
this.Controls.Add(rdbtn);
rdbtn.Text = option.Value;
i++;
}
}
break;
}
}
}
}
In comments, you don't seem to know what a Control is. Everyone is telling you to use Label and RadioButton so just do it.
In the VS designer, select Label from the toolbox and drag it into the form. Change some of its properties, and BOOM! You've done it!
In the comments you said that you need to "store" the text in the Label. Well, that can be wrong in some contexts. You store text in strings, not labels. The latter displays the text.
You also mentioned how you get the text i.e. From XML. But that's irrelevant, you can just store the text you got from XML in a string, let's call it text. And then you change the Text property of the label.
label.Text = text;
Now your label will display the text.
EDIT
let me assume that you are not using VisualStudio. You can also do this with code. First you need to create a Form.
Form form = new Form();
form.Show();
//set properties of the form
Label label = new Label();
//set properties of the label. E.g. Text, width, position etc
form.Controls.Add(label);
So after you created the label, you set the text using the code before the edit and now your label should appear on the form.
private void button1_Click(object sender, EventArgs e)
{
if (id != "")
{
var papers = doc.Descendants("paper");
foreach (var paper in papers)
{
if (paper.Attribute("id").Value == id)
{
var questions = paper.Descendants("question");
var j = 1;
foreach (var question in questions)
{
GroupBox Ques&Ansoptn = new GroupBox();
Ques&Ansoptn.Size = new System.Drawing.Size(720, 120);
Ques&Ansoptn.Text = question.Attribute("ques").Value;
Ques&Ansoptn.Location = new Point(15, 40*j);
Ques&Ansoptn.Font = new Font("Microsoft Sans Serif", 10);
this.Controls.Add(Ques&Ansoptn);
var options = question.Descendants("option");
var i =1;
foreach (var option in options)
{
RadioButton rdbtn = new RadioButton();
rdbtn.Size = new System.Drawing.Size(400, 20);
rdbtn.Location = new Point(20, 20 * i);
rdbtn.Font = new Font("Microsoft Sans Serif", 10);
rdbtn.Text = option.Value;
Ques&Ansoptn.Controls.Add(rdbtn);
i++;
}
j = j+3;
}
break;
}
}
}
}
I have a panel called mainPanel in my Form1 and I want to add controls to it from another class. I tried to set the visibility to public but it didn't work.
This is where I create my controls:
public List<Control> addControlsToMain(string SearchWord, Size ContainerSize)
{
List<Control> ListOfControls = new List<Control>();
Panel Box;
Label Title, Content;
int PositionCounter = 10;
foreach (string data in GetSearchData(SearchWord))
{
Box = new Panel();
Title = new Label();
Content = new Label();
Box.Size = new Size((int)(ContainerSize.Width * 0.8), 100);
Title.Size = new Size(Box.Width, 20);
Content.Size = new Size((int)(Box.Width * 0.8), 60);
Box.Location = new Point(10, PositionCounter);
Title.Location = new Point(25, 10);
Content.Location = new Point(25, 40);
Title.Text = "Title";
Content.Text = "Content here";
ListOfControls.Add(Box);
Box.Controls.Add(Title);
Box.Controls.Add(Content);
PositionCounter += 110;
}
return ListOfControls;
}
Where GetSearchData(SearchWord) is just another function that returns random strings in a list, this function addControlsToMain() belongs to my class SearchFunctions.cs (a class separated from the Form1). I've tried to add these controls doing this:
var mainForm = new Form1();
SearchFunctions src = new SearchFunctions();
System.Drawing.Size panelSize = mainForm.mainPanel.Size;
foreach(System.Windows.Forms.Control data in src.addControlsToMain("Stack overflow", panelSize))
{
mainForm.mainPanel.Controls.Add(data);
}
My class CommandFunctions.cs is who has to add these controls. How can I add these list of controls to my panel?
You could go about this a few ways. One of which would be to add a SearchFunctions object as a a property to your Form1 class. Then use that object to call the method that adds the controls.
public partial class Form1 : Form
{
SearchFunctions src = new SearchFunctions();
public void Button_Click(object sender, EventArgs e)
{
List<Control> myControls = src.addControlsToMain(mySearchWord, mySize);
foreach (Control c in myControls)
{
this.Controls.Add(c);
}
}
}
This example uses a button click but you would place that method anywhere you want.
The problem is most likely here:
var mainForm = new Form1();
You're creating a new instance of Form1 that never gets displayed.
Here are some options:
Pass a reference to your existing Form1 instance into your class and add the controls using that reference.
or
Use the addControlsToMain() function directly from the Form itself so you can add the controls using this as Juken has demonstrated in his post.
or
Pass the controls out to the form using a custom event.
I have rather used webform, but still, this should work: I am basically trying to add controls
To a ** Panel** (instead of Form)in one class(say Main.cs) And
Create and add the controls to this Panel from another class(say class1.cs)
========================================================================
Steps:
1.Make use of the appropriate namespaces for calling control class utility
ex: System.Web.UI.Web Controls (in my case)
2.In Main.cs
Panel Panel1= new Panel();
foreach(some condition)
{
class1.addControlsToMain("xyz_Searchword", 2, ref Panel1); //Calls the method which creates the controls
}
Class1.cs
{
Public static void addControlsToMain(string searchword, int size,ref Panel p1)
{
List<WebControl> list = new List<WebControl>(); //should be List<Control> for Windows
Label lb1, title;
lb1 = new Label();
title = new Label();
lb1.Text = "Test Label Control1";
title.Text = "Test Title Label Control";
p1.Controls.Add(lb1);
p1.Controls.Add(title);
list.Add(p1);
}
}
To cut the long story short, try using Ref keyword. Hope this helps in a bit.
I'm trying to do a notepad with chrome-like tabs on it. I have a "New Page" button on my page. When I click on it, it creates a new tabpage with a richtexbox on it. The richboxes are created like this
public void yeni()
{
//create a new tabpage
TabPage newPage = new TabPage("Not-" + (tabControl1.TabPages.Count + 1));
//create a new richtexbox
RichTextBox rtb = new RichTextBox();
int rtbname = tabControl1.TabPages.Count + 1;
rtb.Name = "richTextBox" + rtbname.ToString();
rtb.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
rtb.BorderStyle = BorderStyle.None;
rtb.Width = 778;
rtb.Height = 395;
rtb.Location = new Point(0, 4);
rtb.HideSelection = false;
rtb.Font = new Font("Lucida Console", 9.75f);
rtb.ForeColor = Color.Maroon;
//add rtb to the tabpage
newPage.Controls.Add(rtb);
tabControl1.TabPages.Add(newPage);
//make the new created tab the selected one
tabControl1.SelectedTab = tabControl1.TabPages[tabControl1.TabPages.Count - 1];
//selectedRtb.Text = null;
openFileDialog1.FileName = null;
}
Now I create a RichTextBox and the name of that rtb is richTextBox*indexofthetabhere*. So if I'm working on the second tabpage, the name of the rtb is "richTextBox2". Now what I'm trying to do is I want an textchanged event for the richtextbox on the selected tabpage. I'm getting the selected richtextbox with this code here.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
selectedone = "richTextBox" + (tabControl1.SelectedIndex+1).ToString();
selectedRtb = (RichTextBox)tabControl1.SelectedTab.Controls[selectedone];
textBox2.Text = selectedone;
}
Now here I get the selected tab index and i get the rtb name then I get the selected rtb as "selectedRtb". Now I can't make a textchanged event for this. I don't know what to do actually. I tested if the above code was working and yes I get the rtb names right. But I can't use them because I don't know how to do.. Thanks for the help.
public void yeni()
{
//....
RichTextBox rtb = new RichTextBox();
rtb.Name = "richTextBox" + selectedTabPageIndex.ToString();
rtb.TextChanged += rtb_TextChanged;
//....
}
void rtb_TextChanged(object sender, EventArgs e)
{
RichTextBox rtb = (RichTextBox)sender;
if (rtb.Name == "richTextBox" + selectedTabPageIndex.ToString())
{
//rtb is selected page richtextbox
//......
}
}
You don't know how to create events? Or you can't access something while knowing it's name (use reflection)?
Alright I solved my problem. Here is the answer;
selectedRtb.TextChanged += (bs, be) =>
{
//whatever you want to do
};
Simply added this to my code after I created the rtb, and it worked. Thanks to everyone who helped.
I am developing a windows application containing 3 split containers(two panels each,
total 6 panels).
Now I want to add 3 labels dynamically in each panel.One solution I am trying to use for loop and access all splitcontainers and their panels but I dont know how to use for loop for accessing splitcontainer.
Can I use for loop for this? Also I want to add controls to all panels(6) at the same time. How to do this.
Thanks in advance..!!
This is what I have done...
foreach (SplitContainer sp in this.Controls)
{
Label tileTitle = new Label();
tileTitle.Text = "OneClick";
tileTitle.Visible = true;
tileTitle.Location = new Point(10, 10);
sp.Panel1.Controls.Add(tileTitle);
}
foreach (Control c in this.Controls)
{
if (c is SplitContainer)
{
Label tileTitle = new Label();
tileTitle.Text = "OneClick";
tileTitle.Visible = true;
tileTitle.Location = new Point(10, 10);
Label tileTitle2 = new Label();
tileTitle2.Text = "OneClick";
tileTitle2.Visible = true;
tileTitle2.Location = new Point(10, 10);
((SplitContainer)c).Panel1.Controls.Add(tileTitle);
((SplitContainer)c).Panel2.Controls.Add((tileTitle2));
}
}
Try to use the Controls.OfType extension to get only the controls of SplitContainer type
foreach (SplitContainer sp in this.Controls.OfType<SplitContainer>())
{
Label title = MakeLabel("OneClick", new Point(10, 10);
sp.Panel1.Controls.Add(title);
Label title1 = MakeLabel("OneClick", new Point(10, 10);
sp.Panel2.Controls.Add(title1);
}
private Label MakeLabel(string caption, Point position)
{
Label lbl = new Label();
lbl.Text = caption;
lbl.Location = position;
lbl.Visible = true;
return lbl;
}
edit
Steve, you add the same label to panel1 and panel2. i fixed the variable name in the add method of panel2.
i have done this in the same way as Steve did it, but i'm using a TableLayoutPanel to store all the splitcontainers, because you can add more than one SplitContainer with Dockstyle.Fill at the same time.
private void Form1_Load(object sender, EventArgs e)
{
foreach (SplitContainer sc in this.tableLayoutPanel1.Controls.OfType<SplitContainer>())
{
Label title = MakeLabel("OneClick", new Point(10, 10));
sc.Panel1.Controls.Add(title);
Label title1 = MakeLabel("TwoClick", new Point(10, 10));
sc.Panel2.Controls.Add(title1);
}
}
private Label MakeLabel(string caption, Point position)
{
Label lbl = new Label();
lbl.Text = caption;
lbl.Location = position;
lbl.Visible = true;
return lbl;
}
the solution works perfectly as seen here: http://imageshack.us/photo/my-images/838/splitcontainer.png/