1 Star 1 Fork 0

hehe1008899/NameReload

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
RandomNamePage.cs 5.09 KB
一键复制 编辑 原始数据 按行查看 历史
Cube 提交于 2022-05-10 14:44 . SourceCode
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Namer.Pages
{
public class RandomNamePage
{
private static readonly List<Control> PageControls = new List<Control>();
public RandomNamePage() => Interlize();
public List<Control> GetControls(int x)
{
PageControls.Clear();
Interlize();
foreach (Control ctr in PageControls)
{
if(!ctr.Name.Contains("[noresize]")) ctr.Location = new Point(x + ctr.Location.X, ctr.Location.Y);
}
return PageControls;
}
private static UILabel Name;
private static UIComboBox Class;
private static void Interlize()
{
UIMarkLabel title = new UIMarkLabel()
{
Text = "随机点名",
Location = new Point(10, 10)
}; PageControls.Add(title);
Name = new UILabel()
{
Location = new Point(10, 120),
Width = UI.MainForm.Width - 70,
Height = 45,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Microsoft YaHei", 18),
Text = "请点击按钮抽取吧"
};PageControls.Add(Name);
UIButton GenRandom = new UIButton()
{
Text = "点一个!",
Location = new Point(10, UI.MainForm.Height - 120),
Size = new Size(UI.MainForm.Width - 70, 30)
};PageControls.Add(GenRandom); GenRandom.Click += GenRandom_Click;
Class = new UIComboBox()
{
Size = new Size(UI.MainForm.Width - 70, 30),
Location = new Point(10, UI.MainForm.Height - 80)
};PageControls.Add(Class);intersize();
}
private static void intersize()
{
Class.Items.Clear();
foreach (string dara in BackGround.cm) Class.Items.Add(dara);
if(Class.Items.Count > 0) Class.SelectedIndex = 0;
}
private static void GenRandom_Click(object sender, EventArgs e)
{
Random r1 = new Random();
string[] data = BackGround.UsersList[Class.SelectedItem.ToString()];
Name.Text = data[r1.Next(0, data.Count())];
}
}
class BackGround
{
public static Dictionary<string, string[]> UsersList = new Dictionary<string, string[]>();
public static readonly List<string> cm = new List<string>();
private static string loccalbase = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Namer\\";
public static void Delete(string table)
{
File.Delete(loccalbase + table + ".json");
OnDone(0);
}
public static void SaveToFile(string table,string newtxt,int count)
{
JObject root = JObject.Parse(File.ReadAllText(loccalbase + table + ".json"));
JArray ary = JArray.Parse(root["users"].ToString());
ary[count] = newtxt;
root["users"] = ary;
File.WriteAllText(loccalbase + table + ".json", root.ToString());
OnDone(0);
}
public static void CreatNew(string table,string[] args)
{
JObject root = new JObject();
JArray ary = new JArray();
foreach (string data in args) ary.Add(data);
root.Add("users",ary.ToString());
File.WriteAllText(loccalbase + table + ".json",root.ToString());
OnDone(0);
}
public static void AddToFileEnd(string table, string newtxt)
{
JObject root = JObject.Parse(File.ReadAllText(loccalbase + table + ".json"));
JArray ary = JArray.Parse(root["users"].ToString());
ary.Add(newtxt);
root["users"] = ary;
File.WriteAllText(loccalbase + table + ".json", root.ToString());
OnDone(0);
}
public static void OnDone(int id)
{
cm.Clear();
UsersList.Clear();
if (!Directory.Exists(loccalbase)) Directory.CreateDirectory(loccalbase);
DirectoryInfo d1 = new DirectoryInfo(loccalbase);
FileInfo[] fs = d1.GetFiles();
foreach (FileInfo cfg in fs)
{
JObject root = JObject.Parse(File.ReadAllText(cfg.FullName));
List<string> Users = new List<string>();
foreach (JValue data in JArray.Parse(root["users"].ToString()))
{
Users.Add(data.ToString());
}
UsersList.Add(cfg.Name.Replace(".json", ""), Users.ToArray());
cm.Add(cfg.Name.Replace(".json", ""));
GC.Collect();
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/FxTStudios/name-reload.git
git@gitee.com:FxTStudios/name-reload.git
FxTStudios
name-reload
NameReload
master

搜索帮助