c# textbox更新及输入问题

os超级菜鸟 2019-02-14 10:45:49
各位大神,请教如下问题:

上位机软件,跟外部通讯,得到一个值A,通过timer 周期性的把A赋值给textbox。但是我需要可以设置此textbox内部的值,反向传递来更改A值,应该怎么实现?

...全文
637 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
OrdinaryCoder 2019-02-15
  • 打赏
  • 举报
回复
引用 6 楼 os超级菜鸟 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassDataChange
{

public struct CustemData
{
public int Data;
public string Property;
}

class DataChange
{
public static string addr; //PLC的IP地址
public static int port; //PLC的端口号
public static int UserNo; //用户模式,1代表工程师

public static CustemData[] AAA = new CustemData[50];

public static void xx()
{
AAA[0].Property = "D1106"; //上料伺服首层位置
AAA[1].Property = "D1100"; //加速度
AAA[2].Property = "D1101"; //速度
AAA[3].Property = "D103"; //层间距
AAA[4].Property = "D104"; //当前层
AAA[5].Property = "D105"; //总层数
AAA[6].Property = "D106"; //点动距离
}
}
}


你看我这样写好不好:
这个是我自定义的类和结构体数据类型(property是这个参数的属性,用来在其他页面引用这个属性当作PLC的地址值,Data是用来保存这个通讯读取的值),这个类是用来共享各个窗口的值,有一个通讯类不停的更新这个类内部的值。

在另一个窗口,textbox要绑定AAA[i],这样可以吗?

如果按照你说的textbox和AAA[i]进行绑定,说明你有N个textbox需要绑定值,但是系统并不会区分AAA[i]对应的是哪一个textbox
所以

public struct CustemData
{
int 上料伺服首层位置;
int 加速度;
int 速度;
int 层间距;
int 当前层;
int 总层数;
int 点动距离;
}

CustemData这样 每一个字段对应一个textbox,大概是这样,没往深想,具体你自己再考虑一下
OrdinaryCoder 2019-02-15
  • 打赏
  • 举报
回复
引用 7 楼 os超级菜鸟 的回复:
[quote=引用 5 楼 OrdinaryCoder 的回复:]
比如textbox和后台一个字段filePath进行绑定,当后台filePath被改变时,界面textbox的内容随之改变。当改变界面textbox中内容时,后台的filePath也会发生变化,所以你需要的写入操作并不需要专门去写。可以理解为绑定后这俩值变成一个了。


刚忘记引用了,不知道您能不能看到[/quote]
看你的意思应该是有N个textbox,每个绑定一个CustemData
对象,如果这样做的话 不如用一个listView表格来显示你的数据,如果要修改数据就对相应行进行操作
os超级菜鸟 2019-02-15
  • 打赏
  • 举报
回复
引用 9 楼 OrdinaryCoder 的回复:
CustemData这样 每一个字段对应一个textbox,大概是这样,没往深想,具体你自己再考虑一下


谢谢,查了下,textbox好像不能单独对某个变量进行绑定。不过问题还是解决了。

后来只能绕了一下,在结构体中又加个2个属性modify和WRDate,如果是从textbox端进行了修改值的动作,就把textbox内部的值放到WWrData中,在通讯类中,用timer变量轮询,如果modify=true,就向外部进行写入。

一个窗口内有很多textbox,是显示不同的设备动作的设定值,所以不能在listview中显示,我每个变量只有AAA[i].Date是需要显示的,其他的是用来指定变量在PLC中的地址的。
os超级菜鸟 2019-02-14
  • 打赏
  • 举报
回复
引用 1 楼 OrdinaryCoder 的回复:
WPF的话可以将A和textbox的text属性双向绑定,这样一端改变,另一端也会跟着改变


我这里的A值是通过网口读取的PLC的地址的值来的,我还需要触发一个写入的操作。
OrdinaryCoder 2019-02-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication
{
public class OrderInfo
{
public string temp { get; set; }
public string temp1 { get; set; }
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public ObjectDataProvider myObject
{
get
{
return TryFindResource("GoodsDetail") as ObjectDataProvider;//找到前端设置的资源文件
}
}
public MainWindow()
{
InitializeComponent();
OrderInfo obj = new OrderInfo()
{
temp = "11da",
temp1 = "admin"
};
setData(obj);
}
public void setData(OrderInfo obj)
{
myObject.ObjectInstance = obj;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
OrderInfo obj = myObject.ObjectInstance as OrderInfo;
MessageBox.Show(string.Format("temp:{0},temp1:{1}", obj.temp, obj.temp1));
}
}
}

后台代码
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ObjectDataProvider x:Key="GoodsDetail"/>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBox Margin="10" Text="{Binding Source={StaticResource GoodsDetail},Path=temp}" Width="80"/>
<TextBox Margin="10" Text="{Binding Source={StaticResource GoodsDetail},Path=temp1}" Width="80"/>
<Button Content="提交信息" Margin="10" Click="Button_Click"/>
</StackPanel>
</Window>

前台代码
OrdinaryCoder 2019-02-14
  • 打赏
  • 举报
回复
可以看看https://blog.csdn.net/jeryjeryjery/article/details/70216179
OrdinaryCoder 2019-02-14
  • 打赏
  • 举报
回复
WPF的话可以将A和textbox的text属性双向绑定,这样一端改变,另一端也会跟着改变
os超级菜鸟 2019-02-14
  • 打赏
  • 举报
回复
引用 5 楼 OrdinaryCoder 的回复:
比如textbox和后台一个字段filePath进行绑定,当后台filePath被改变时,界面textbox的内容随之改变。当改变界面textbox中内容时,后台的filePath也会发生变化,所以你需要的写入操作并不需要专门去写。可以理解为绑定后这俩值变成一个了。


刚忘记引用了,不知道您能不能看到
os超级菜鸟 2019-02-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassDataChange
{

public struct CustemData
{
public int Data;
public string Property;
}

class DataChange
{
public static string addr; //PLC的IP地址
public static int port; //PLC的端口号
public static int UserNo; //用户模式,1代表工程师

public static CustemData[] AAA = new CustemData[50];

public static void xx()
{
AAA[0].Property = "D1106"; //上料伺服首层位置
AAA[1].Property = "D1100"; //加速度
AAA[2].Property = "D1101"; //速度
AAA[3].Property = "D103"; //层间距
AAA[4].Property = "D104"; //当前层
AAA[5].Property = "D105"; //总层数
AAA[6].Property = "D106"; //点动距离
}
}
}


你看我这样写好不好:
这个是我自定义的类和结构体数据类型(property是这个参数的属性,用来在其他页面引用这个属性当作PLC的地址值,Data是用来保存这个通讯读取的值),这个类是用来共享各个窗口的值,有一个通讯类不停的更新这个类内部的值。

在另一个窗口,textbox要绑定AAA[i],这样可以吗?
OrdinaryCoder 2019-02-14
  • 打赏
  • 举报
回复
比如textbox和后台一个字段filePath进行绑定,当后台filePath被改变时,界面textbox的内容随之改变。当改变界面textbox中内容时,后台的filePath也会发生变化,所以你需要的写入操作并不需要专门去写。可以理解为绑定后这俩值变成一个了。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧