Arrange controls in panel in ASP.NET - c#

I want to arrange the controls in a Panel control. For example, I have four labels and four textboxs, I want to put them in a panel like a table without using the VS designer, only use the code. Does anyone do it before?
Best Regards,

C#, and use styles to control layout.
Panel pnl = new Panel();
Label lbl1 = new Label();
lbl1.Text = "1";
pnl.Controls.Add(lbl1);
TextBox tb1 = new TextBox();
pnl.Controls.Add(tb1);
Page.Controls.Add(pnl);
label
{
display: inline;
}

You could essential do the same thing Visual Studio does on the back end. Create a new control and set the properties, such as: size, name, text, and location.

I think that you can simply create a custom control that contains a label and a text box. Let's call this control LabeledTextBox. And then on your code simply add 4 instances of LabeledTextBox one after the other. This should provide the look and feel you want.

Related

UserControl inside Panel inside UserControl not working

I am having some trouble creating the template of my application :
I am making some dynamic parts of forms and I use a panel in which I include my User Control according to the choice of the user. The panel has the same size as the user control but it doesn't work at runtime. The panel in which the UC is included is inside another UC also.
I tried autoSize = true, fixed size and many things but I don't know why it goes like this. Any idea?
Some parts of my code to load the User Control :
UC_panel.Controls.Clear();
UC_ADDRESS uca = new UC_ADDRESS();
uca.Dock = DockStyle.Fill;
uca.AutoSize = true;
UC_panel.Controls.Add(uca);
My UserControl for the address
At runtime it goes bigger
Since you set
uca.Dock = DockStyle.Fill;
and also
uca.AutoSize = true;
(which is redundant)
it is possible that some other container has its AutoScaleMode set to Font and its font size is bigger.

How do I add a textblock dynamically to a page?

I'm just learning how to make universal apps for windows 10. Most of the tutorials show you how to work with XAML and how to assign functions to elements when you click on them but I couldn't find a way to make a new control appear when I click a button.
I'm making a note taking application. I've designed most of the stuff that I need. Now I want whenever I click a button to create a new textblock where the user can write their note.
//Create a new note when clicking the add button
private void newNoteBtn_Click(object sender, RoutedEventArgs e)
{
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
}
This is the code that runs when the button is clicked. When I run the application nothing happens. I think I have to put the new textbox in some kind of Grid or something.
Most of the tutorials are really old or mention windows forms and use some sort of this.Controls.Add(newNote); but Visual studio doesn't give me the Controls.Add option. I've also created a <Grid x:Name="notes"></Grid> which I thought I could use as a placeholder for the notes that are being created but I can't access the Grid element through the code.
Container Controls like Grid have Children property so you should use Childern like this:
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);
When defining
<Grid x:Name="notes"></Grid>
in XAML on the page, you be able to use notes as the identifier to access this Grid from the page's code behind:
notes.Children.Add(newNote);

windows forms can't add more components

I want to build a form that has 100 label and 100 text box
what I did is:
add new form
add panel to that form using drag and drop
change the dock property of that panel to fill
change the AutoScroll property to True
start adding the labels and text boxes using drag and drop
The problem
I added like 40 labels and text boxes but I can't add any more because I can't expand the form nor the label vertically.
Note
I can minimize the size of the panel and a vertical scroll bar appears. (maybe this information helps you to help me).
A data entry window with that many text boxes is going to require scrolling. So set the Panel's AutoScrollMinSize property to, say, (1000, 1000) as a first guess. You'll see the scrollbars appear. They work at design time as well, allowing you to scroll the panel and place the controls. High odds you should be using a DataGridView btw.
Something that needs to be said: the odds that you can get a human to enter 100 data items without any mistake are very close to zero. A very frustrating job for the hapless user, it will take him 10 or more minutes only to arrive at failure. Create a user friendly UI, one that partitions the data entry job in small steps that can be successfully completed. Automatically solves this problem as well.
Set parent form's properties AutoSize and AutoScroll to true. Then disable docking for your panel. This way you can set any size to panel and scroll form contents to add new controls. When panel design is done, set docking to Fill again.
Or you can set position for newly added controls using Properties panel. This will move controls to appropriate position on the panel.
This is a sample method I've used to add an unknown number of controls to a form. The trick is a FlowLayoutPanel.
As has been said before: you don't want 100 manually added controls on your page.
private void AddMappingControls() {
HeaderFlowLayoutPanel.Controls.Clear();
MappingFlowLayoutPanel.Controls.Clear();
Label sourceHeaderLabel = new Label();
sourceHeaderLabel.Text = "Velden in Excel (bron)";
sourceHeaderLabel.Name = "BronLabel";
sourceHeaderLabel.Width = MappingFlowLayoutPanel.Width / 2 - 20;
HeaderFlowLayoutPanel.Controls.Add(sourceHeaderLabel);
Label destinationHeaderLabel = new Label();
destinationHeaderLabel.Text = "Velden in Word sjabloon (bestemming)";
destinationHeaderLabel.Name = "BestemmingLabel";
destinationHeaderLabel.Width = MappingFlowLayoutPanel.Width / 2 - 20;
HeaderFlowLayoutPanel.Controls.Add(destinationHeaderLabel);
foreach (string destination in this.destinationFields) {
ComboBox sourceFieldComboBox = new ComboBox();
sourceFieldComboBox.BindingContext = new System.Windows.Forms.BindingContext();
sourceFieldComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
//sourceFieldComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
sourceFieldComboBox.Name = destination + "ComboBox";
sourceFieldComboBox.ValueMember = destination;
sourceFieldComboBox.DataSource = this.sourceFields;
sourceFieldComboBox.Width = MappingFlowLayoutPanel.Width / 2 - 20;
MappingFlowLayoutPanel.Controls.Add(sourceFieldComboBox);
Label nameLabel = new Label();
nameLabel.Text = destination;
nameLabel.Name = destination + "Label";
nameLabel.Width = MappingFlowLayoutPanel.Width / 2 - 20;
MappingFlowLayoutPanel.Controls.Add(nameLabel);
}
}
I meant exactly the same as MeanGreen but he was first. I have created sample solution: https://www.amazon.com/clouddrive/share?s=i9N7raPPQPEjOdHPRn99uE

Is it possible to add more than one panel in same location

i'm using telerik control.
So i want to ask,
In winforms application ,Is it possible to add more than one panel in same location and display one at a time just like show/hide property.
Make sure you have placed all panel control in same container or form. then you can use Visible property to show and hide panel. BringFront and SendToBack function will be used to bring panel on top or send it to back. If you have placed any panel in another panel then that will be disappeared when you Hide parent panel. So, Make sure all panels' parent control must be same. To determine the parent control simply select that panel and press escape key to select their parent.
private void LoadPanels()
{
panel1.Location = new Point(10,10);
panel2.Location = new Point(10,10);
panel3.Location = new Point(10,10);
panel4.Location = new Point(10,10);
panel5.Location = new Point(10,10);
VisiblePanel("panel1");
}
private void VisiblePanel(string panelName)
{
string[] panels = new string[]{"panel1","panel2","panel3","panel4","panel5"};
for (int i=0;i<panels.Length;i++)
this.Controls[panels[i]].Visible = (panels[i] == panelName);
this.Controls[panelName].BringToFront(); //Not required you can remove this line.
}
Here's a slightly different approach you might want to consider...
Are you wanting to be able to programmatically select the contents of a rectangular area at runtime, selecting among various controls to display? If so, you could use a custom TabControl which has its tabs (not the pages) hidden.
Then you can select which page is displayed by programmatically changing its SelectedIndex property at runtime.
Doing it like this means that your form editor will show a normal tab control, which allows you to much more easily add the content to each page - but at runtime the tabs will be hidden from the user; they will just see the contents of the currently selected page.
See Hans Passant's answer here for how to create such a custom tab control.
(However, you might also want to override the OnKeyDown for the custom tab control in order to ignore Ctrl-Tab.)

control to show steps to do something in winforms

i want to show steps on how to cook something in winform c# .net as steps. Something like a set of text area would be nice but:
-> list box considers the whole string of one step as one item so user needs to scroll horizontally to view the whole step.
-> datagridview is also not suitable as i want the text to word wrapped.
i also want the user to be able to edit the step.
any suggestions of custom control would be nice.
Maybe a wizard like app would be suitable for you. AFAIK there's no native wizard control in C# but you could implement one using tabs or using one of many in the web.
A multi line text box will do the job great. just take a simple text box and do the following to it, and it will turn to a text area:
TextBox listBoxNewInput = new TextBox();
//Initialize label's property
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Width = 315;
listBoxNewInput.Height = 150;
listBoxNewInput.DoubleClick += new EventHandler(listBoxNewInput_DoubleClick);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(list
BoxNewInput);

Categories