Good day all.
I have an issue with using the ListView control in C#:
I can create an Object of it and at least from what the debugger says (nothing) I can also add items and columns and all. However:
I cannot see the ListView at all.
Below the code with which I want to draw it onto an otherwise emtpy Form:
private ListView auftraegeView;
And then inside the constructor, after InitializeComponent:
auftraegeView = new ListView();
auftraegeView.View = View.Details;
auftraegeView.Width = this.Width - 12;
auftraegeView.Height = this.Height - 20;
auftraegeView.Left = 6;
auftraegeView.Top = 14;
I tried .Show() and .Refresh() methods, but to no avail.
The problem is that you are not adding your control to your form. You have created the object but now you need to show it on the form. For that you have to add it to the list of controls that need to be shown on the forms.
Some thing like
yourFormName.Controls.Add(auftraegeView );
Have tyou tried BringToFront and checked that the prorperties Visible = true?
i Now is trivial but..
Related
Im new in Wpf C# programming and have maybe a stupid question.
I have a form and I need to create some controls with dynamic names.
(For Ex.: Grid: 'main' Controls: "str"+(int)i)
And I need to set Property Margin of this Controls OnTick.
So, Ik how to add this Controls, but have some problems in changing their properties.
Some code:
Image img = new Image();
img.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/img.png"));
img.Name = "str_" + i;
img.Margin = new Thickness(-10,-10,0,0);
img.Width = 1;
img.Height = 2;
main.Children.Add(img);
// ToDo Something like this:
main["str_"+i].Margin = new Thickness(x,y,0,0);
So, the question is: How to Edit property of already Created Dynamic control?
Update.
I found it rather stupid to create tons of controls except of refreshing drawings.
Now I have no idea what is the best way of drawing bitmaps on form.
For example:
I have List of locations of bitmaps.
I need to update the
bitmap's locations 'x' times per second.
Look at the next solution. Here is the code that references to the properties of an existing controls by it's (control's) name. It changes the control's set of properties according to the set of properties you support in view model (or code behind). If you will be interested I can make the adaptation to achieve your set of requirements. Here is the link
WPF user control, access dependency properties of component elements .
regards,
Let's say that I have a panel with like... 3 controls in it. I may end up adding more controls to it or changing the positioning within that panel. When the program starts, I will programmatically HIDE the control. Eventually, the user can click a button that will create a duplicate of the original panel to populate an area on the form. The button should have the option for another click eventually, meaning that multiple instances of these can come about to populate this area. Remember that these controls may have text labels within them that can be individually set or altered later on, programmatically. I am assuming that to do this program, I need to make a List of controls, maybe a List of panels? I'm not exactly sure how to do this considering the fact that I need multiple controls duplicated multiple times.
Is there a nice, simple way to do this? I really don't want to do the duplication with any kind of 3rd-party package.
You will have to do it in code and therefore it'll be as nice as you can code ;-)
Seriously the course most often taken is to
create a UserControl which is a class related to a form, with the layout you want..
..and add more and more instances of it..
..often to a FlowLayoutPanel, often with AutoScroll
This is pretty nice and simple imo.
Here is a short walk-though..:
first we start, as usual, by picking a nice name for the UserObject class, maybe 'DataPanel' or 'BookItem'..
Next we create it: Go to the project explorer and right-click, choosing Add-New UserControl and give it the class name you chose. I'll use 'BookItem'.
Now you can see the Designer showing you a small empty control.
Look closer: You can also see that in the project explorer ther is now not only the new 'BookItem.cs' file but also the complementary 'BookItem.Designer.cs' and even a 'BookItem.resx' file; so this works very much like creating a new Form..
Let's add a few controls from the toolbox, I chose to add a PictureBox, four Labels and a NumericUpDown.
Have a look at the BookItem.Designer.cs file: Here you can see the very things you see in a Form.Desginer.cs file: All settings and all declarations for all controls you add to the layout. Note especially the declarations (at the bottom of the file): Just like for a Form, all controls by default are declared as private!
We can now work on the layout and script the controls. We also can add functions and properties to the UC, just like a Form.
Please note: Anything you need to access from outside, read from your form or its methods must be public! So if you want to access the NUpDown, let call it 'nud_quantity' you have a choice
You can change its declaration in the BookItem.Designer.cs from private to public or in the Designer by changing the Modifiers property
Or you can write a public function in the UC to get/set its value
Chosing between those two ways is a matter of taste; if other developers will work with the UC class, it will probably be better to put close control over what you expose by writing access methods.
After you have compiled the project you can see the new UC in the Toolbox.
You can now either add it from the Toolbox or
you can add it in code like any control you create dynamically.
Let's look at an example:
Imagine a simple order system in a bookstore: The customer has done a search on the books in our store and is presented with a list of books in a DataGridView 'dgv_bookList', readonly, multiselect. To the right there is a FlowLayoutPanel 'flp_cart' represeting a shopping cart. And we have a command button 'cb_addItems' to add selected books to the cart.
The Button might be scripted like this:
private void cb_addItems_Click(object sender, EventArgs e)
{
if (dgv_bookList.SelectedRows.Count <= 0) return;
foreach (DataGridViewRow row in dgv_bookList.SelectedRows)
{
BookItem book = new BookItem (row);
book.label1.Text = "#00" + book.label1.Text;
book.Name = book.label1.Text;
flp_cart.Controls.Add(book);
}
}
This will add one BookItem for each selected row in the DGV.
A few things to note on the above code:
I pass a DataGridViewRow into the constructor of the UC so it can directly set its labels! This means that, in addition to the parameterless contructor the desginer has built for us, we need to write a second contructor, maybe like this:
public bookItem()
{
InitializeComponent();
}
public bookItem(DataGridViewRow bookData)
{
InitializeComponent();
label1.Text = bookData.Cells[0].FormattedValue.ToString();
label2.Text = bookData.Cells[1].FormattedValue.ToString();
label3.Text = bookData.Cells[2].FormattedValue.ToString();
label4.Text = bookData.Cells[3].FormattedValue.ToString();
}
Instead you could write a public setData(DataGridViewRow bookData) function.
Also note how stupid my labels are named! You can do better than that, I hope!
Also note how I access 'label1' and modify its Text from a Button in the Form; to do that I had to change its declaration in the Desginer.cs file:
private System.Windows.Forms.PictureBox pb_cover;
public System.Windows.Forms.Label label1; // <<----expose this label !
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.NumericUpDown numericUpDown1;
Often preferrable: An access function, maybe like this:
public int quantity() { return (int) numericUpDown1.Value; }
Or, of course a Property:
public int quantity { get { return (int)numericUpDown1.Value; } }
Also note, that I set the Name of the BookData item to some variant of the 1st data item, my book id. This might as well, or better, happen in the constructor; and there should be a check to prevent adding the same item twice..
All in all one can say, that using UserControls is very much like working with Forms, including all the usual ways or tricks for inter-form communication: keep references, expose members, create properties and functions..
One final Note: Like with forms or subclassed controls there is one catch: By placing them in the designer, you assign the designer the responsiblity to display your UC during design time.
This is normally just fine; however it is also possible to introduce subtle mistakes which make it impossible for the designer to display the control. You need to correct these problems before the designer will be able to show a control or any form that contains it. Let have a look at a simple example of such a problem:
Let's script the Paint event of the PictureBox 'pb_cover' in the UC:
public Brush myBrush = null;
private void pb_cover_Paint(object sender, PaintEventArgs e)
{
if (pb_cover.Image == null)
{
Size s = pb_cover.ClientSize;
e.Graphics.FillRectangle(myBrush, 0, 0, s.Width, s.Height);
e.Graphics.DrawLine(Pens.Red, 0, 0, s.Width, s.Height);
e.Graphics.DrawLine(Pens.Red, s.Height, 0, 0, s.Width);
}
}
And let's modify the code in the Add button:
BookItem book = new BookItem (row);
book.label1.Text = "#00" + book.label1.Text;
book.myBrush = Brushes.OliveDrab;
flp_cart.Controls.Add(book);
Now, if you run the program all will be fine. Even if you try to look at the UC in the designer there may or may not be problems. But once you try to open a Form on which the UC was placed, the Desginer will crash and tell you that it can't work, since the Brush is null. Here the remedy is simple: add a default value to the Brush declaration and all is well. Other situations may need a little more thinking..
I don't even run into the problem btw, since I have not placed an instance of BookItem on the Form; they are only created in the Add Button..
I hope that gets you started!
So, in a new form I create programmatically some labels and linklabels.
The number is NOT known by the start of the program. And I need to display them in a container or something, and when they are too many, the container should display a vertical scrollbar..
My Form is must have a fixed size
I tried Panel, LinkLabel but no success, meaning it doesn't show a scrollbar and some of the controls don't show up
Do you have any ideas?
(I want to make something like a table of contents, but which doesn't have a fixed number of items)
Thank you...
Some code:
for(int i=0; i<number ;i++)
{
Label l=new Label();
l.Name = i + ".label";
this.Controls.Add(l); // need to replace "this" with name of a control
l.Location(50, i * 20 + 50);
}
I solved this by using a Panel, and setting its AutoScroll property to TRUE. Now it works.
i am using .NET CF making application in window mobile.
need want to add textboc in third column of listview.
googled it but only getting solution for web application.
aint it possible in window mobile ????
code i tried so far... (although it is not working :-) )
for (int i = 0; i < soups.Length; i++)
{
ListViewItem li = new ListViewItem();
li.Text = "RSO" + (i+1);
li.SubItems.Add(arrval[i]);
//in 1st attemp i tried
li.SubItems.Add(new TextBox());
//in 2nd attemped
TextBox tbox = new TextBox();
li.SubItems.Add(tbox);
li.SubItems.Add(Convert.ToString(5 * (i + 1)));
li.SubItems.Add(Convert.ToString(35 * (i + 1)));
lst_option.Items.Add(li);
}
but both ways not working.... bcz of obvious reasons as i am trying to add obj in add() method instead of string :) :)
thnkx in advance
None of the out-of-the-box controls for the Compact Framework (ListView, ListBox, DataGrid, etc) provide this capability. The common work-around is to place a separate TextBox on the form and when a ListViewItem is selected, move the Textbox to cover the location of the subitem/cell, put the subitem/cell text into the TextBox, then make it visible.
I have a c# windows form application that has a similar GUI functionality as MSN. It works in the way such that only a notification window appears if there is notification which in this case I have put several buttons and other stuffs in a single panel. (is this the right way to do so?)
How do I code it such that I can use a arrayList to add similar panels to the list and use a for loop to call it out. Example would be calling 2 or 3 similar panels through the use of arraylist(?) and for them to appear below one another. (Maybe like how MSN notifications window comes up one above another.)
the code for the panel is
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.lblImage);
this.panel1.Controls.Add(this.lblName);
this.panel1.Controls.Add(this.lblLinkName);
this.panel1.Controls.Add(this.lblLinkLocation);
this.panel1.Controls.Add(this.lblLocation);
this.panel1.Location = new System.Drawing.Point(13, 134);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(506, 100);
this.panel1.TabIndex = 17;
do I have to code the for loop in the designer file or the coding file? as after I have tried to add for loop in the designer code file, the designer view sort of unable to display my UI.
I'm guessing this is what you're looking for
for(int i = 0; i < panels.length; i++){
AddPanel(panels[i], i);
}
AddPanel(System.Drawing.Point point, int tabIndex){
Panel panel = new Panel();
this.Add(panel);
panel.Controls.Add(new Button());
panel.Controls.Add(new Label("Image"));
panel.Controls.Add(new Label("Name"));
panel.Controls.Add(new Label("linkName"));
panel.Controls.Add(new Label("linkLocation"));
panel.Controls.Add(new Label("location"));
panel.Location = point;
panel.Name = "panel" + i.ToString();
panel.Size = new System.Drawing.Size(506, 100);
panel.TabIndex = tabIndex;
}
You'll need to populate the panels array with a the points you'd like your panels to be added at.
I would create a Custom Control that has the interface you want for each item. Have the Control expose properties, methods, and events that allow you to access the child controls in a constant manner.
You can create multiple instances of the custom control and add them to a List and attach event handlers to them, etc...
If you use a flow layout panel as the parent and add each instance of the custom control to it, it will automatically handle the layout for you without you having to manually position them. If the flow layout is inside a container with autoscroll set, you will have a nice scrolling list of whatever kind of Items you can dream up.
adding example
With a custom control called ListItem.
ListItem item = new ListItem();
someFlowPanel.controls.add(item);
You should probably set item's width to the width of the flow panel you add it to, and set it to anchor left and right.