How Can I Implement the Grid-like Control Using C# 2010 Express - c#

I need a control like this:
This is from the Microsoft Word: Insert => Symbols.
This dialog has a grid-like control with a list of Unicode Characters;
You can select any character;
The selected character's Unicode is displayed;
User cannot edit the character;
Besides, I need to extend it's feature:
1. User can delete the cell with the selected character;
2. User can add a list of characters (from a file or whatever).
I'm asking what built-in controls I should use to implement this specific control.
Thanks.
Peter

I was able to create a simple mock up using the standard DataGridView control.
private void InitilizeDataGridView(DataGridView view)
{
var defaultCellStyle = new DataGridViewCellStyle();
defaultCellStyle.ForeColor = SystemColors.ControlText;
defaultCellStyle.WrapMode = DataGridViewTriState.False;
defaultCellStyle.SelectionBackColor = SystemColors.Highlight;
defaultCellStyle.BackColor = System.Drawing.SystemColors.Window;
defaultCellStyle.SelectionForeColor = SystemColors.HighlightText;
defaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
defaultCellStyle.Font = new Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((0)));
view.DefaultCellStyle = defaultCellStyle;
view.MultiSelect = false;
view.RowHeadersVisible = false;
view.AllowUserToAddRows = false;
view.ColumnHeadersVisible = false;
view.AllowUserToResizeRows = false;
view.AllowUserToDeleteRows = false;
view.AllowUserToOrderColumns = true;
view.AllowUserToResizeColumns = false;
view.BackgroundColor = SystemColors.Control;
for(var i = 0; i < 16; i++)
{
view.Columns.Add(new DataGridViewTextBoxColumn { AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, Resizable = DataGridViewTriState.False });
}
DataGridViewRow row = null;
for (int index = 32, cell = 0; index < 255; index++, cell++)
{
if(cell % 16 == 0)
{
if(row != null)
{
view.Rows.Add(row);
}
row = new DataGridViewRow { Height = 40 };
row.CreateCells(view);
cell = 0;
}
if (row != null)
{
row.Cells[cell].Value = Char.ConvertFromUtf32(index);
}
}
}

In WinForms, you can add fixed-size Label controls to a FlowLayoutPanel at runtime.
Note that this will not scale well; do not make thousands of Label controls.
If you want large numbers of characters, you can make a single screenfull of labels, then add a ScrollBar control and handle the Scroll event to change the label captions.

Related

Disabled checkboxes in flowLayoutPanel

I'm trying to create some checkboxes dynamically in the flowLayoutPanel control.
I have managed to create it but they seem to be disabled inside the panel. I can't check/uncheck them. They are grey and not active.
I suppose there is some flowLayoutPanel property that prevents these checkboxes to be enabled that I'm missing.
Here is my code for flowLayoutPanel:
//
// flowLayoutPanelForCheckBoxes
//
this.flowLayoutPanelForCheckBoxes.Controls.Add(this.SomeCheckBox);
this.flowLayoutPanelForCheckBoxes.AutoScroll = true;
this.flowLayoutPanelForCheckBoxes.Location = new System.Drawing.Point(132, 8);
this.flowLayoutPanelForCheckBoxes.Name = "flowLayoutPanelForCheckBoxes";
this.flowLayoutPanelForCheckBoxes.Size = new System.Drawing.Size(546, 38);
this.flowLayoutPanelForCheckBoxes.TabIndex = 28;
this.flowLayoutPanelForCheckBoxes.WrapContents = false;
For generating checkboxes:
private List<CheckBox> GetGeneratedCheckboxes()
{
var generatedCheckboxes = new List<CheckBox>();
var valuesForCheckboxes = GetCheckboxValuesFromDb(); //it returns dictionary<int, string>
// with some numbers and text. I think this method is not as important
if (valuesForCheckboxes != null && valuesForCheckboxes.Count != 0)
{
int index = 0;
foreach (var chbx in valuesForCheckboxes)
{
var checkboxToAdd = new System.Windows.Forms.CheckBox();
checkboxToAdd.AutoSize = true;
checkboxToAdd.Enabled = true;
checkboxToAdd.Checked = true;
checkboxToAdd.CheckState = System.Windows.Forms.CheckState.Checked;
checkboxToAdd.Size = new System.Drawing.Size(84, 21);
checkboxToAdd.UseVisualStyleBackColor = true;
checkboxToAdd.Name = "chboxCountry" + chbx.Key;
checkboxToAdd.Text = chbx.Value;
checkboxToAdd.TabIndex = index + 1;
index++;
generatedCheckboxes.Add(checkboxToAdd);
}
}
return generatedCheckboxes;
}
And it's used in the FormLoad method:
var checkboxesForPanel = GetGeneratedCheckboxes();
foreach (var checkbox in checkboxesForPanel)
{
this.flowLayoutPanelForCheckBoxes.Controls.Add(checkbox);
}
Code looks fine for me. It has no issue. Check if your flow layout panel is in disabled state some how. Check in designer or check your code if its getting in disabled state.

How to add radio buttons dynamically in Windows form?

I need to add radio buttons dynamically in my windows form and in horizontal mode.
for (int i = 0; i <= r.Count; i++)
{
RadioButton rdo = new RadioButton();
rdo.Name = "id";
rdo.Text = "Name";
rdo.ForeColor = Color.Red;
rdo.Location = new Point(5, 30 );
this.Controls.Add(rdo);
}
You could do something like this:
FlowLayoutPanel pnl = new FlowLayoutPanel();
pnl.Dock = DockStyle.Fill;
for (int i = 0; i < 4; i++)
{
pnl.Controls.Add(new RadioButton() { Text = "RadioButton" + i });
}
this.Controls.Add(pnl);
You could also add the FlowLayoutPanel in the designer and leave that part out in the code.
To get the selected RadioButton use a construct like this:
RadioButton rbSelected = pnl.Controls
.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
To use this the FlowLayoutPanel needs to be known in the calling method. So either add it to the Form in the designer (Thats what I would prefer) or create it as an instance member of the form and add it at runtime (this has no benefit).
You can do something like this
//This is my dynamic data list
List<ItemType> itemTypeData = new List<ItemType>()
RadioButton[] itemTypes = new RadioButton[ItemType.Count];
int locationX = 0;
for (int i = 0; i < ItemType.Count; i++)
{
var type = ItemType[i];
itemTypes[i] = new RadioButton
{
Name = type.Code,
Text = type.Code,
AutoSize = true,
Font = new System.Drawing.Font("Calibri", 11F, FontStyle.Regular),
Location = new Point(156 + locationX, 88),
};
this.Controls.Add(itemTypes[i]);
locationX += 80;
}
This works fine for me

which control is best for adding text and radio buttons in windows form application

I want to display the question and options, and for every option the radio button should be add, and also question number in should be in number circle. suggest me how can to do this one
public Form2(string paperid)
{
InitializeComponent();
if (paperid != "")
{
var papers = doc.Descendants("paper");
foreach (var paper in papers)
{
if (paper.Attribute("id").Value == paperid)
{
var questions = paper.Descendants("question");
foreach (var question in questions)
{
Label ques = new Label();
ques.Text = question.Attribute("ques").Value;
this.Controls.Add(ques);
var options = question.Descendants("option");
var i = 0;
foreach (var option in options)
{
RadioButton rdbtn = new RadioButton();
rdbtn.Name = "rdbtn" + i;
this.Controls.Add(rdbtn);
rdbtn.Text = option.Value;
i++;
}
}
break;
}
}
}
}
In comments, you don't seem to know what a Control is. Everyone is telling you to use Label and RadioButton so just do it.
In the VS designer, select Label from the toolbox and drag it into the form. Change some of its properties, and BOOM! You've done it!
In the comments you said that you need to "store" the text in the Label. Well, that can be wrong in some contexts. You store text in strings, not labels. The latter displays the text.
You also mentioned how you get the text i.e. From XML. But that's irrelevant, you can just store the text you got from XML in a string, let's call it text. And then you change the Text property of the label.
label.Text = text;
Now your label will display the text.
EDIT
let me assume that you are not using VisualStudio. You can also do this with code. First you need to create a Form.
Form form = new Form();
form.Show();
//set properties of the form
Label label = new Label();
//set properties of the label. E.g. Text, width, position etc
form.Controls.Add(label);
So after you created the label, you set the text using the code before the edit and now your label should appear on the form.
private void button1_Click(object sender, EventArgs e)
{
if (id != "")
{
var papers = doc.Descendants("paper");
foreach (var paper in papers)
{
if (paper.Attribute("id").Value == id)
{
var questions = paper.Descendants("question");
var j = 1;
foreach (var question in questions)
{
GroupBox Ques&Ansoptn = new GroupBox();
Ques&Ansoptn.Size = new System.Drawing.Size(720, 120);
Ques&Ansoptn.Text = question.Attribute("ques").Value;
Ques&Ansoptn.Location = new Point(15, 40*j);
Ques&Ansoptn.Font = new Font("Microsoft Sans Serif", 10);
this.Controls.Add(Ques&Ansoptn);
var options = question.Descendants("option");
var i =1;
foreach (var option in options)
{
RadioButton rdbtn = new RadioButton();
rdbtn.Size = new System.Drawing.Size(400, 20);
rdbtn.Location = new Point(20, 20 * i);
rdbtn.Font = new Font("Microsoft Sans Serif", 10);
rdbtn.Text = option.Value;
Ques&Ansoptn.Controls.Add(rdbtn);
i++;
}
j = j+3;
}
break;
}
}
}
}

c# How to change the visible property of a label created at runtime

//Here I create the labels at runtime in one click
Label[] labels = new Label[countresult];
for (int i = 1; i < countresult; i++)
{
labels[i] = new Label();
labels[i].Font = new Font("Arial Rounded MT Bold", 30);
labels[i].ForeColor = System.Drawing.Color.Red;
labels[i].AutoSize = true;
labels[i].Text = "";
//Here I try to assign the value visible = true
labels[i].Visible = true;
labels[i].TabIndex = i;
}
//In a private void of a timer tick I assign the name of label to var "a" and I do the 3 methods
string a = string.Format("labels[{0}]", labelscount);
//1st method
if (this.Controls.ContainsKey(a))
{
this.Controls[a].Visible=false;
}
//2nd method
foreach (Control control in Controls)
{
if (control.Name == a)
{
control.Visible = false;
}
}
//3rd method
if (this.Controls[a] is Label) this.Controls[a].Visible=false;
labelscount++;
Unfortunately none works.
Someone know What's happened?
You are not adding the labels to the owning control. So they will never be displayed. So in your loop you need to add the following as the last line...
this.Controls.Add(labels[i]);

TableLayoutPanel changing it's sizing

I have a few tablelayoutpanels in my program in order to lay out different controls in rows. Until recently these were all working correctly, but after a change in server this is no longer the case. A potential cause is a change in screen resolution as I noticed similar problems on other computers running my program.
An example of this problem is shown below
Before the data rows are loaded (from database), the sizing is incorrect. A measurement using the VS designer shows that the size in figure 2 is correct. Interestingly, if I add a blank row to this table then the resizing doesn't happen.
public JobOpMainTable()
{
DoubleBuffered = true;
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
DoubleBuffered = true;
BackColor = Color.White;
CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
headers();
}
private void headers()
{
string[] names = { "Operation", "Est Lab Hrs", "Est UM Hrs", "Act Lab Hrs", "Act UM Hrs" };
for (int i = 0; i < names.Length; i++) header(names[i], i);
}
private void header(string name, int col)
{
Panel pan = new Panel();
pan.Width = widths[col];
pan.Height = 25;
pan.BackColor = Color.Black;
pan.Dock = DockStyle.Fill;
pan.Margin = new System.Windows.Forms.Padding(0);
TextBox txt = new TextBox();
txt.Font = new System.Drawing.Font("Microsoft sans serif", 8, FontStyle.Bold);
txt.Text = name;
txt.ReadOnly = true;
txt.BackColor = Color.Black;
txt.ForeColor = Color.White;
txt.Dock = DockStyle.Fill;
txt.BorderStyle = System.Windows.Forms.BorderStyle.None
pan.Controls.Add(txt);
Controls.Add(pan, col, 0);
}
private int newRow()
{
int row = Controls.Count / 5;
for (int i = 0; i <= 4; i++)
{
TextBox txt = new TextBox();
txt.Width = widths[i];
txt.BackColor = Color.White;
txt.BorderStyle = System.Windows.Forms.BorderStyle.None;
txt.ReadOnly = true;
txt.Dock = DockStyle.Fill;
txt.Margin = new Padding(0);
txt.Enter += new EventHandler(enterCell);
txt.Leave += new EventHandler(leaveCell);
Controls.Add(txt, i, row);
}
return row;
}
Above is the code I believe is relevant to the problem. The resizing occurs the first time newRow() is called when loading in data: each new box makes the column wider. Again though, the odd thing is this doesn't occur if I add a row from the constructor

Categories