Form control selection - c#

How to change order of control selection in C# Window form. I wanted to change where text box appears in the form, but now when I use Tab key it skips that text box an later goes to it. I've worked with WPF and I would just change control position in XAML, but I can't do it in Win form.

Here is a simple example that adds a button and sets its TabIndex property
// Create a button and add it to the form.
Button button1 = new Button();
// Anchor the button to the bottom right corner of the form
button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
// Assign a background image.
button1.BackgroundImage = imageList1.Images[0];
// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;
// Make the button the same size as the image.
button1.Size = button1.BackgroundImage.Size;
// Set the button's TabIndex and TabStop properties.
button1.TabIndex = 1;
button1.TabStop = true;
// Add a delegate to handle the Click event.
button1.Click += new System.EventHandler(this.button1_Click);
// Add the button to the form.
this.Controls.Add(button1);
Alternatively you can also take a look at this (To set from Design)
Hope it helps

Related

How can I resize a TreeView during runtime in a Control

I am trying to resize my TreeView window during runtime and I cant do it.
in my program I can press on a button and the TreeView opens as a popup window.
The TreeView is inside a Control:
private Control parent;
mytree= new TreeView();
parent.Controls.Add(mytree);
I have already tried to search for any resize properties but no luck, I can't find a way that the tree will be resizable during runtime.
The only way I can see it is to delete the control and to put it in a Form and then I can make it look like the same but I still want to know if there is another way to solve this, please if someone knows !
thank you
Assign anchor property to tree view so it resizes with form (or maybe control he is child of)
Here is example of anchoring button:
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
// Create a button and add it to the form.
Button button1 = new Button();
// Anchor the button to the bottom right corner of the form
button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
// Assign a background image.
button1.BackgroundImage = imageList1.Images[0];
// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;
// Make the button the same size as the image.
button1.Size = button1.BackgroundImage.Size;
// Set the button's TabIndex and TabStop properties.
button1.TabIndex = 1;
button1.TabStop = true;
// Add a delegate to handle the Click event.
button1.Click += new System.EventHandler(this.button1_Click);
// Add the button to the form.
this.Controls.Add(button1);
}
Important part is button1.Anchor where in example button is anchored to bottom right of window, so it will just follow window as you resize, but you can anchor to all sides of window and it will resize with it.
Try different things and you will figure it out.
SOURCE: MSDN

C# WPF Create function that creats new inputs

I am using C# Wpf and i would like to create a function that once you click on a button A new Textbox will appear is this possible and I want to know if it possible to increase the form hight when a new Textbox is created
The question isnt exactly specific but yes you can make new UI elements in the code-behind. In WPF you would put your button on the window, then add the "Click" event to it. Then in the click event you could code something like. . .
private void Button_Click(object sender, RoutedEventArgs e)
{
Button newButton = new Button(); //Create a new button control and assign it to newButton
newButton.Width = 35; //Access fields like this (You can access any of the ones you access from the Xaml user interface)
newButton.Height = 20;
newButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
newButton.VerticalAlignment = System.Windows.VerticalAlignment.Top;
newButton.Content = "click me!";
grMain.Children.Add(newButton); //Add it to the grid
}
grMain is the name of my grid, make sure you name it and call it by the name. You can also call other elements, such as a stack panel or whatever you need. So for your case just make it a textbox instead of a button
Oops didnt see the second part of your question! To resize the window you need to first add a name to it in the XAML (Like you would name a grid or button - just name the window something). And just as we called grMain in my example call that and change the height/width properties.

WPF Button is static and does nothing

I'm designing a WPF Application in C# and I'm creating a grid with its controls at runtime. All of my controls work fine (Image, Textblocks, Labels), but my Button does not. It just sits there. When I move my mouse over it, it does nothing. When I click it, (w/ a click event handler) it does absolutely nothing! I've tried using different event handlers such as (Click, MouseDown, MouseUp). I have no clue what the problem is. Here's my code for the button:
Button AccessBtn = new System.Windows.Controls.Button();
AccessBtn.Content = "Access:";
AccessBtn.Margin = new Thickness(397, 571, 472, 41);
AccessBtn.Name = "AccessButton";
AccessBtn.Focusable = true;
AccessBtn.Click += (s, EventArgs) =>{
MessageBox.Show("Nothing here yet!");
};
grid.Children.Add(AccessBtn);
When you add children to the grid. They are added to row 0 and column 0.
So they will all be stacked on to each other. The button is no longer visible for the mouse event. You should put the button on top of the items or use row and columns to arrange the items. (Alternatively you might want to check a Canvas to position your elements.)

C# delete 2 dynamic controls based only on the first controls clicked

I have some dynamically created PictureBox's and Label's for each PictureBox, and a ContextMenu with a ToolStripMenuItem named "Remove" ... I want to remove only the PictureBox and the Label that is related to that PictureBox that is clicked but this ContextMenu is attached to all PictureBox's only, and not to Label's.
Is there a way to delete two controls in one click based only on the first one clicked ?
How to do it ?
Store a reference to the associated Label in the Tag() property of the PictureBox when you create them:
Label lbl = new Label();
PictureBox pb = new PictureBox();
pb.Tag = lbl;
Later you can remove them both:
// ...assuming "pb" now refers to the PictureBox that fired the ContextMenu...
((Control)pb.Tag).Dispose();
pb.Dispose();
You could store the id of another control in the first control as its attribute. Now you could delete the second control using the attribute in the first control click event.

How to remove waiting cursor from textBox?

Whenever I load windows form with text box, I'm get waiting cursor blinking in first textbox, but I waiting to blink when I click on that textbox.
This is how textbox code is looking like:
this.textBox1.Location = new System.Drawing.Point(166, 51);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(122, 20);
this.textBox1.TabIndex = 3;
Could anyone please tell me how I make it possible?
You can set the focus to your first input field(textbox) by using TextBox1.Focus();
If you don't want any other textbox to be focused, you need to add an element that has Focus method implemented and use its Focus like a picture box. The easiest would be to add an invisible textbox and set the focus to it.
This is probably because the textbox is the first control to get focus.
Do you wish to have the focus on a different control, or not have the text box focussed?

Categories

Resources