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.
Related
This question already has an answer here:
Alignment of Items in GroupBox
(1 answer)
Closed 4 years ago.
I've created window applications and would like to center elements in the groupbox. Can I do this with a code?
groupbox
This code assumes you will be creating the additional controls entirely in code and not using the Designer.
If you do use the Designer, just remove the line
Button button = new Button() { Text = "Button1" };
and put the name of the button control in the next line.
private void AddControlsToGroupBox()
{
Button button = new Button() { Text = "Button1" };
CentreControlInGroupBox(this.groupBox1, button);
}
private void CentreControlInGroupBox(GroupBox theGroupBox, Control theControl)
{
// Find the centre point of the Group Box
int groupBoxCentreWidth = theGroupBox.Width / 2;
int groupBoxCentreHeight = theGroupBox.Height / 2;
// Find the centre point of the Control to be positioned/added
int controlCentreWidth = theControl.Width / 2;
int controlCentreHeight = theControl.Height / 2;
// Set the Control to be at the centre of the Group Box by
// off-setting the Controls Left/Top from the Group Box centre
theControl.Left = groupBoxCentreWidth - controlCentreWidth;
theControl.Top = groupBoxCentreHeight - controlCentreHeight;
// Set the Anchor to be None to make sure the control remains
// centred after re-sizing of the form
theControl.Anchor = AnchorStyles.None;
// Add the control to the GroupBox's Controls collection to maintain
// the correct Parent/Child relationship
theGroupBox.Controls.Add(theControl);
}
The code also assumes you will place only a single control in the Group Box. I don't think you will find it too hard to adjust the sample accordingly if there are multiple controls.
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
I have tried to create a radiobutton dynamically and add it to groupbox/form, but the whole text associated with the radiobutton is not getting displayed. When the radiobutton is added from the designer, the whole text is getting displayed. For dynamically adding radio button am I missing anything or are there any ways to do it?
Please find the sample code below:
public partial class Form1 : Form
{
private void SelectMicrophone_Load(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton r1 = new System.Windows.Forms.RadioButton(); //created a radiobutton
r1.Name = "Microphone(RealTex";
r1.Text = "Microphone(RealTex";
r1.Location = new System.Drawing.Point(15, 15);
this.groupBox1.Controls.Add(r1);
When you set the text property in the designer, it adjust the radio button to the new size to cover the width of the text. By default I think the width is 90 and with the text above it is resized to a width of 124. So when you create the object at runtime, it is probably just keeping the width to 90. You can however just set r1.Width = 124 before adding it to your controls collection.
Keep in mind that you may not know the length each time so you could either set the width to the maximum size that you need or use the TextRender's .MeasureText method to get the size of the text and then just add 20 to that to cover the graphic of the radio button circle that also appears and set the result of the X property to your width before adding the radiobutton to the collection.
RadioButton r1 = new RadioButton();
r1.Text = "This is short text";
//Measure the Text property and get the width and add 20 to accomodate the circle
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 15);
this.Controls.Add(r1);
//Just another RB with even longer text.
r1 = new RadioButton();
r1.Text = "This is even longer text that we want to show";
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 35);
this.Controls.Add(r1);
I'm creating a chat program and for the chatbox i want there to be a whatsapp kind of style message layout. My way of doing it (probably not the best) is by creating a picturebox dynamically with my blue background picture in it and then adding a label , making the picturebox the parent and then overlay the message over the picture box. I'm using this bit of code :
private void CreateChatBox(int height, string message)
{
PictureBox pb = new PictureBox();
Label pbl = new Label();
pb.Name = height.ToString();
pbl.Text = message;
pbl.Name = height.ToString();
pb.Image = LocalChat.Properties.Resources.ChatBox_Test;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
// set picturebox possitions and margins
pb.Left = 15;
pb.Top = 100;
pb.Width = 250;
pb.Height = 75;
tabPage.Controls.Add(pb);
//set label positions and margins
pbl.Parent = pb;
pbl.AutoSize = true;
pbl.Width = 200;
pbl.BackColor = Color.Transparent;
pbl.Location = new Point(1, 1);
// Add button click event Handler and add buttons and lables to the panel
tabPage.Controls.Add(pbl);
}
my problem is that i'm making the picturebox the parent of the label, yet the label will just sit on the very top left of the form and not inside the picturebox as i want. Am i not getting what parent should be doing?
or how do i get my label to be confined inside the picturebox?
You must remove your line of code
tabPage.Controls.Add(pbl);
at the end of the file
because after you set the parent of the label to the PictureBox you're setting its parent to the TabPage, again
When you use anyControl.Controls.Add(otherControl) you're setting the otherControl.Parent to anyControl
But i'll say that if you want that kind of control settings you should instead of dynamically creating and setting its control you could create an usercontrol and inside the usercontrol you would put the logic to the display of the message, and you could dinamically create the userControl and put it anywhere you would like
I have two panels on a form. I want one of them, when an user maximizes the form, the panel to be maximized too, depending on the form. I tried and the panel is shown on the entire form,hiding the other panel.
Here is my code:
public MainForm()
{
InitializeComponent();
panel2.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
panel2.Dock = DockStyle.Fill;
TaskControl t = new TaskControl();
int x, y;
x = 0;
y = 0;
t.Location = new Point(x, y);
panel2.Controls.Add(t);
t.BringToFront();
}
I managed to do it. I changed the values of Anchor from Properties. Thank you for help!
It sounds like you want a splitcontainer. With this control you get two panels. Set the "fixedpanel" property to the panel that you want to not resize. The other panel will resize as the form is resized (or maximised)
You should NOT only do so in initialization, but also in Form_Resize event etc. For instance,
you can handle Form.ResizeEnd event, see reference here.