从其他形式设置标签的字体. [英] Setting font of a label from other form..

查看:104
本文介绍了从其他形式设置标签的字体.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有两种形式(form1,form2).Form1具有3个标签. form2具有2个组合框(combobox1,combobox2)和保存"按钮. combobox1用于字体列表,combobox2用于字体大小列表.选择所需的字体之后.如何将其分配给form1 ... ???

My application has two forms(form1,form2).. Form1 has 3 labels. form2 has 2 comboboxes(combobox1,combobox2) and save button. combobox1 is for list of fonts and combobox2 for list of fontsizes. After selecting required font.. How do i assign it to the labels in the form1...???

推荐答案

使用此代码..

在form2中,单击保存按钮..

Session ["listoffonts"] = combobox1.SelectedValue.ToString();
Session ["listoffontsize"] = combobox2.SelectedValue.ToString();


在Form1中页面加载

label1.Text = Session ["listoffonts"].ToString();
label2.Text = Session ["listoffontsize"].ToString();
Use This code..

In form2,at save button click..

Session["listoffonts"]=combobox1.SelectedValue.ToString();
Session["listoffontsize"]=combobox2.SelectedValue.ToString();


In Form1 Page_load

label1.Text=Session["listoffonts"].ToString();
label2.Text=Session["listoffontsize"].ToString();


首先在Form1.Designer.cs中,将所有三个标签声明设为"public".
在Form2中,如果从Combbox字体中选择的是"Calibri",则在保存"上单击,编写以下代码. Jst用所选的任何字体替换"Calibri".
first in Form1.Designer.cs, make all three labels declarations as "public".

in Form2 if from Combbox font selected is "Calibri", then on Save click, write below code. Jst replace "Calibri" with whichever font selected.
Font objFont = new Font("Calibri", 9, FontStyle.Bold);

Form1 objForm1 = new Form1();
objForm1.label1.Font = objFont;
objForm1.label2.Font = objFont;
objForm1.label3.Font = objFont;
objForm1.Show();


我是vb.net开发人员,不太熟练C#
抱歉,如果代码块中有任何错误,


但过程应如下...,

创建一个类LookNFeel
创建静态的"NewFont"变量,以便在您的应用程序中将其用作全局变量...并且您可以轻松地以其他形式检索它.
I am vb.net developer not so fluent in C#
sorry if any mistake in code-blocks,


but process should like below...,

create a class LookNFeel
create static ''NewFont'' variable so, that in your application it can work as global variable... and you can retrieve it in other forms easily.
public class LookNFeel
{
   Public static Font NewFont = New Font("Arial", 12);
   Public static SetFont(string FontName,int as fontsize)
   {
       NewFont = New Font(FontName, fontsize); //Make sure passing correct font name
   }
}



现在,从Form1中单击按钮调用函数以在全局NewFont变量中设置字体



Now, from Form1 when click button call function to set font in global NewFont variable

LookNFeel.SetFont(ComboFontNm.Text, convert.ToInt32(ComboFontsize.Text));



在像这样的Form2的Load事件设置字体中



in Form2''s Load event set font like this

foreach (Control c in this.Controls)
{
    c.font = LookNFeel.NewFont;
}



祝您编码愉快!
:)



Happy Coding!
:)


这篇关于从其他形式设置标签的字体.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆