Control









How do I use Control
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: Form9.cs Copy
121         private void ribbonControl1_SelectedPageChanged(object sender, EventArgs e)
122         {
123             CloseForm("Form9");
124         }
File name: frmReg.cs Copy
25         private void txt6_KeyPress(object sender, KeyPressEventArgs e)
26         {
27             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
28                 e.Handled = true;
29         }
File name: frmThiThat.cs Copy
135         void LoadCauHoiVaoControl()
136         {
137
138             try
139             {
140                 groupBox1.Text = "Câu: " + (CauHienTai + 1).ToString();
141                 lbCauHoi.Text = BangDeThi.Rows[CauHienTai][1].ToString();
142                 lbA.Text = BangDeThi.Rows[CauHienTai][2].ToString();
143                 lbB.Text = BangDeThi.Rows[CauHienTai][3].ToString();
144                 lbC.Text = BangDeThi.Rows[CauHienTai][4].ToString();
145                 lbD.Text = BangDeThi.Rows[CauHienTai][5].ToString();
146
147                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("A"))
148                     checkBox1.Checked = true;
149                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("B"))
150                     checkBox2.Checked = true;
151                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("C"))
152                     checkBox3.Checked = true;
153                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("D"))
154                     checkBox4.Checked = true;
155             }
156             catch (Exception ex)
157             { MessageBox.Show(ex.Message); }
158         }
File name: frmThiThat.cs Copy
190         private void frmThiThat_Load(object sender, EventArgs e)
191         {
192
193
194             lbNgaythang.Text = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
195
196             {
197                 try
198                 {
199                     if (DeThi == true)
200                         Load_DeThiThat();
201                     else
202                         Load_DeThiThat();
203                     TaoBangRandomCauTraLoi();
204                     LoadCauHoiVaoControl();
205                     timer1.Start();
206
207                     btPre.Enabled = false;
208                 }
209                 catch (Exception ex)
210                 { MessageBox.Show(ex.Message); }
211             }
212
213         }
File name: frmThiThat.cs Copy
215         private void btPre_Click(object sender, EventArgs e)
216         {
217             try
218             {
219                 GhiLaiDapAnTS();
220                 if (CauHienTai > 0)
221                 {
222                     GhiLaiDapAnTS();
223                     btNext.Enabled = true;
224                     checkBox1.Checked = false;
225                     checkBox2.Checked = false;
226                     checkBox3.Checked = false;
227                     checkBox4.Checked = false;
228                     CauHienTai--;
229                     LoadCauHoiVaoControl();
230                 }
231                 if (CauHienTai == 0)
232                     btPre.Enabled = false;
233             }
234             catch (Exception ex)
235             { MessageBox.Show(ex.Message); }
236         }
File name: frmThiThat.cs Copy
238         private void btNext_Click(object sender, EventArgs e)
239         {
240             try
241             {
242                 GhiLaiDapAnTS();
243                 if (CauHienTai < SoCauHoi - 1)
244                 {
245                     GhiLaiDapAnTS();
246                     btPre.Enabled = true;
247                     checkBox1.Checked = false;
248                     checkBox2.Checked = false;
249                     checkBox3.Checked = false;
250                     checkBox4.Checked = false;
251                     CauHienTai++;
252                     LoadCauHoiVaoControl();
253                 }
254                 if (CauHienTai == SoCauHoi - 1)
255                     btNext.Enabled = false;
256             }
257             catch (Exception ex)
258             { MessageBox.Show(ex.Message); }
259         }
File name: DemoOwnershipGui.cs Copy
23     public void OnGUI()
24     {
25         GUI.skin = this.Skin;
26         GUILayout.BeginArea(new Rect(Screen.width - 200, 0, 200, Screen.height));
27         {
28             string label = TransferOwnershipOnRequest ? "passing objects" : "rejecting to pass";
29             if (GUILayout.Button(label))
30             {
31                 this.TransferOwnershipOnRequest = !this.TransferOwnershipOnRequest;
32             }
33         }
34         GUILayout.EndArea();
35
36
37
38         if (PhotonNetwork.inRoom)
39         {
40             int playerNr = PhotonNetwork.player.ID;
41             string playerIsMaster = PhotonNetwork.player.isMasterClient ? "(master) " : "";
42             string playerColor = PlayerVariables.GetColorName(PhotonNetwork.player.ID);
43             GUILayout.Label(string.Format("player {0}, {1} {2}(you)", playerNr, playerColor, playerIsMaster));
44
45             foreach (PhotonPlayer otherPlayer in PhotonNetwork.otherPlayers)
46             {
47                 playerNr = otherPlayer.ID;
48                 playerIsMaster = otherPlayer.isMasterClient ? "(master)" : "";
49                 playerColor = PlayerVariables.GetColorName(otherPlayer.ID);
50                 GUILayout.Label(string.Format("player {0}, {1} {2}", playerNr, playerColor, playerIsMaster));
51             }
52
53             if (PhotonNetwork.inRoom && PhotonNetwork.otherPlayers.Length == 0)
54             {
55                 GUILayout.Label("Join more clients to switch object-control.");
56             }
57         }
58         else
59         {
60             GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
61         }
62     }
File name: ChatGui.cs Copy
103     public void OnGUI()
104     {
105         if (!this.IsVisible)
106         {
107             return;
108         }
109
110         GUI.skin.label.wordWrap = true;
111         //GUI.skin.button.richText = true; // this allows toolbar buttons to have bold/colored text. nice to indicate new msgs
112         //GUILayout.Button("lala"); // as richText, html tags could be in text
113
114
115         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
116         {
117             if ("ChatInput".Equals(GUI.GetNameOfFocusedControl()))
118             {
119                 // focus on input -> submit it
120                 GuiSendsMsg();
121                 return; // showing the now modified list would result in an error. to avoid this, we just skip this single frame
122             }
123             else
124             {
125                 // assign focus to input
126                 GUI.FocusControl("ChatInput");
127             }
128         }
129
130         GUI.SetNextControlName("");
131         GUILayout.BeginArea(this.GuiRect);
132
133         GUILayout.FlexibleSpace();
134
135         if (this.chatClient.State != ChatState.ConnectedToFrontEnd)
136         {
137             GUILayout.Label("Not in chat yet.");
138         }
139         else
140         {
141             List channels = new List(this.chatClient.PublicChannels.Keys); // this could be cached
142             int countOfPublicChannels = channels.Count;
143             channels.AddRange(this.chatClient.PrivateChannels.Keys);
144
145             if (channels.Count > 0)
146             {
147                 int previouslySelectedChannelIndex = this.selectedChannelIndex;
148                 int channelIndex = channels.IndexOf(this.selectedChannelName);
149                 this.selectedChannelIndex = (channelIndex >= 0) ? channelIndex : 0;
150
151                 this.selectedChannelIndex = GUILayout.Toolbar(this.selectedChannelIndex, channels.ToArray(), GUILayout.ExpandWidth(false));
152                 this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
153
154                 this.doingPrivateChat = (this.selectedChannelIndex >= countOfPublicChannels);
155                 this.selectedChannelName = channels[this.selectedChannelIndex];
156
157                 if (this.selectedChannelIndex != previouslySelectedChannelIndex)
158                 {
159                     // changed channel -> scroll down, if private: pre-fill "to" field with target user's name
160                     this.scrollPos.y = float.MaxValue;
161                     if (this.doingPrivateChat)
162                     {
163                         string[] pieces = this.selectedChannelName.Split(new char[] {':'}, 3);
164                         this.userIdInput = pieces[1];
165                     }
166                 }
167
168                 GUILayout.Label(ChatGui.WelcomeText);
169
170                 if (this.chatClient.TryGetChannel(selectedChannelName, this.doingPrivateChat, out this.selectedChannel))
171                 {
172                     for (int i = 0; i < this.selectedChannel.Messages.Count; i++)
173                     {
174                         string sender = this.selectedChannel.Senders[i];
175                         object message = this.selectedChannel.Messages[i];
176                         GUILayout.Label(string.Format("{0}: {1}", sender, message));
177                     }
178                 }
179
180                 GUILayout.EndScrollView();
181             }
182         }
183
184
185         GUILayout.BeginHorizontal();
186         if (doingPrivateChat)
187         {
188             GUILayout.Label("to:", GUILayout.ExpandWidth(false));
189             GUI.SetNextControlName("WhisperTo");
190             this.userIdInput = GUILayout.TextField(this.userIdInput, GUILayout.MinWidth(100), GUILayout.ExpandWidth(false));
191             string focussed = GUI.GetNameOfFocusedControl();
192             if (focussed.Equals("WhisperTo"))
193             {
194                 if (this.userIdInput.Equals("username"))
195                 {
196                     this.userIdInput = "";
197                 }
198             }
199             else if (string.IsNullOrEmpty(this.userIdInput))
200             {
201                 this.userIdInput = "username";
202             }
203
204         }
205         GUI.SetNextControlName("ChatInput");
206         inputLine = GUILayout.TextField(inputLine);
207         if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
208         {
209             GuiSendsMsg();
210         }
211         GUILayout.EndHorizontal();
212         GUILayout.EndArea();
213     }
File name: ChatGui.cs Copy
215     private void GuiSendsMsg()
216     {
217         if (string.IsNullOrEmpty(this.inputLine))
218         {
219
220             GUI.FocusControl("");
221             return;
222         }
223
224         if (this.inputLine[0].Equals('\\'))
225         {
226             string[] tokens = this.inputLine.Split(new char[] {' '}, 2);
227             if (tokens[0].Equals("\\help"))
228             {
229                 this.PostHelpToCurrentChannel();
230             }
231             if (tokens[0].Equals("\\state"))
232             {
233                 int newState = int.Parse(tokens[1]);
234                 this.chatClient.SetOnlineStatus(newState, new string[] { "i am state " + newState }); // this is how you set your own state and (any) message
235             }
236             else if (tokens[0].Equals("\\subscribe") && !string.IsNullOrEmpty(tokens[1]))
237             {
238                 this.chatClient.Subscribe(tokens[1].Split(new char[] {' ', ','}));
239             }
240             else if (tokens[0].Equals("\\unsubscribe") && !string.IsNullOrEmpty(tokens[1]))
241             {
242                 this.chatClient.Unsubscribe(tokens[1].Split(new char[] {' ', ','}));
243             }
244             else if (tokens[0].Equals("\\clear"))
245             {
246                 if (this.doingPrivateChat)
247                 {
248                     this.chatClient.PrivateChannels.Remove(this.selectedChannelName);
249                 }
250                 else
251                 {
252                     ChatChannel channel;
253                     if (this.chatClient.TryGetChannel(this.selectedChannelName, this.doingPrivateChat, out channel))
254                     {
255                         channel.ClearMessages();
256                     }
257                 }
258             }
259             else if (tokens[0].Equals("\\msg") && !string.IsNullOrEmpty(tokens[1]))
260             {
261                 string[] subtokens = tokens[1].Split(new char[] {' ', ','}, 2);
262                 string targetUser = subtokens[0];
263                 string message = subtokens[1];
264                 this.chatClient.SendPrivateMessage(targetUser, message);
265             }
266         }
267         else
268         {
269             if (this.doingPrivateChat)
270             {
271                 this.chatClient.SendPrivateMessage(this.userIdInput, this.inputLine);
272             }
273             else
274             {
275                 this.chatClient.PublishMessage(this.selectedChannelName, this.inputLine);
276             }
277         }
278
279         this.inputLine = "";
280         GUI.FocusControl("");
281     }
File name: NamePickGui.cs Copy
38     public void OnGUI()
39     {
40         // Enter-Key handling:
41         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
42         {
43             if (!string.IsNullOrEmpty(this.InputLine))
44             {
45                 this.StartChat();
46                 return;
47             }
48         }
49
50
51         GUI.skin.label.wordWrap = true;
52         GUILayout.BeginArea(guiCenteredRect);
53
54
55         if (this.chatComponent != null && string.IsNullOrEmpty(this.chatComponent.ChatAppId))
56         {
57             GUILayout.Label("To continue, configure your Chat AppId.\nIt's listed in the Chat Dashboard (online).\nStop play-mode and edit:\nScripts/ChatGUI in the Hierarchy.");
58             if (GUILayout.Button("Open Chat Dashboard"))
59             {
60                 Application.OpenURL("https://www.exitgames.com/en/Chat/Dashboard");
61             }
62             GUILayout.EndArea();
63             return;
64         }
65
66         GUILayout.Label(this.helpText);
67
68         GUILayout.BeginHorizontal();
69         GUI.SetNextControlName("NameInput");
70         this.InputLine = GUILayout.TextField(this.InputLine);
71         if (GUILayout.Button("Connect", GUILayout.ExpandWidth(false)))
72         {
73             this.StartChat();
74         }
75         GUILayout.EndHorizontal();
76
77         GUILayout.EndArea();
78
79
80         GUI.FocusControl("NameInput");
81     }

Download file with original file name:Control

Control 150 lượt xem

Gõ tìm kiếm nhanh...