It easier to see my problem in below image. I need to fill the tabPage with a chart, and then put some buttons in the top left corner.
How may I do this programmatically?
Here is what I have tried:
//UI
tabControl1.TabPages.Clear();
TabPage tp1 = new TabPage("tp1");
tabControl1.TabPages.Add(tp1);
Button btn1 = new Button();
btn1.Text = "OK";
btn1.Parent = tp1;
Button btn2 = new Button();
btn2.Text = "OK2";
btn2.Size = new System.Drawing.Size(220, 50);
//btn2.Anchor = AnchorStyles.None;
//btn2.Margin = new Padding(200,3,3,3);
btn2.Parent = tp1;
Chart cht = new Chart();
cht.Parent = tp1;
cht.Dock = DockStyle.Fill;
Related
I created separate class with custom button, which i want to be poped when this "newButton" is clicked.
How do i do it manually? so if there is multiple "newButtons" and i want to hide previous apeared Custom Buttons
My problem is that i dont know how to seek locations of the new buttons because it is in flowLayout which puts them automatically.
Button newButton = new Button();
newButton.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(175)))), ((int)(((byte)(241)))));
newButton.FlatAppearance.BorderSize = 2;
newButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
newButton.Font = new System.Drawing.Font("Arial", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
newButton.ForeColor = System.Drawing.Color.White;
newButton.Location = new System.Drawing.Point(158, 288);
newButton.Size = new System.Drawing.Size(150, 38);
newButton.UseVisualStyleBackColor = true;
newButton.Text = k.grp;
newButton.Click += (sender, e) => newButton_Click(sender, e);
if (checkinNames.Contains(newButton.Text))
{ }
else
flowLayoutPanel5.Controls.Add(newButton);
checkinNames.Add(k.grp);
You can't achieve it by specific property directly in FlowLayoutPanel. All visual controls placed in it will automatically adjust their positions according to the size of the FlowLayoutPanel.
Here is a workaround via setting button border, backcolor, text and enable.
private void HideButton(Button button)
{
button.BackColor = this.BackColor;
button.Text = string.Empty;
button.TabStop = false;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.Enabled = false;
}
Hope this can help you.
I'm observing that when the Popup control loses focus it returns focus back to the object that initiated the Popup. So basically this sequence of events.
Textbox GotFocus fires and I open the popup
When finished with the Popup user clicks anywhere outside Popup to activate LostFocus
LostFocus activates, but immediately after GotFocus for the object in step 1 (Textbox) executes - relaunching the popup.
My question, is there a way to reset focus to whatever the default is when you first start an app? I want to prevent it from returning focus back to the control that launched the popup.
EDIT: I removed LostFocus event because I found it wasn't necessary in order to remove the popup from view when user taps away.
Popup p;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
p = new Popup();
p.IsLightDismissEnabled = true;
//p.LostFocus += p_LostFocus;
}
private void m_GotFocus(object sender, RoutedEventArgs e)
{
if (p.IsOpen != true)
{
// Create some content to show in the popup. Typically you would
// create a user control.
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(0);
StackPanel panel1 = new StackPanel();
panel1.FlowDirection = Windows.UI.Xaml.FlowDirection.LeftToRight;
panel1.Background = new SolidColorBrush(Color.FromArgb(255, 30, 30, 30));
Button button1 = new Button();
button1.Content = ".";
button1.Margin = new Thickness(5.0);
button1.BorderThickness = new Thickness(0);
button1.Background = new SolidColorBrush(Color.FromArgb(255, 90, 90, 90));
button1.Click += new RoutedEventHandler(Feedback_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "";
textblock1.TextWrapping = TextWrapping.WrapWholeWords;
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
// Set where the popup will show up on the screen.
p.VerticalOffset = 400;
p.HorizontalOffset = 100;
// Open the popup.
p.IsOpen = true;
}
}
Why my Ok and Cancel Button are not showing up on the UI screen? I see the form, the label and the text box but I can't see the Cancel and OK buttons.
To give you a background I am creating this dialog box programmatically and all I need a a couple of text boxes , and their labels of course. And an OK and Cancel button .
All these sizes that I have used here is by trial and error as I am not much experienced in the UI control area of Visual C# 2010.
public void function x ()
{
var fileNameDialog = new Form();
fileNameDialog.Text = "Save New Name";
Label fileLabel = new Label();
fileLabel.Size = new System.Drawing.Size(150, 40);
fileLabel.Text= "Enter Person Name";
fileNameDialog.Controls.Add(fileLabel);
TextBox fileTextBox = new TextBox();
fileTextBox.Location = new System.Drawing.Point(fileLabel.Location.X + 300, fileLabel.Location.Y);
fileTextBox.Size = new System.Drawing.Size(220, 40);
fileNameDialog.Controls.Add(fileTextBox);
fileTextBox.TextChanged += TextBox_TextChanged;
fileTextBox.Text= textboxValue;
Button okButton = new Button();
okButton.Visible = true;
okButton.Text = "OK";
okButton.Location = new System.Drawing.Point(fileTextBox.Location.X, fileTextBox.Location.Y - 80);
fileNameDialog.Controls.Add(okButton);
okButton.Click += new EventHandler(okButton_Click);
Button cancelButton = new Button();
cancelButton.Visible = true;
cancelButton.Text = "Cancel";
fileNameDialog.Controls.Add(cancelButton);
cancelButton.Click += new EventHandler(cancelButton_Click);
cancelButton.Location = new System.Drawing.Point(fileTextBox.Location.X+50, fileTextBox.Location.Y - 80);
fileNameDialog.ShowDialog();
}
Your fileTextbox.Location.Y is zero, so subtracting 80 puts in above the form.
Try fileTextBox.Bottom + 4 or something like that.
Using the designer to create this dialog form is probably the better route to take. Along with the placement of the controls, you can use Anchors to make the controls relate to the size of the form.
I add a MenuStrip to my form and I would like to add other controls below it like usual Point(0, 0) is the top left corner of empty form space. After I add the menu to my form and add more controls they overlap each other. So I want to take away some height of the client rect for the menu and a button with Location = (0,0) must be RIGHT below the menu.
How do I do that ?
If I assign a MainMenu to Menu property of the form it does that automatically but I really want and need to use MenuStrip.
Edit: this doesn't work:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = 50;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 25, 128, 50);
Controls.Add(b);
While this works like I would like it to do with MenuStrip:
Menu = new MainMenu();
Menu.MenuItems.Add("File");
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
When you SetBounds(0, 25, 128, 50), you are actually setting b.Top to 25 (the second parameter). In order to set the top bound relative to the menu control, use:
b.SetBounds(0, menu.Bottom, 128, 50);
[UPDATE]
Alternatively, you could use:
public partial class Form1 : Form
{
private int menuStripHeight = 50;
public Form1()
{
InitializeComponent();
this.ControlAdded += Form1_ControlAdded;
}
private void Form1_Load(object sender, EventArgs e)
{
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
// note that the position used is 0,0
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
}
void Form1_ControlAdded(object sender, ControlEventArgs e)
{
if (e.Control.GetType().FullName != "System.Windows.Forms.MenuStrip")
e.Control.Top += menuStripHeight;
}
}
[UPDATE 2]
Or you could just use a Panel:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Panel p = new Panel();
p.SetBounds(0, menuStripHeight, this.Width, this.Height);
Controls.Add(p);
Button b = new Button();
b.Text = "hello world";
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
Use DockStyle.Top in both MenuStrip and Panel, but add them in the reverse order. Adding a control with Dock=Top puts this last control closest to the border, that is, on top of all the other controls. So without resorting to private constants and event handlers:
MenuStrip menu = new MenuStrip() {
AutoSize = false,
Dock = DockStyle.Top
};
menu.Items.Add("File");
Panel p = new Panel(){
Dock = DockStyle.Top
};
Controls.Add(p);
Controls.Add(menu);
MainMenuStrip = menu;
Button b = new Button(){
Text = "hello world"
};
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
private void newThumbNail(int docType, string fileName)
{
thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Parent = panel1;
thmbNail[thmbNailCnt].Visible = true;
thmbNail[thmbNailCnt].Location = new Point(2, 5);
thmbNail[thmbNailCnt].Size = new Size(222, 50);
picBox[thmbNailCnt] = new PictureBox();
picBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
picBox[thmbNailCnt].Visible = true;
picBox[thmbNailCnt].Location = new Point(6, 13);
picBox[thmbNailCnt].Size = new Size(31, 31);
//picBox[thmbNailCnt].Image = new Bitmap("images/excel.png");
texBox[thmbNailCnt] = new TextBox();
texBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
texBox[thmbNailCnt].Visible = true;
texBox[thmbNailCnt].Location = new Point(53, 24);
texBox[thmbNailCnt].Size = new Size(163, 20);
texBox[thmbNailCnt].Text = fileName;
texBox[thmbNailCnt].Enabled = false;
Controls.Add(thmbNail[thmbNailCnt]);
Controls.Add(picBox[thmbNailCnt]);
Controls.Add(texBox[thmbNailCnt]);
}
this is a function that dynamically adds a groupBox with some controls int it inside a panel. Unfortunately it does not appears inside the panel. The panel was created before hand using the c# design tools. It is placed directly on top of the windows form at 15,52 having a size of 279,489. Help please.
It seems that you are adding these controls to the form controls collection.
Instead you should use the panel controls collection like:
panel1.Controls.Add(thmbNail[thmbNailCnt]);
panel1.Controls.Add(picBox[thmbNailCnt]);
panel1.Controls.Add(texBox[thmbNailCnt]);
Try Panel.Controls.Add(thmbNail[thmbNailCnt])
Also a tip to make your code faster and easier to read:
// Not modified to use Panel.Controls.Add()
GroupBox box = new GroouBox();
thmbNail[thmbNailCnt] = box;
box.Parent = panel1;
box.Visible = true;
box.Location = new Point(2, 5);
box.Size = new Size(222, 50);