I'm working on a project at the moment and I'm trying to have it where every contact within the ArrayList has their own tab page. Within the tab page I want all their info to be displayed, to do this I'm trying to create labels that will have their information. I'm right now just at the point trying to get at least one label to appear, but it does not appear to be displaying at all. My code is below. Any help?
int count = 0;
foreach (clsContactHandler contact in clsGlobal.mContacts)
{
string tabName = contact.FirstName + " " + contact.LastName;
Font font = new Font("Microsoft Sans Serif", 16.0f, FontStyle.Bold);
TabPage contactPage = new TabPage(tabName);
tabs.TabPages.Add(tabName);
Label label = new Label();
contactPage.Controls.Add(label);
label.Font = font;
label.AutoSize = true;
label.Location = new System.Drawing.Point(16, 7);
label.Name = "label" + count;
label.Size = new System.Drawing.Size(43, 13);
label.Text = "Name:";
count++;
}
Since you're creating the new page as TabPage contactPage object,
tabs.TabPages.Add(tabName);
should be
tabs.TabPages.Add(contactPage);
like suggested use
tabs.TabPages[tabName].Controls.Add(label);
but before that set name property:
contactPage.Name="someUniqueName"
and use
tabs.TabPages[count].Controls.Add(label);
where count is I suppose, the index tracker in case TabPages["someUniqueName"] throws NullReferenceException.
Also, add contactpage to tabs and not tabName
Related
I am trying to add a row in middle of table layout panel. However I am struggling to find a way to do this. I tried following from one of the article on internet but this doesn't seems to be working. Can you help.
i have 3 columns and 5 rows in table layout panel. each row containing lable, textbox and blank lable.
i am trying to add a row after 2nd row in already created table layout panel.
var AddOnControl = ConfirmationTable.Controls.Find("Discount", true).First();
int childIndex = 1+ ConfirmationTable.Controls.GetChildIndex(AddOnControl);
Label lbl = new Label();
lbl.Name = key;
lbl.Text = key;
lbl.Font = new Font("Calibri", 10F, System.Drawing.FontStyle.Regular);
lbl.Size = new Size(lbl.Size.Width + 70, lbl.Size.Height);
ConfirmationTable.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.Multiline = true;
txt.TextChanged += txt_TextChanged;
txt.Name = "txtPrice" + key;
txt.Text = value;
txt.BorderStyle = BorderStyle.None;
ConfirmationTable.Controls.Add(txt);
lbl = new Label();
lbl.Name = "blank";
ConfirmationTable.Controls.Add(lbl);
ConfirmationTable.Controls.SetChildIndex(lbl, childIndex);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 1);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 2);
but above code always adds a row at the bottom of the table. Any suggestion?
There is an overload of the Controls.Add method that accepts two integers as indexes for column/row position.
Like this:
tableLayoutPanel1.Controls.Add(lb3, 0, 1);
I believe this is enough to solve your problem in a simple way.
I am trying to make a text editor and I have 2 rich text boxes. I am uses the 1st rich text box as a number like and setting its Enabled property to false. Then the second text box is going next to it
I have currently set the dock of the first text box to left and the second one to fill. But the 2nd one keeps taking up the whole tabpage? And going slightly more left towards the number line and its hidden under there a little bit. Here is my create new document void I have... T2 is the number line and T is the default text box they will type into.
TabPage t = new TabPage("new " + getNumber());
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = false;
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();
The problem is that you define your fill-docked text box at the end of the control list... winforms likes the fill-docked elements first in the list.
Easily solved:
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
// Add this line
T.BringToFront();
Or you could also do T2.SendToBack(); after T is added to the controls collection.
Or you could simply create (and add to t.Controls), the fill-docked textbox first, and the left-docked textbox second.
Either way works
By the way, try to name your variables correctly. t, T, T2 are just not good names
Here you go:
t.Controls.Add(T2);
t.Controls.Add(T);
t.Controls.SetChildIndex(T, 1);
t.Controls.SetChildIndex(T2, 0);
t.PerformLayout(); // needed after SetChildIndex!
T2.Dock = DockStyle.Left;
T.Dock = DockStyle.Left;
If you want your boxes to grow with the TabPage here is one way to do it:
private void t_Resize(object sender, EventArgs e)
{
// assuming you want the two RTBs to fill the TabPage
// if you want something else, best add an anchored container panel
// and use its resize event instead
T2.Width = t.Width / 4;
T.Width = t.Width / 4;
}
And yes, t, T and T2 are really bad names!
There's a few things going on:
Fill does just that; it Fills the entirety of the PARENT control. Whether anything else is there or not.
Your other text box will be hidden until it is set to Enabled.
What you Might? be looking for is a way to have the text box size itself based on the size of the tab page being created:
{
TabPage t = new TabPage("new " + 1);
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = true ;
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T2.Dock = DockStyle.Right;
var AdjustedSize = T2.Size;
AdjustedSize.Width = t.Size.Width * 2 / 3;
T2.Size = AdjustedSize;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();
}
I have a Listview and it has one column. The view is set to List and I can see each Listviewitem but I can't select the item's row, I have to select the item's text. Is it possible to make it so that the Listviewitem's width is the same size as the Listview itself so that the user can click anywhere on the Listviewitem to select the item?
I tried searching but could only find how to change the column width and how to fix it in XAML, but this is for a WinForm.
Edit - As requested, this is the code that is generated by the Visual Studio designer. It is the only code involved with the ListviewItem.
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("2");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("3");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("4");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("5");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("6");
System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem("7");
this.listView1 = new System.Windows.Forms.ListView();
this.listView1.Activation = System.Windows.Forms.ItemActivation.OneClick;
this.listView1.AllowDrop = true;
this.listView1.AutoArrange = false;
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listView1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
listViewItem1.StateImageIndex = 0;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4,
listViewItem5,
listViewItem6,
listViewItem7});
this.listView1.Location = new System.Drawing.Point(105, 129);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(157, 475);
this.listView1.TabIndex = 4;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
A simple solution will be using a ListBox instead of List View
In case of simple text, it is better to use ListBox.
Update:
Simplest hack for getting this functionality in ListView will be adding dummy spaces at the end of each item string to fill.
Better ListView and Better ListView Express (free) support this. It behaves like a ListBox by default (Details view, no columns):
The item auto sizing can be triggered by setting AutoSizeItemsInDetailsView property to true:
betterListView.AutoSizeItemsInDetailsView = true;
Set listView1.FullRowSelect = true
When i add a radio button in visual c# the text name of the radio button is aligned perfectly to the right . if i create a radio button dynamically and give it several properties etc.. when i debug and view it, the text or name of the radio button is shifted slighted up and to the right of the radio button? i am looked over several properties including padding etc.. but i am not able to figure out how to correct this dynamically. what is going on and how can i fix this?
here is example of the properties i am using right now
radio_ip_addresses[i] = new RadioButton();
radio_ip_addresses[i].Name = "radio_" + i;
radio_ip_addresses[i].Text = ip_addresses.Dequeue();
radio_ip_addresses[i].Location = new Point(x, y);
radio_ip_addresses[i].Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
radio_ip_addresses[i].ForeColor = Color.White;
radio_ip_addresses[i].TextAlign = ContentAlignment.MiddleLeft;
radio_ip_addresses[i].Visible = true;
radio_ip_addresses[i].Parent = this;
Thanks Rotem, i took your suggestion to check the designer.cs i should have thought of that. it was autosize that was the key :), see below from what i found in the designer.cs.
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(192, 50);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 71;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
I wish to add a button for every line in a file to a panel.
My code so far is:
StreamReader menu = new StreamReader("menu.prefs");
int repetition = 0;
while(!menu.EndOfStream)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = menu.ReadLine();
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
MessageBox.Show(dynamicbutton.Location.ToString());
}
menu.Close();
The problem is that only the first control gets created.
The code looks fine but there could be a following situations.
1.You might have only one entry in the file, so you are experiencing only One Button added to the panel.
2.Your panel width is smaller than the sum of all the dynamic buttons width.
I suspect no 2 is the main reason that is causing problem.
So, I recommend that you use FlowLayoutPanel. To add a dynamic content as it automatically layout all the child controls.
Each time it is generating the same name for dynamic controls. That's the reason why it is showing only the last one. It simply overwrites the previous control each time.
int x = 4;
int y = 4;
foreach(PhysicianData pd in listPhysicians)
{
x = 4;
y = panPhysicians.Controls.Count * 30;
RadioButton rb = new RadioButton();
rb.CheckedChanged += new System.EventHandler(rbPhysician_CheckedChanged);
rb.Text = pd.name;
rb.Visible = true;
rb.Location = new Point(x, y);
rb.Height = 40;
rb.Width = 200;
rb.BackColor = SystemColors.Control;
rb.ForeColor = Color.Black;
rb.Font = new Font("Microsoft Sans Serif", 10);
rb.Show();
rb.Name = "rb" + panPhysicians.Controls.Count;
panPhysicians.Controls.Add(rb);
}
Try this code
StreamReader menu = new StreamReader("menu.prefs");
var str = menu.ReadToEnd();
var items = str.Split(new string[] {"\r\n" } , StringSplitOptions.RemoveEmptyEntries);
foreach (var item in items)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = item;
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
}
The problem with Panel and similar controls other than the FlowLayoutPanel is when you create a control and a second one, the second is created at the same position if you are not changing it's location dynamically or setting it according to the other already added controls. Your control is there, it's in the back of the first control.
A flowLayoutPanel is better as it will add the controls next to each other as you add them while compromising more finer control at their positioning.
I also have similar problems with panels. For what you are doing it could be useful to just add strings to a listbox rather than using labels and a panel. That should be simpler.