Why can't I create a RichTextBox from a reference of RichTextBox - c#

I'm confused with the RichTextBox control.
I have a RichTextBox control of my WinForm with a width of 100px. I want to create one dynamically and then reference the instance.
So, assuming on my Form I have a RichTextBox called rtbResult I would have thought I can do the following:
RichTextBox rtb = new RichTextBox();
rtb.Text = "Hello all";
rtb.Width = 50;
rtbResult = rtb;
When the code executes, the result is an empty RichTextBox on my page, at the original size of 100px (eg, not at width 50).
Please note, in live I'm using text highlighting and as such it must be a RichTextBox, the example above is stripped to make the question more concise.
Any ideas what I'm doing wrong?

What you did is not so different from this:
RichTextBox r1 = new RichTextBox();
r1.Text = "I am RTB #1";
Controls.Add(r1);
RichTextBox r2 = new RichTextBox();
r2.Text = "I am RTB #2";
Now on your page you have the RichTextBox named r1. If you do this:
r1 = r2;
You do not replace the control in the form with the new RichTextBox, you simply assign the same reference to the variable r1. In your form you still have the old control but both variables (r1 and r2) point to the same object.
What you may do, if you can't simply update the original RichTextBox with new values, is to delete the old one and to add the new one in the same position:
r2.Bounds = r1.Bounds;
Controls.Remove(r1);
Controls.Add(r2);
This is pretty naive, many properties may need to be copied from the old one to the new one (Dock, TabIndex and so on), moreover the order is important too so you may need to call Controls.SetChildIndex() for proper positioning (this depends on what you really have to do and how your code is).

The rtbResult is just a variable that contains a reference to the control, it's not the control itself. By putting the new control in that variable you are only changing the variable, the original control is still in the form, and the new control doesn't belong to any form so it's not visible anywhere.
You have to add the new control to the form to make it visible, for example:
somePanel.Controls.Add(rtb);

Related

UserControl inside Panel inside UserControl not working

I am having some trouble creating the template of my application :
I am making some dynamic parts of forms and I use a panel in which I include my User Control according to the choice of the user. The panel has the same size as the user control but it doesn't work at runtime. The panel in which the UC is included is inside another UC also.
I tried autoSize = true, fixed size and many things but I don't know why it goes like this. Any idea?
Some parts of my code to load the User Control :
UC_panel.Controls.Clear();
UC_ADDRESS uca = new UC_ADDRESS();
uca.Dock = DockStyle.Fill;
uca.AutoSize = true;
UC_panel.Controls.Add(uca);
My UserControl for the address
At runtime it goes bigger
Since you set
uca.Dock = DockStyle.Fill;
and also
uca.AutoSize = true;
(which is redundant)
it is possible that some other container has its AutoScaleMode set to Font and its font size is bigger.

Is it possible to add more than one panel in same location

i'm using telerik control.
So i want to ask,
In winforms application ,Is it possible to add more than one panel in same location and display one at a time just like show/hide property.
Make sure you have placed all panel control in same container or form. then you can use Visible property to show and hide panel. BringFront and SendToBack function will be used to bring panel on top or send it to back. If you have placed any panel in another panel then that will be disappeared when you Hide parent panel. So, Make sure all panels' parent control must be same. To determine the parent control simply select that panel and press escape key to select their parent.
private void LoadPanels()
{
panel1.Location = new Point(10,10);
panel2.Location = new Point(10,10);
panel3.Location = new Point(10,10);
panel4.Location = new Point(10,10);
panel5.Location = new Point(10,10);
VisiblePanel("panel1");
}
private void VisiblePanel(string panelName)
{
string[] panels = new string[]{"panel1","panel2","panel3","panel4","panel5"};
for (int i=0;i<panels.Length;i++)
this.Controls[panels[i]].Visible = (panels[i] == panelName);
this.Controls[panelName].BringToFront(); //Not required you can remove this line.
}
Here's a slightly different approach you might want to consider...
Are you wanting to be able to programmatically select the contents of a rectangular area at runtime, selecting among various controls to display? If so, you could use a custom TabControl which has its tabs (not the pages) hidden.
Then you can select which page is displayed by programmatically changing its SelectedIndex property at runtime.
Doing it like this means that your form editor will show a normal tab control, which allows you to much more easily add the content to each page - but at runtime the tabs will be hidden from the user; they will just see the contents of the currently selected page.
See Hans Passant's answer here for how to create such a custom tab control.
(However, you might also want to override the OnKeyDown for the custom tab control in order to ignore Ctrl-Tab.)

Arrange controls in panel in ASP.NET

I want to arrange the controls in a Panel control. For example, I have four labels and four textboxs, I want to put them in a panel like a table without using the VS designer, only use the code. Does anyone do it before?
Best Regards,
C#, and use styles to control layout.
Panel pnl = new Panel();
Label lbl1 = new Label();
lbl1.Text = "1";
pnl.Controls.Add(lbl1);
TextBox tb1 = new TextBox();
pnl.Controls.Add(tb1);
Page.Controls.Add(pnl);
label
{
display: inline;
}
You could essential do the same thing Visual Studio does on the back end. Create a new control and set the properties, such as: size, name, text, and location.
I think that you can simply create a custom control that contains a label and a text box. Let's call this control LabeledTextBox. And then on your code simply add 4 instances of LabeledTextBox one after the other. This should provide the look and feel you want.

trying to add/remove controls to a form

plz keep in mind I'm very noobish...
What I'm trying to do is add "blips" to my form. I have a calculation that determines where all these "blips" will be, which is plotted on a graph. the solution determines that the coordinates are "blipHours, blipAltitude"
I want to somehow add small dots to my form at these locations. At first I was going to create something to be my "blips" but then I realized that I want it so small that I could just use an empty picture box with the background color what I want (I know this isn't the best way but I'm still very new to this).
I've created the code that will add the blip
PictureBox blip = new PictureBox();
blip.Location = new Point(blipHours, blipAltitude);
blip.Size = new Size(6, 6);
blip.BackColor = System.Drawing.Color.Lime;
blip.Text = "";
blip.Name = callsign;
this.Controls.Add(blip);
It adds the blip, but it always adds it underneath other controls. Is there a way to make it add the new blip on top of everything else so that it's visible?
my second question is how do I remove all of the blips that get created at once with the click of a button?
An alternative to nobugz' answer is to change the Z-order of your controls via the Form.Controls.SetChildIndex method:
this.Controls.Add(blip);
this.Controls.SetChildIndex(blip, 0);
You can use .AddAt to set its position in the list of controls.

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