Font does not change dynamically - c#

I have a Windows Form containing a DataGridView which binds to a DataSet dynamically. In the form on the Button.Click event I am changing the DataGridView.Font appearance.
I tried the below code but it's not affecting the DataGridView. Only the header part is being changed.
Please recommend what I've done wrong in coding.
My Code
private void Btn_Language_Click(object sender, EventArgs e)
{
if(DGV_View.Font.Name == "Trebuchet MS")
{
DGV_View.Font = new System.Drawing.Font("NILKANTH", 12);
this.DGV_View.DefaultCellStyle.Font = new System.Drawing.Font("NILKANTH", 12);
}
else if(DGV_View.Font.Name == "NILKANTH")
{
DGV_View.Font = new System.Drawing.Font("Trebuchet MS", 11);
}
}

DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new Font(dataGridView.Font, FontStyle.Bold);
dataGridView.Rows[0].DefaultCellStyle = style;
Just saw it on the other site.

Related

C# combobox with fonts using Datasource Visual Studio 2013

I have a form with a ComboBox on it. I would like to fill this with the available fonts on the system and make the user to select one of these options.
I looked for different approaches to achieving this and I used this question and the answer to load the ComboBox with all the fonts: Fill ComboBox with List of available Fonts
This is my code currently that works:
form.comboBox2.Items.Clear();
System.Drawing.Text.FontCollection fontcoll = new System.Drawing.Text.InstalledFontCollection();
foreach (FontFamily font in fontcoll.Families)
{
form.comboBox2.Items.Add(font.Name);
}
But now I am trying to use instead the DataSource property and I imported the System.Drawing.Text.InstalledFontCollection into my project as datasource.
Here is the code of the designer:
//
// comboBox2
//
this.comboBox2.DataSource = this.installedFontCollectionBindingSource;
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(16, 44);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(144, 21);
this.comboBox2.TabIndex = 9;
this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
Then in my initialization of the form, I have this to set selected the font name to Times New Roman as default:
form.comboBox2.Text = "Times New Roman"
I thought this would be enough to fill the ComboBox and select Times New Roman but apparently is not enough. It displays Times New Roman alright, but the box is empty.
What I would like to get help with:
1) How to make the datasource to populate the ComboBox?
2) Is there a simple way to force the user to select one of the entries from the box and not type in some other value that is not in the list (similarly to the "MatchRequired" property in VBA userforms)?
Thanks in advance.
You should first get a list of all installed font families and then set the list as DataSource of ComboBox. Also you can set DropDownStyle of combo box to DropDownList.
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.DataSource = new InstalledFontCollection().Families;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
You can get selected font family from SelectedValueof ComboBox. For example:
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.SelectedValue != null)
this.Font = new Font((FontFamily)this.comboBox1.SelectedValue, this.Font.Size);
}
You can use :
private void Form1_Load(object sender, EventArgs e)
{
FontFamily[] fontArray = FontFamily.Families;
foreach (FontFamily font in fontArray)
{
comboBox1.Items.Add(font.Name);
}
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
With a property DropDownStyle, users are limited to choices in the list.
For example if you want to assign font with size 14 to the Label :
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Font = new Font(comboBox1.Text , 14);
}

Adding dynamic controls to winform not working

I am creating dynamic PictureBox and label in WinForms. For this I have created a method which creates these items on the basis of given integer. In the first run while loading the form, its works smoothly, but when I pass any integer from a dropdown box, it does not make any changes. I tried debugging the code, and all the labels are created accordingly but it is not reflected in the winForm. I tried using Invalidate, Update, Refresh but non of them worked.
Here is the method that I have implemented.
private void createPictureBox(int size)
{
//this.Controls.Clear();
panel1.Controls.Clear();
Label[] ParameterLabel = new Label[size];
PictureBox[] ParameterBack = new PictureBox[size];
int y_value = 11;
this.Refresh();
for (int i = 0; i < size; ++i)
{
ParameterLabel[i] = new Label();
ParameterLabel[i].Text = "Test Text";
ParameterLabel[i].Font = new System.Drawing.Font("Calibri", 8, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
ParameterLabel[i].ForeColor = System.Drawing.Color.White;
ParameterLabel[i].BackColor = System.Drawing.Color.FromArgb(1, 0, 64);
ParameterLabel[i].Size = new System.Drawing.Size(145, 20);
ParameterLabel[i].Location = new Point(30, y_value);
ParameterLabel[i].Anchor = AnchorStyles.Left;
ParameterLabel[i].Visible = true;
ParameterBack[i] = new PictureBox();
ParameterBack[i].Image = Image.FromFile(STR_SETTING_PATH + "\\" + STR_IDEA_NO_XXXXX + "_01_nv.png");
ParameterBack[i].Size = new System.Drawing.Size(400, 32);
ParameterBack[i].Location = new Point(2, y_value - 10);
ParameterBack[i].Anchor = AnchorStyles.Left;
ParameterBack[i].Visible = true;
//this.Controls.Add(ParameterBack[i]);
y_value += 37;
}
panel1.Controls.AddRange(ParameterLabel);
panel1.Controls.AddRange(ParameterBack);
panel1.Invalidate();
}
Who can you distinguish between controls created in the first call and those created in other calls? I've tested your function with a tiny change, it seems to be working fine:
int CallIndex = 0; // this is on the form level
private void button1_Click(object sender, EventArgs e)
{
createPictureBox(3);
CallIndex += 1;
}
private void createPictureBox(int size)
{
// this has the exact same code as your method (copy-paste into my visual studio),
// except this change:
// ParameterLabel[i].Text = "Test Text";
ParameterLabel[i].Text = string.Format("Test {0}", CallIndex); // instead of the row above
}
I did remove the previously added controls and added the new one after which apparently solved my problem. The problem was due to piling of Controls one over another. I first removed the previously created controls using
this.Controls.Remove(UserControl1);
Then re-created its instance, which solved my problem.

How to Remove Textbox control created dynamically in WPF Grid?

I am unable to remove the textbox which is created Dynamically using Combobox selected Item in Grid. if the selected value is not equal to "Other (describe)", i have to remove the textbox. I have this code..
private void btn_addnew_Click(object sender, RoutedEventArgs e)
{
ComboBox cmb=new ComboBox();
.....
cmb.SelectionChanged+= cmb_SelectionChanged;
.....
}
void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var txt = new TextBox();
if (e.AddedItems[0].ToString() == "Other (describe)")
{
var row = (int)((ComboBox)sender).Tag;
Grid.SetRow(txt, row);
Grid.SetColumn(txt, 1);
txt.Margin = new Thickness(10, 10, 0, 0);
grid_typeFixture.Children.Add(txt);
}
else
grid_typeFixture.Children.Remove(txt);
}
There is a risk of setting the Textbox names dynamically if it doesn't follow the namming rules (Example : Textbox name can not have white space), Instead you can use "Tag" property of the textbox while creating and search it whenever want to remove it.
Assign a name to your TextBox while creating, you can use RegisterName,
txt = new TextBox();
txt.Margin = new Thickness(10, 10, 0, 0);
txt.Name = "DynamicLine" + i;
RegisterName(txt.Name, txt);
Grid.SetRow(txt, i);
Grid.SetColumn(txt, 2);
grid_typeFixture.Children.Add(txt);
And you can remove like this, using FindName
txt = (TextBox)grid_typeFixture.FindName("lbl_DynamicLine" + row);
if (txt != null)
{
UnregisterName(txt.Name);
grid_typeFixture.Children.Remove(txt);
}

C# Get text from a textbox that is made at runtime

Hello I am making a program that has 2 textboxes and 2 buttons
When I press the add button then it will make 2 new textboxes using this code :
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
TextBox NEW_TEXTBOX_1 = new TextBox();
NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
//Set NEW_TEXTBOX_1 font
NEW_TEXTBOX_1.Font = new Font("Segoe Print", 9);
NEW_TEXTBOX_1.Font = new Font(NEW_TEXTBOX_1.Font, FontStyle.Bold);
//Set pos and size and then create it.
NEW_TEXTBOX_1.Location = new System.Drawing.Point(16, 71 + (35 * HOW_FAR));
NEW_TEXTBOX_1.Size = new System.Drawing.Size(178, 29);
this.Controls.Add(NEW_TEXTBOX_1);
//Make the PRICE_TEXTBOX_
TextBox NEW_TEXTBOX_2 = new TextBox();
NEW_TEXTBOX_2.Name = "PRICE_TEXTBOX_" + HOW_FAR.ToString();
//Set NEW_TEXTBOX font
NEW_TEXTBOX_2.Font = new Font("Segoe Print", 9);
NEW_TEXTBOX_2.Font = new Font(NEW_TEXTBOX_2.Font, FontStyle.Bold);
//Set pos and size and then create it.
NEW_TEXTBOX_2.Location = new System.Drawing.Point(200, 71 + (35 * HOW_FAR));
NEW_TEXTBOX_2.Size = new System.Drawing.Size(89, 29);
this.Controls.Add(NEW_TEXTBOX_2);
//Change pos of the add button
ADD_ROW.Location = new System.Drawing.Point(295, 71 + (35 * HOW_FAR));
this.Height = 349 + (35 * HOW_FAR);
this.Width = 352;
}
This works very well but now I want to get the text from a newly made textbox back how do I do this?
This doesn't work because it says : NAME_TEXTBOX_1 Does not exist in the current context.
private void button2_Click(object sender, EventArgs e)
{
string tmpStr = NAME_TEXTBOX_1.Text;
}
You need to move the variable declaration outside of the ADD_ROW_Click event handler so that it's accessible outside that block;
TextBox NEW_TEXTBOX_1;
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
NEW_TEXTBOX_1 = new TextBox(); //remove "TextBox" since we declared it above
NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
//...
The alternative, and possibly better depending on the number of textboxes, is to add each TextBox you create into a List. You can then iterate that List from and find the TextBox you want. For example
List<TextBox> allTextBoxes = new List<TextBox>();
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
TextBox NEW_TEXTBOX_1 = new TextBox();
//...fill out the properties
//add an identifier
NEW_TEXTBOX_1.Tag = 1;
allTextBoxes.Add(NEW_TEXTBOX_1);
}
Then when you want a particular TextBox
private void button2_Click(object sender, EventArgs e)
{
TextBox textBox1 = allTextBoxes.Where(x => x.Tag == 1).FirstOrDefault();
string tmpStr = "";
if(textBox1 != null)
tmpStr = textBox1.Text;
}
Alternatively, and especially if you're going to have a lot of TextBoxes, you could store them in a Dictionary as Corak suggested in the comments.
you're declaring NAME_TEXTBOX_1 within the ADD_ROW_Click method, which is why it isn't available within the button2_Cick method.
You can declare the textbox at the class level to access it in both places.
(You should work on renaming your variables too - e.g. TextBoxPrice)
One simple solution:
Make a private field called "NEW_TB" for example.
In your button2_Click(..) { string tmpStr = NEW_TB.Text; }
Add in your ADD_ROW_Click(..) method NEW_TB = NAME_TEXTBOX_1;
If I understood your question right, this should work.
Make global your textboxes:
TextBox NEW_TEXTBOX_1;
then initiate them in your method:
NEW_TEXTBOX_1 = new TextBox();
OMG Never mind sorry guys I found a good way :D
var text = (TextBox)this.Controls.Find("PRICE_TEXTBOX_1", true)[0];
text.Text = "PRO!";
This works pretty well :)

Adding a control in Datagridview

How to add a control in DataGridView? Using Button event. For example i want to create a new a row and column in DataGridView, this i want to happen through button control. How can i do it?
I am using C#.net and MS-Access.
Your question doesn't match it's title. The title asks about controls but the question is about rows and columns, I'm ignoring the title and I'm assuming it's an unbound DataGridView.
This MSDN link shows how to add rows and this shows how to add columns.
Here is a piece of code for adding a control into the gridview.
private void addNewRowButton_Click(object sender, EventArgs e)
{
this.DataGridViewIssue.Rows.Add();//This line will add a new button contol into the grid
}
private void deleteRowButton_Click(object sender, EventArgs e)
{
if (this.DataGridViewIssue.SelectedRows.Count > 0 &&
this.DataGridViewIssue.SelectedRows[0].Index !=
this.DataGridViewIssue.Rows.Count - 1)
{
this.DataGridViewIssue.Rows.RemoveAt(
this.DataGridViewIssue.SelectedRows[0].Index);
}
}
private void SetupLayout()
{
this.Size = new Size(1055, 800);
addNewRowButton.Text = "Add Row";
addNewRowButton.Location = new Point(10, 10);
addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
deleteRowButton.Text = "Delete Row";
deleteRowButton.Location = new Point(100, 10);
deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
buttonPanel.Controls.Add(addNewRowButton);
buttonPanel.Controls.Add(deleteRowButton);
buttonPanel.Height = 50;
buttonPanel.Dock = DockStyle.Bottom;
this.Controls.Add(this.buttonPanel);
}

Categories