Sometime ago I asked this question. I get an answer and it worked fine at time. But now, I'm trying to do the same but isn't working. I have a Form and a FlowLayoutPanel setted in the same way as the answer but it isn't working. Both Form has FLowLayoutPanel has set AutoSize to true and FlowDirection set to TopDown but the form is growing vertically without pushing down the progressBar control and label itself. Here's what's like my form after click on button a couple of times(the button's code is the same as in the accepted question in the link I have linked):
What am I missing?
Try this and see if it works!
public Form1()
{
InitializeComponent();
Size = new Size(400, 150);
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Size = new Size(200, 150);
panel.MaximumSize = new System.Drawing.Size(panel.Width, int.MaxValue);
panel.FlowDirection = FlowDirection.TopDown;
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Controls.Add(panel);
Label label = new Label();
label.Text = "Starting text!\n";
label.Padding = new System.Windows.Forms.Padding(0, 0, 0, 50);
label.AutoSize = true;
panel.Controls.Add(label);
ProgressBar progressBar = new ProgressBar();
progressBar.Location = new Point(0, 125);
progressBar.Size = new Size(190, 25);
panel.Controls.Add(progressBar);
Button button = new Button();
button.Location = new Point(275, 50);
button.Text = "Click me!";
button.Click += (object sender, EventArgs e) => { label.Text += "some more text, "; };
Controls.Add(button);
}
Ok, I've tested the solution suggested in the earlier post you made and it's working fine for me...
Test these things:
Make sure both the Label and the ProgressBar are located inside the FlowLayoutPanel
If you mean that it's growing horizontally <---->, then set the MaximumSize-Width of the FlowLayoutPanel to how wide it can be before switching to a new row (and from there growing vertically instead!)
Otherwise please provide more information so that I can help you from there.
Related
I have set this flowLayoutPanel, the controls inside arrange well, till the last arrives to the bottom border of the panel, then the controls start arranging on the right side (forming another column) keepping the vertical flow. I just want one column.
this.panel.Anchor =
((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)| System.Windows.Forms.AnchorStyles.Right)));
this.panel.AutoScroll = true;
this.panel.BorderStyle = BorderStyle.None;
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.panel.Location = new System.Drawing.Point(0, 184);
this.panel.Name = "myPanel";
this.panel.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.panel.Size = new System.Drawing.Size(300, 371);
this.panel.TabIndex = 9;
Use
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;
instead of
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
if you want only one column than please add below code to your application just after control added to your flowlayoutpanel
this.panel.SetFlowBreak(<<YOUR_ADDED_CONTROL_NAME>>, true);
Example
Button btn1 = new Button();
btn1.Text = "TEST";
btn1.Height = 30;
btn1.Width = 100;
this.panel.Controls.Add(btn1);
this.panel.SetFlowBreak(btn1, true);
So I need to add textboxes to a panel with a click of a button. Every click adds one textbox under the last one and so on. But when it goes over the panel height it suddenly makes bigger space between the texboxes even though the int is still the same.
Here's my code so far.
List<TextBox> textboxes = new List<TextBox>();
private void button1_Click(object sender, EventArgs e)
{
tbY += 30;
TextBox tb = new TextBox();
tb.Left = 3;
tb.Top = tbY;
tb.Font = new Font("Verdana", 12, FontStyle.Bold);
tb.Size = new Size(325, 25);
tb.BorderStyle = BorderStyle.None;
button1.Top = tbY;
panel1.Controls.Add(tb);
textboxes.Add(tb);
ScrollToBottom(panel1);
}
The Top of a Control is calculated relative to the scroll position of its Parent.
You are always scrolling to the bottom of your Panel, so you need to set it like this, taking the curent scroll position into account:
tb.Top = tbY + panel1.AutoScrollPosition.Y;
Note that the AutoScrollPosition.Y is negative when you have scrolled downward, so we need to add it!
You might as well use a flowLayoutPanel for this purpose. Use the following properties on your flowLayoutPanel and it'll work as you intend. (without having to do the manual calculation)
List<TextBox> textboxes = new List<TextBox>();
public Form1()
{
InitializeComponent();
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel1.AutoScroll = true;
}
private void button1_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Left = 3;
tb.Font = new Font("Verdana", 12, FontStyle.Bold);
tb.Size = new Size(325, 25);
tb.Text = tb.Top.ToString();
tb.BorderStyle = BorderStyle.None;
flowLayoutPanel1.Controls.Add(tb);
textboxes.Add(tb);
}
I have one textbox (textBox1) and panel (Panel1) I have code like this
Panel1.Controls.Add(textBox1)
so when I run it I can't see textbox anymore, If I do like this I can see textBox
textBox1.Location = Panel1.Location
can anyone tell me what's problem?
When a textbox (or any control) is part of a panel the top left of the panel is point(0.0);
so when textBox1.Location = Panel1.Location the textbox probably falls out of view in the panel.
try something like this instead/
//
// panel1
//
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(59, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(193, 176);
this.panel1.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
I believe the reason you're not able to see the Textbox has to do with the Panel's properties. Try setting the AutoSize property to true and the AutoSizeMode property to GrowAndShrink.
I'm trying to set an image background in code-behind. I tried adding a background image to the button, but as soon as I hover over the button, the image disappears. To solve this, I have to write functions to override the button behavior, which is too much to do in code-behind.
I then use an alternative method, that is to add a button and an image separately to a grid cell. The issue is -- when I click on the image, the button won't trigger.
How do I make the button to have the hover and pressed effect, even when, the mouse is either hovering or presses the image on the button, but not the remaining area of the button?
Or hope someone can suggest me a better solution.Below is my code.
InitializeComponent();
Button playBtn = new Button();
playBtn.Width = 60;
playBtn.Height = 30;
Image playIcon = new Image();
playIcon.Source = new BitmapImage(new Uri(#"PATH"));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
grid1.Children.Add(playBtn);
grid1.Children.Add(playIcon);
Grid.SetColumn(playBtn, 0);
Grid.SetRow(playBtn, 0);
Grid.SetColumn(playIcon, 0);
Grid.SetColumn(playIcon, 0);
thanks for everyone input, after digging more into it, it sort of work out. What I did is add a Grid to Button.Content then add the image to the Grid. And using Opacity to add the grey out effect for IsEnable false state. Below I post my code, hope someone find it useful or improve on:
Button playBtn = new Button();
Image playIcon = new Image();
public MainWindow()
{
InitializeComponent();
Grid grid2 = new Grid();
RowDefinition grid2_row1 = new RowDefinition();
ColumnDefinition grid2_col1 = new ColumnDefinition();
grid2.RowDefinitions.Add(grid2_row1);
grid2.ColumnDefinitions.Add(grid2_col1);
playBtn.Width = 60;
playBtn.Height = 30;
playBtn.Click += playBtn_Click;
playIcon.Source = new BitmapImage(new Uri(#"pack://PATH..."));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
playBtn.Content = grid2;
grid2.Children.Add(playIcon);
grid1.Children.Add(playBtn);
Grid.SetRow(playIcon, 0);
Grid.SetColumn(playIcon, 0);
}
public void playBtn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
playBtn.IsEnabled = false;
playIcon.Opacity = 0.3;
}
Buttons in WPF have different states = "normal", "mouse over" and "pressed" are three.
When you create the button you are setting up it's "normal" state. You also need to set the "mouse over" state to have the same image as well.
Check the below links
http://blogs.msdn.com/b/knom/archive/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton.aspx
http://www.hardcodet.net/2009/01/create-wpf-image-button-through-attached-properties
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/
I have a child panel in a form which contains some text boxes and buttons. I tried setting tabstop and tabindex properties for these controls so that the user can tab from one control to the next. But for some reason the tabbing does not work, the curor stays on the same field which has the focus when I press the tab key. I am using C# with .Net 3.5 framework. Below is how my code looks like -
rightPanel.Controls.Clear();
marketMessageLabel = new Label();
marketMessageLabel.Location = new Point(0, 20);
marketMessageLabel.AutoSize = false;
marketMessageLabel.Size = new Size(rightPanel.Width, 42);
marketMessageLabel.BackColor = Color.White;
marketMessageLabel.Font = new System.Drawing.Font("Verdana", 8.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
rightPanel.Controls.Add(marketMessageLabel);
signinUserNameLabel = new Label();
signinUserNameLabel.Location = new Point(0, 150);
signinUserNameLabel.Size = new Size(60, 14);
signinUserNameLabel.BackColor = Color.White;
signinUserNameLabel.Text = "User Name";
signinUserNameLabel.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
rightPanel.Controls.Add(signinUserNameLabel);
signinUserNameTextBox = new TextBox();
signinUserNameTextBox.Location = new Point(0, 170);
signinUserNameTextBox.Width = this.Width - 80;
signinUserNameTextBox.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
signinUserNameTextBox.TabIndex = 0;
signinUserNameTextBox.TabStop = true;
rightPanel.Controls.Add(signinUserNameTextBox);
signinPasswordLabel = new Label();
signinPasswordLabel.Location = new Point(0, 192);
signinPasswordLabel.Size = new Size(100, 14);
signinPasswordLabel.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
signinPasswordLabel.BackColor = Color.White;
signinPasswordLabel.Text = "Password";
rightPanel.Controls.Add(signinPasswordLabel);
signinPasswordTextBox = new TextBox();
signinPasswordTextBox.Location = new Point(0, 210);
signinPasswordTextBox.Width = this.Width - 80;
signinPasswordTextBox.PasswordChar = '*';
signinPasswordTextBox.TabIndex = 1;
signinPasswordTextBox.TabStop = true;
rightPanel.Controls.Add(signinPasswordTextBox);
signInButton = new Button();
signInButton.Text = "Sign In";
signInButton.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
signInButton.Width = 70;
signInButton.BackColor = Color.White;
signInButton.Location = new Point(0,240);
signInButton.Click += new EventHandler(signInButton_Click);
signInButton.TabIndex = 2;
signInButton.TabStop = true;
rightPanel.Controls.Add(signInButton);
Another possible problem is if the form where the "tabbing" does not work is on a form that is not displayed modally.
For some reasons, "tabbing" sometimes does not work if a child form is displayed with .show, and you'd rather display the form with .ShowDialog.
If the form is modeless (displayed with .Show()), then you need to add the following code to handle the keyDown event:
private void YourForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
if (e.Modifiers == Keys.Shift)
this.ProcessTabKey(false);
else
this.ProcessTabKey(true);
}
}
You also need to set the KeyPreview property to True.
The solution is setting TabStop = true on the panel.
I just ran a little test, and it seems that winforms won't tab into the child panel if there are no other focusable controls outside of the panel.
You won't actually end up tabbing "onto" the panel, but it gets you around this issue you're seeing and it will tab to it's first child control.
Make sure you set the tabindex for the labels also, despite it is not focusable.
From VS designer window, with your form on the screen in design more, click on
View Menu
Tab Order menu option
point and click to set the sequential order of the controls (including labels).
Hope this helps,
Best regards,
Tom.