C# Form Size won't not set - c#

i have a panel in a form and i load another form in this. this new loaded form have a picture box and the picture was larger as the main form. i want to scroll in the new form and show the picture. i set the size of the new form in code but in the breakpoint i can see the size show the right settings but if i show the settings in a message it show the false settings.
here the code
Form maleTree = new maleTreeForm(maleTreePanel);
maleTree.TopLevel = false;
maleTreePanel.Controls.Add(maleTree);
Form maletreeForm = maleTreePanel.Controls.Find("maleTreeForm", true).FirstOrDefault() as Form;
PictureBox pictureBox = maleTreePanel.Controls.Find("maletreePictureBox", true).FirstOrDefault() as PictureBox;
maletreeForm.Size = new Size(pictureBox.Width, pictureBox.Height);
MessageBox.Show(maletreeForm.Size.toString());
the picturebox.width show in breakpoint 2300, but in messagebox 1300.
i want to set the full 2300 how i can solve this ?
this is the result
you see in the right the scroll bar is not completly down but the image end

Related

Displaying form at the same location as parent of calling button

I have a UserControl with a button in a panel which is on the UserControl (this is part of a custom calendar set up with a TableLayoutPanel and the UserControl is what displays the contents of each day in the calendar). Clicking the button displays a dialog. I want the dialog to display at the same location as the UserControl. I have the StartPosition property for the dialog set to "Manual".
dlgStatuses DlgStatuses = new dlgStatuses(this);
ucDate ucParent = (ucDate)((Button)sender).Parent.Parent; //This is the UserControl
int x = PointToScreen(ucParent.Location).X;
int y = PointToScreen(ucParent.Location).Y;
DlgStatuses.Location = new Point(x, y);
DlgStatuses.ShowDialog();
This is not working as desired. I've tried PointToClient and it fails as well. I've also tried this and it fails:
dlgStatuses DlgStatuses = new dlgStatuses(this);
ucDate ucParent = (ucDate)((Button)sender).Parent.Parent;
DlgStatuses.Location = ucParent.Location;
DlgStatuses.ShowDialog();
So what I'm looking for is to have DlgStatuses display at the same location as ucParent.

RichTextBox not showing the text

I am trying to create a Bitmap from a RichTextBox and set it as the background image for a panel, but unfortunately the text is not shown.
Bitmap l_bitmap = new Bitmap(m_control.Width, m_control.Height);
m_control.DrawToBitmap(l_bitmap, new Rectangle(0, 0, l_bitmap.Width, l_bitmap.Height));
m_panel.BackgroundImage = l_bitmap;
m_panel.Refresh();
m_control is my RichTextBox. When I debug, I can see that the control contains the text I wrote, but the bitmap just shows an empty RichTextBox.
I use the same code for other types of controls (Button, CheckBox, TextBox...). The text is shown with no problems.
Well you are trying to create a bitmap from the control. The text you put in there isn't the control, so it won't bother to chow it as bitmap. Try to create a picture from screen (like a screenshot).
Example:
Graphics gr = Graphics.FromImage(l_bitmap);
gr.CopyFromScreen(m_control.PointToScreen(Point.Empty), point.Empty, m_control.Size);
This will make a bitmap from your given points. This will additional show you the text.
EDIT
Maybe you can use this instead. In addition to your idea, I simply put a label onto my panel. (L for Label and P for Panel)
As you can see, the label is empty because I cleared the Text property. Now, when you click one of the buttons below the panel, it will update the label.Text propertie and there will be the text you gave the control.
Here is some example:
As you can see, the label shows the Name of the control. Completly custom as you can see on my source code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public RichTextBox tmpRtf = new RichTextBox();
//Poor button name incoming...
private void button1_Click(object sender, EventArgs e)
{
if (tmpRtf == null)
tmpRtf = new RichTextBox();
//You can add any text here and it will be shown on the label.
this.tmpRtf.Text = "Richtextbox";
this.UpdatePanel(this.tmpRtf);
}
//Custom method to update the panel for any control. Can pobably be done way better than this, but hey.
private void UpdatePanel(object pControl)
{
//Checks if control is a rtf
if(pControl is RichTextBox)
{
//This is your code! Ay.
Bitmap l_bitmap = new Bitmap(this.panel1.Width / 2, this.panel1.Height / 2);
(pControl as RichTextBox).DrawToBitmap(l_bitmap, new Rectangle(0, 0, l_bitmap.Width, l_bitmap.Height));
this.tmpRtf.BackColor = Color.LightGray;
this.panel1.BackgroundImage = l_bitmap;
this.panel1.BackgroundImageLayout = ImageLayout.Center;
this.labelControlName.Text = this.tmpRtf.Text;
this.panel1.Refresh();
}
}
}
Its not possible to show text on a control thats not visualized. But you can build a workaround! Or, instead of taking a picture you can simply create the control on top of it, that will also show the Text and maybe the user can test it (e.g. click on buttons, look at the control behaviour).
Hopefully this is something to get you inspired that there are always more ways to accomplish.

How to add a picturebox above a button control

I have a picturebox1 -> Button -> picturebox2 all three are in a consecutive layer so which I want is that picturebox2 should be appear within the button when I debug the program.
My code is,
public Form1()
{
InitializeComponent();
picturebox2.parent = button;
picturebox.backcolor = color.transparent;
}
I am using .jpg for picturebox1 and a .png for picturebox2 but its not appearing. I mean the picture of picturebox2 should appear above the button.
You need to nest all 3 controls.
You also need to correct the Location of the nested controls or else they keep the original location, which are relative to their original parents, probably to the form, and not to their new parents!!
This should work better:
public Form1()
{
InitializeComponent();
button.Parent = picturebox;
picturebox2.Parent = button;
picturebox.BackColor = Color.Transparent;
button.Location = new Point(1,2); // or whatever you want!!
picturebox2.Location = new Point(3,4); // or whatever you want!!
}
You may also want to consider simply using the Image and/or the BackGroundImage properties of the Button..
Note: If you want your Button to let the bottom PictureBox shine through you need to not only set its Color but also it's FlatStyle to Flat!

Position of link in LinkLabel

I want a box of a fixed size to show some text and have a link in it that is clickable in the bottom right corner for editing. Clicking this edit link shows a set of fields to fill in.
I tried LinkLabel, which does the trick, but, when I change the text, the size of the box changes. I set autosize to false and longer text forces the link outside the box. Short text puts the link to far up.
I could get fancy and calculate the position of the link and insert it at the appropriate place (adding new lines if needed), but I'm wondering if there isn't an easier way to do this.
Is there a better control for doing this or another way of doing this?
EDIT:
The boxes that are filled in are concatenated and replace the text in the linklabel. The Edit link is currently appended to this and a LinkArea (of the last 4 characters) is set.
You need to build a composite layout, for instance using a Panel with Label/TextBox (Dock = Fill) and LinkLabel (Dock = Bottom, TextAlign = MiddleRight) inside, like this
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var text = "I want a box of a fixed size to show some text and have a link in it that is clickable in the bottom right corner for editing.";
int textSize = 50;
var form = new Form { Padding = new Padding(8) };
var panel = new Panel { Parent = form, BorderStyle = BorderStyle.FixedSingle, Padding = new Padding(4) };
var label = new Label { Dock = DockStyle.Fill, Parent = panel, AutoSize = false, Text = text, Height = textSize };
var link = new LinkLabel { Dock = DockStyle.Bottom, Parent = panel, AutoSize = false, TextAlign = ContentAlignment.MiddleRight, Text = "Edit" };
panel.Location = form.DisplayRectangle.Location;
panel.Width = form.DisplayRectangle.Width;
panel.Height = panel.Padding.Vertical + link.Height + label.Height;
Application.Run(form);
}
}
}
Result:
I found another way.
I use a text box and position a linklabel in its corner.
textbox is readonly and disabled so it acts like a label and the backcolor is set to white to over-ride the disabled/readonly colour.
The edit link now stays put

How to Create Popup Form With Same Location of Other Control

I have read about Control.PointToClient or Control.PointToScreen on MSDN.
But how to show another form as same location as TextBoxwhen Button clicked, if I have this control hierarchy ?
+- Form
+--- Panel
+---- Panel
+------ TextBox
+------ Button
Use PointToScreen with an empty point (0, 0) to get the location of the control relative to the screen, then just show the form there (make sure StartPosition is Manual):
Point controlPosition = myTextBox.PointToScreen(Point.Empty);
MyForm newForm = new MyForm();
newForm.Location = controlPosition;
newForm.Show(this);
If you show new form as showdialog you need to use :
newForm.StartPosition = FormStartPosition.Manual;

Categories