我txt1在应用程序运行时(在运行时)创建了一个在私有void内部以编程方式调用的标签,我想在另一个私有void中更改此标签的文本,但我无法txt1从另一个void 访问它!
用于动态创建标签的脚本:
private void labelCreate()
{
Label txt1 = new Label();
}
Run Code Online (Sandbox Code Playgroud)
用于更改其文本txt1已在labelCreatevoid中创建的脚本(此脚本不起作用,因为txt1尚未声明为控件):
private void labelTextChange()
{
txt1.Text = "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)
更新1:我需要创建100个具有不同名称的标签,然后我将使用一个for语句来创建100个标签.我无法声明100个全局变量.所以我需要传递变量而不是将它们声明为全局变量.
更新2:是否可以将for语句中的100个标签声明为全局?
更新3:假设我想从数据库中获取一些数据,我想在唯一标签中单独显示它们.所以我给每个标签命名并根据我从DB获得的不同数据更改它们的文本!所以我需要2个空洞:一个用于根据我从DB获得的行数创建标签,另一个空白用于更改我之前创建的标签文本!
问题:如何访问在不同的void中创建的控件?如果有答案请分享链接:)
谢谢
我有一维一维的数组:
int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};
Run Code Online (Sandbox Code Playgroud)
我想将这个1D数组划分为4行5列的2D数组,其中前5个值进入第1行,后5个进入第2行,依此类推.最终结果应如下所示:
array2D:
[[10, 11, 12, 13, 14]
[20, 21, 22, 23, 24]
[30, 31, 32, 33, 34]
[40, 41, 42, 43, 44]]
Run Code Online (Sandbox Code Playgroud)
实际上,阵列将更长(可能超过100行),但列数为5,行数可以分为5.我已经简化了例如.这是我到目前为止所尝试的:
int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};
int[,] array2D = new int[(array.Length/5),5];
int count_0 = 1;
int count_1 = 1;
int count_2 = 1;
int …Run Code Online (Sandbox Code Playgroud) 请注意,每个<Field></Field>可具有不同的元件,如突出显示<I32>或<String>.我想在datagridview中显示元素名称,其中Type是元素的名称(I32或String或其他子元素<Field>): -

到目前为止,我已经尝试过此代码,但它返回了An unhandled exception of type 'System.NullReferenceException'.
XDocument doc = XDocument.Load("GetLotDetails.xml");
var data = doc.Descendants("Document").Where(x => (String)x.Attribute("name") == "DATA").SelectMany(x => x.Elements("Field"));
var query = from d in data
let str = d.Element("String").Name
let other = d.Element("I32").Name
select new
{
Name = d.Attribute("name").Value,
Type = str.Equals("String") ? "String" : (other.Equals("I32") ? "I32" : null),
Value = d.Value,
};
dataGridView1.DataSource = query.ToList();
Run Code Online (Sandbox Code Playgroud)
所以这个想法就是让匿名Type = *whatever element name under …
我在我的Text Class中有这个方法,我似乎无法翻转整个文本.
我正在使用Matrix来转换GraphicsPath用于绘制字符串的内容.
以下是我使用@ Jimi答案的代码:
public LayerClass DrawString(LayerClass.Type _text, string text, RectangleF rect, Font _fontStyle, Brush brush, float angle, PaintEventArgs e)
{
using (StringFormat string_format = new StringFormat())
{
SizeF stringSize = e.Graphics.MeasureString(text, _fontStyle);
rect.Location = new PointF(Shape.center.X - (rect.Width / 2), Shape.center.Y - (rect.Height / 2));
GraphicsContainer gc = e.Graphics.BeginContainer();
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(rect));
RectangleF r = new RectangleF(rect.Location, rect.Size);
GraphicsPath path = new GraphicsPath();
if (text == "" || text == " …Run Code Online (Sandbox Code Playgroud) 我是Async和Await的新手,并且创建了一个简单的项目以了解其工作原理。为此,我有一个简单的Windows Form应用程序,其中包含2个元素:
当我单击按钮时,它应该在文本框中显示所有已完成的项目。这是我编写的代码:
private async void btnGetCompletedItems_Click(object sender, EventArgs e)
{
QueueSystem queueSystem = QueueSystem.NewInstance(75);
Stopwatch watch = new Stopwatch();
watch.Start();
await Task.Run(() => GetCompletedItems(queueSystem));
watch.Stop();
lblTime.Text = $"{watch.ElapsedMilliseconds.ToString()} ms";
}
private void GetCompletedItems(QueueSystem queueSystem)
{
foreach (var item in queueSystem.GetCompletedItems())
{
txtItems.Text += $"{txtItems.Text}{item.ItemKey}{Environment.NewLine}";
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在
txtItems.Text + = $“ {txtItems.Text} {item.ItemKey} {Environment.NewLine}”;
错误说
附加信息:跨线程操作无效:控制'txtItems'是从不是在其上创建线程的线程访问的。
我检查了Debug,并为GetCompletedItems()创建了一个新线程。当我阅读有关Async和Await的文章时,我读到它不一定创建一个新线程,但是由于某种原因它似乎创建了一个新线程。
我对Async和Await的实现和理解错了吗?是否可以在Windows Forms应用程序中使用Async和Await?
如何检查数组中所有TextBox控件的Tag属性?
我想要这样的东西:
If textBox.Tag And textbox2.Tag And textbox21.Tag And
textbox22.Tag And textbox23.Tag And textbox24.Tag = "2" Then
Run Code Online (Sandbox Code Playgroud)
这是我的TextBoxes数组:
Dim allTextboxes() As TextBox = {textBox, narNaslov, narPersona, narDani, narPersona2,
kupIme, kupAdresa, kupKontakt, uvBroj, uvDatum, uvIznos,
uvAvans, uvRok, uvNacin, datumTbox}
Run Code Online (Sandbox Code Playgroud) 我最近听说.NET Core(.NET Core 3.0)的alpha版本支持Windows Forms和WPF.但Visual Studio Designer(和Visual Studio)不支持Windows窗体和WPF的.NET Core版本.是否有任何方法可以使Visual Studio Designer(和Visual Studio)与.NET Core 3.0一起使用,还是可以使用其他任何设计器或IDE来使用.NET Core 3?
我问这个的主要原因是因为使用代码编辑器(如VS Code和Atom)很难(对我和其他人而言).
我希望这是一个很好的问题.
我遇到了一个问题,我认为这可能是由于我的类的复杂性将对象传递给对方所以我最小化它并且问题仍然存在:
我有一个在VS2017社区中创建的默认winform项目
在表单上我添加了一个文本框,一个richtextbox,一个backgroundworker和一个用于激活后台工作者的按钮.
我在表单中放入以下代码来填充文本框并在按钮单击时运行worker:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "Hello";
richTextBox1.Text = "World!";
}
private void button1_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
MessageBox.Show(textBox1.Text);
MessageBox.Show(richTextBox1.Text);
}
}
Run Code Online (Sandbox Code Playgroud)
我运行程序,我不明白接下来会发生什么.
textBox1.Text可从表格访问,以便MessageBox显示正常.richTextBox1.Text无法访问并给我这个错误:
跨线程操作无效:控制'richTextBox1'从其创建的线程以外的线程访问.
为什么?
我假设richTextBox有更多的路由和包装,但.Text属性不完全相同?!这里发生了什么?
编辑:我不认为这是一个重复的标记问题,因为他没有工作,TextBox.Text而我的是.我问的是TextBox和RichTextBox .Text属性之间的区别.
在Windows窗体应用程序中,我有一个存储为byte []的文件.
当用户单击按钮时,我想打开文件而不将其保存在本地.这是可能的,如果,那怎么样?
或者我必须将字节数组保存为本地文件然后运行此文件?
谢谢,卡尔
winforms ×10
c# ×9
.net ×2
.net-core ×1
arrays ×1
async-await ×1
asynchronous ×1
bytearray ×1
file ×1
gdi+ ×1
graphicspath ×1
linq ×1
richtextbox ×1
text ×1
textbox ×1
uwp ×1
vb.net ×1
wpf ×1
xml ×1