Add simple text to panel control - c#

I have a panel in C#:
Panel aspPanel = new Panel();
Button aspbutton = new Button();
aspbutton.Text = "Download PDF";
aspbutton.Click += initDownload;
aspPanel.Controls.Add(aspbutton);`
I have added Attributes and buttons and all kinds of cool dynamic stuff. But I want to just add simple text and so far am unsuccessful.
I'm looking for how to add text behind the button. In the end the HTML code would render something like:
<input type="button"/> Hello, this is a button, please click
Can someone point me in the right direction?

To add Literal text after the button:
Panel aspPanel = new Panel();
Button aspbutton = new Button();
aspbutton.Text = "Download PDF";
aspbutton.Click += initDownload;
aspPanel.Controls.Add(aspbutton);
aspPanel.Controls.Add(new LiteralControl("some more text!"));

Try this
Label lbl = new Label();
lbl.Text = "Hello, this is a button, please click";
aspPanel.Controls.Add(lbl);

Related

is it possible to add elements like panel to a listbox, listview or other list with .items value using c# and winforms?

is it possible to add elements like panel to a listbox, listview or other list with .items?
I want to create panels with other elements like labels, checkbox, buttons,.. on a panel.
The panel should then be in a list so that I can check it for example: checkbox (which is on the panel) and is active in the list is showing, the other elements should be hidden in the list.
If a panel is between 2 panels, it should move upwards so that there is no space in between. If I then only display the panels of the non-activated elements, the hidden ones should be displayed again and the displayed ones should be hidden.
I would like to control this display using button event,
show everything,
only activated and
only deactivated.
With another button I would like to be able to bring a panel to the top position if it was declared as a favorite. if the favorite is removed again it should go back to where it was before.
In addition, I would then like to create a search mask that only displays the elements that match the search string when entered.
The only way I found is with listbox1.Controls.Add(panel1); for it to appear.
Unfortunately it doesn't work with listbox1.Items. :(
So I don't have a selectedItem either....
Here is my code that I have so far:
private void Reload_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
panel1.Size = new Size (250, 35);
panel1.BackColor = Color.Red;
panel1.ForeColor = Color.Green;
panelxy.Dock = DockStyle.Top;
Panel panel2 = new Panel();
panel2.Size = new Size(250, 35);
panel2.BackColor = Color.Blue;
panel2.ForeColor = Color.Green;
panel2.Dock = DockStyle.Top;
listBox1.Controls.Add(panel1);
listBox1.Controls.Add(panel2);
Button btn_1 = new Button();
btn_1 .Size = new Size(200, 30);
btn_1 .Location = new Point(5, 2);
btn_1 .ForeColor = Color.Blue;
btn_1 .BackColor = Color.Yellow;
btn_1 .Font = new Font("Sitka Text", 15F, (FontStyle)(FontStyle.Bold | FontStyle.Italic), GraphicsUnit.Point, (byte) 0);
btn_1 .Text = "testbutton";
panel1.Controls.Add(btn_1 );
}
And here a Picture to show that:
listbox_elements
I hope someone can help me here. :)
thanks and BR
Cusy
Super thank you!
I just found out that it is possible to realize the required with a TableLayoutPanel. :)
Since I already have about 7000 lines of code, I don't want to convert it to WPF. ;)
i think the Answer from "Jimi" with FlowLayoutPanel are also a Solution. :)
Thanks
BR Cusy

Calling asp function from div

I have creates a side list of some item which is generated dynamically.Using
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.Attributes.Add("id", "div"+(index));
div1.Attributes.Add("class", "teacher-data");
div1.Attributes.Add("onclick", "getHighlight(this)");
Image img = new Image();
img.Attributes.Add("class", "teacher-image");
HtmlGenericControl div2 = new HtmlGenericControl("div");
div2.Attributes.Add("class", "teacher-data-container");
Label lbl1 = new Label();
lbl1.Text = name + "<br/>";
lbl1.ID = "lbl_"+ (index++);
Label lbl2 = new Label();
lbl2.Text = degree;
Label lbl3 = new Label();
lbl1.Attributes.Add("class", "teacher-info");
lbl2.Attributes.Add("class", "teacher-info");
lbl3.Attributes.Add("class", "teacher-info");
div2.Controls.Add(lbl1);
div2.Controls.Add(lbl2);
div2.Controls.Add(lbl3);
div1.Controls.Add(img);
div1.Controls.Add(div2);
teacher_containr.Controls.Add(div1);
What I tried is that when some one attempt a click over that div tag it gets selected but here what i also want is it should call a asp.net function so that after clicking, relevant data comes over right side of it.But nothing is working.
Can we add some event or anything in div so that it calls asp.net function. I tried ajax method but it did not work.

How to create a pop up window when a button is clicked

This is the code I have for the button. I want the configbutton to display a pop up window when clicked on. I have looked up and tried a lot of different codes but none of them work. I feel like I am missing a key component but I am not sure what that is. Appreciate the help!
tr = new TableRow();
// Create the cell
tc = new TableCell();
tc.Width = Unit.Point(300);
tc.BorderWidth = 0;
tc.BorderStyle = BorderStyle.None;
Button ConfigButton = new Button();
ConfigButton.Text = "Configuration";
ConfigButton.Visible = true;
tc.Controls.Add(ConfigButton);
tr.Cells.Add(tc);
tbl.Controls.Add(tr);
Using JavaScript Along with ASP.NET you will do the following:
// when you create the button, you can add attributes
Button ConfigButton = new Button();
// this example will display alert dialog box
ConfigButton.Attributes.Add("onclick", "javascript:alert('ALERT ALERT!!!')");
// to get you popup windows
// you would use window.open with the BLANK target
ConfigButton.Text = "Configuration";
ConfigButton.Visible = true;
I recommend looking into using the AJAX Control Toolkit ModalPopupExtender. This is what I use for my pop ups in ASP.NET.
Here is a link to he AJAX Control Toolkit samples website showing how this control works:
http://www.ajaxcontroltoolkit.com/ModalPopup/ModalPopup.aspx

How do I align controls in FlowLayoutPanel properly

I am currently trying to play around with c#. I want to create a simple GUI containing (as minimal example) three labels. The first two should be aligned from top to buttom and the third one to the right of these two button.
I have this in a panel, like
Label p_LabelX = new Label();
p_LabelX.Text = "I am to the right";
FlowLayoutPanel p_ButtonLayout = new FlowLayoutPanel();
p_ButtonLayout.Width = 200;
Label p_Label1 = new Label();
p_Label1.Text = "I am upper left";
Label p_Label2 = new Label();
p_Label2.Text = "I am lower left";
p_ButtonLayout.Controls.Add(p_Label1);
p_ButtonLayout.Controls.Add(p_Label2);
FlowLayoutPanel p_MainLayout = new FlowLayoutPanel();
p_MainLayout.FlowDirection = FlowDirection.LeftToRight;
p_MainLayout.WrapContents = false;
p_MainLayout.AutoScroll = true;
p_MainLayout.Controls.Add(p_ButtonLayout);
p_MainLayout.Controls.Add(p_LabelX);
this.Controls.Add(p_MainLayout);
With this code, it kind of works, but I have some scrollbars and the mainlayout is not fullscreen within the panel. However, if I skip the AutoScroll line, the scrollbars are gone and the layout seems to be fullscreen, but the right button is not visible.
Any suggestions?

Close dynamically created form with dynamic button

I'm trying to close a dynamically created form with a dynamic button (this is the simplest of my jobs, I am also adding other buttons to do other jobs but I figured this is a good place to start).
As of now I can create the form, button and the click event for that button, but I don't know what to add within the click event function to close the host of that button. I am guessing I can somehow access the buttons parent through the click function? Or maybe pass the form control as an argument in the function? Any help is appreciated!
//Create form
Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;
//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += new EventHandler(saveButton_buttonClick);
//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();
And my click event function looks pretty much normal:
void saveButton_buttonClick(object sender, EventArgs e)
{
}
Unfortunately I don't know what to add for the function to work! Thanks in advance for any help someone can give me! I feel like this should be a straight-forward problem to solve but I haven't been able to figure it out...
While it's certainly possible to do this with a named function, it's generally simpler to just use an anonymous function in cases like this:
Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;
//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += (_,args)=>
{
SaveSnapshot();
};
//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
exitButton.Click += (_,args)=>
{
snapshot.Close();
};
//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();
A simple way is to use a lambda method:
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Click += (s, e) => { shapshot.Close(); };

Categories